MonoTouch.Dialog 搜索栏在返回视图时会丢失搜索查询

本文关键字:搜索 查询 视图 Dialog 搜索栏 返回 MonoTouch | 更新日期: 2023-09-27 18:32:19

我正在使用MT。D 列出 DialogViewController 中的员工。启用搜索已打开,您可以筛选列表中的项目。但是,如果您推送到另一个视图,然后返回,则搜索栏为空。我能够让它恢复通过覆盖OnSearchTextChanged (string text)并将字符串存储到本地字段使用的搜索查询,当视图重新成为焦点时,我使用以下代码:

public override ViewDidAppear (bool animated)
{
    base.ViewDidAppear (animated);
    if (EnableSearch && !string.IsNullOrWhiteSpace (lastSearchQuery))
    {
        this.SearchButtonClicked (lastSearchQuery); // this inserts text
        this.StartSearch (); // no clue what this is doing
        this.ReloadData (); // does nothing but was worth a try
    }
}

该代码将文本插入搜索栏并显示它,但除非您键入某些内容,否则我无法对其进行过滤。键盘进入视图,并且有一个搜索按钮,但它不执行任何操作。有什么建议吗?

MonoTouch.Dialog 搜索栏在返回视图时会丢失搜索查询

我认为您缺少的只是对DialogViewController上的PerformFilter的调用。

我键入了一个快速示例来显示行为。 我从来没有看到你观察到的确切行为。 我不必重新填充搜索字段。 作为参考,我使用的是单点触控 5.2.11。

using System;
using System.Linq;
using MonoTouch.UIKit;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
namespace delete201204242A
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow _window;
        UINavigationController _nav;
        MyDialogViewController _rootVC;
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            _window = new UIWindow (UIScreen.MainScreen.Bounds);
            RootElement _rootElement = new RootElement ("LINQ root element") {
                new Section ("List") {
                    from x in new Expense [] { new Expense () {Name="one"}, new Expense () {Name="two"}, new Expense () {Name="three"} }
                    select (Element)new BindingContext (null, x, x.Name).Root
                }
            };
            _rootVC = new MyDialogViewController (_rootElement);
            _rootVC.EnableSearch = true;
            _nav = new UINavigationController (_rootVC);
            _window.RootViewController = _nav;
            _window.MakeKeyAndVisible ();
            return true;
        }
        public class MyDialogViewController : DialogViewController
        {
            public MyDialogViewController (RootElement root) : base (root) {}
            public string SearchString { get; set; }            
            public override void ViewDidAppear (bool animated)
            {
                base.ViewDidAppear (animated);
                if (!string.IsNullOrEmpty (SearchString))
                    this.PerformFilter (SearchString);
            }
            public override void OnSearchTextChanged (string text)
            {
                base.OnSearchTextChanged (text);
                SearchString = text;
            }
        }
        public class Expense
        {
            [Section("Expense Entry")]
            [Entry("Enter expense name")]
            public string Name;
            [Section("Expense Details")]
            [Caption("Description")]
            [Entry]
            public string Details;
        }
    }
}