0

我使用 Monotouch.Dialog(Xamarin.iOS 版本:6.2.0.65)创建一个带有 RadioElements 的 RadioGroup。我只想要第二个屏幕中的“搜索”功能(用户从长列表中选择),所以我创建了一个单独的 DialogViewController,我在其中启用了搜索过滤器(enableSearch = true)。

在搜索框中键入时,应用程序在 RadioElement 的 GetCell 方法中崩溃。

Element.cs 的第 1064 行上的“对象引用未设置为对象的实例”。我有 GC 问题吗?我设法在一个小应用程序中模拟它......不要注意一些奇怪的领域成员,那是我测试我是否开始GC......

using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace SearchCrash
{
// The UIApplicationDelegate for the application. This class is responsible for launching the 
// User Interface of the application, as well as listening (and optionally responding) to 
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    // class-level declarations
    UIWindow window;
    public UINavigationController NavigationController { get; set; }

    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        window = new UIWindow(UIScreen.MainScreen.Bounds);
        this.NavigationController = new UINavigationController();
        this.NavigationController.PushViewController(new FirstViewController(this.NavigationController), true);
        window.RootViewController = this.NavigationController;
        window.MakeKeyAndVisible();

        return true;
    }
}

public class FirstViewController : DialogViewController
{
    private UINavigationController controller;
    private LocatiesRootElement locRootElement;
    private RadioGroup radioGroup;

    public FirstViewController(UINavigationController c) : base(new RootElement("Test"), true)
    {
        this.controller = c;
    }

    public override void ViewDidLoad()
    {
        Section section = new Section();
        radioGroup = new RadioGroup(0);
        locRootElement = new LocatiesRootElement("test", radioGroup, this.controller);
        section.Add(locRootElement);
        Root.Add(section);
    }
}

public class LocatiesRootElement : RootElement
{
    private UINavigationController navigationController;
    private LocatiesViewController locatiesViewController;

    public LocatiesRootElement(string selectedLocatie, RadioGroup group, UINavigationController controller) : base("Locatie", group)
    {
        this.navigationController = controller;
        this.locatiesViewController = new LocatiesViewController(this);
    }

    public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        this.navigationController.PushViewController(this.locatiesViewController, true);
    }
}

public class LocatiesViewController : DialogViewController
{
    public LocatiesViewController(LocatiesRootElement root) : base(root, true)
    {
        this.EnableSearch = true;
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        Section sectionList = new Section();

        RadioElement radioElement1 = new RadioElement("item 1");
        RadioElement radioElement2 = new RadioElement("item 2");
        sectionList.Add(radioElement1);
        sectionList.Add(radioElement2);

        Root.Add(sectionList);
    }           
}
}
4

2 回答 2

0

在实例化DialogViewController.

像这样 :

public LocatiesRootElement(string selectedLocatie, RadioGroup group, UINavigationController controller) : base("Locatie", group)
    {
        this.navigationController = controller;
        this.locatiesViewController = new LocatiesViewController(this);
        this.locatiesViewController.EnableSearch = true;
    }

希望这对你有用。

于 2013-03-11T16:36:49.447 回答
0

此处的错误报告包括针对您遇到的问题的根本原因的解决方法,但还讨论了过滤将如何导致即使在应用过滤器后将第 n 个元素标记为选中的可用性问题。

https://github.com/migueldeicaza/MonoTouch.Dialog/issues/203

如果您不想更新核心 MTD 代码,您可以通过将其放入您自己的 UIBarSearchDelegate 来使用相同的技术。不幸的是,默认的 SearchDelegate 类是内部的,因此您需要在委托中添加所有代码。我能够做到这一点并让它在不更改 MTD 源的情况下工作:

    public override void LoadView()
    {
        base.LoadView();
        ((UISearchBar)TableView.TableHeaderView).Delegate = new MySearchBarDelegate(this);
    }

然后你使用它而不是基本方法:

public override void TextChanged (UISearchBar searchBar, string searchText)
{
    container.PerformFilter (searchText ?? "");
    foreach (var s in container.Root)
        s.Parent = container.Root;
}
于 2014-09-28T15:58:26.770 回答