【问题标题】:ViewModel property sort of fatal with VMDisconnectedException带有 VMDisconnectedException 的 ViewModel 属性有点致命
【发布时间】:2013-10-21 14:02:12
【问题描述】:

编辑 2:如果您正在寻找类似问题的答案,请查看 Stuart 的答案和我的 cmets。

编辑:我实际上得到了Mono.Debugger.Soft.VMDisconnectedException。我最近还安装了 Windows 8.1 和 Resharper(尽管 Resharper 现在已暂停)。

当我在我的 MVVMCross Xamarin iOS 应用程序中访问我的视图模型的一个非常简单的列表属性时,程序失败了。它大部分时间都不会退出:它的行为就像它正在运行一样。模拟器黑屏,也不例外。如果我在if (messagesViewModel != null) source.ItemsSource = messagesViewModel.Messages; 上设置断点,然后在即时窗口中输入messagesViewModel.Messages,一切都会停止,所以我可以判断它在这一行失败了。相反,如果我“跨过”,它永远不会移动到下一行。

当我在MvxTableViewSource 中切换此代码时,我遇到了类似的行为:

public override int RowsInSection(UITableView tableview, int section)
{
    return 1;
}

我的视图模型如下所示:

public class MessagesViewModel : MvxViewModel
{
    private List<BaseMessage> _messages = null;

    public List<BaseMessage> Messages
    {
        get
        {
            return _messages; //yes, I know I'm returning null
                              //I wasn't at first.
        }
    }

    public MessagesViewModel()
    {
    }
}

这是我在 MvxTableViewController 上的 ViewDIdLoad:

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

    var source = new MessagesTableViewSource(TableView);
    //was binding here, removed it for debug purposes

    //failure on second line here
    var messagesViewModel = ViewModel as MessagesViewModel;
    if (messagesViewModel != null) source.ItemsSource = messagesViewModel.Messages;

    TableView.Source = source;
    TableView.ReloadData();
}

一些初始化代码:

public class App : MvxApplication
{
    public App()
    {
        var appStart = new MvxAppStart<MessagesViewModel>();
        Mvx.RegisterSingleton<IMvxAppStart>(appStart);
    }
}

public partial class AppDelegate : MvxApplicationDelegate
{
    //empty functions removed.

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        Window = new UIWindow(UIScreen.MainScreen.Bounds);

        var presenter = new MvxTouchViewPresenter(this, Window);

        var setup = new Setup(this, presenter);
        setup.Initialize();

        var startup = Mvx.Resolve<IMvxAppStart>();
        startup.Start();

        Window.MakeKeyAndVisible();

        return true;
    }
}

【问题讨论】:

    标签: visual-studio-2012 windows-8 mono xamarin mvvmcross


    【解决方案1】:

    我怀疑是什么错误,它不在您发布的任何代码中。

    我刚刚创建了一个简单的 ViewModel:

    public class FirstViewModel 
        : MvxViewModel
    {
        private List<string> _items = new List<string>() { "One", "Two", "Three"};
        public List<string> Items 
        {   
            get { return _items; }
            set { _items = value; RaisePropertyChanged(() => Items); }
        }
    }
    

    还有一个简单的视图:

    [Register("FirstView")]
    public class FirstView : MvxTableViewController
    {
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
    
            // ios7 layout
            if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
               EdgesForExtendedLayout = UIRectEdge.None;
    
            var firstViewModel = ViewModel as FirstViewModel;
            var source = new MessagesTableViewSource(TableView);
            source.ItemsSource = firstViewModel.Items;
            TableView.Source = source;
        }
    
        public class MessagesTableViewSource : MvxTableViewSource
        {
            public MessagesTableViewSource(UITableView tableView) : base(tableView)
            {
                tableView.RegisterClassForCellReuse(typeof(MessagesCell), new NSString("MessagesCell"));
            }
    
            protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
            {
                return tableView.DequeueReusableCell("MessagesCell");
            }
        }
    
        public class MessagesCell : MvxTableViewCell
        {
            public MessagesCell(IntPtr handle)
                : base(handle)
            {
                var txt = new UILabel(new RectangleF(0, 0, 320, 44));
                Add(txt);
                this.DelayBind(() =>
                    {
                        this.CreateBinding(txt).Apply();
                    });
            }
        }
    }
    

    而且这段代码运行良好...


    我不会完全相信 Xamarin.iOS 与即时窗口的集成 - 它现在比以前更好,但我之前已经看到了几个问题。

    需要检查的一些事项:

    • 上面的代码对你有用吗?
      • 如果是这样,那么您的 BaseMessageMessagesTableViewSource 类中有什么 - 也许是它们导致了问题?
    • 可以使用Mvx.Trace("The list is {0}", messagesViewModel.Messages ?? "-null")查看列表吗?您可以在 ViewModel 属性get 中使用跟踪 - 它被调用了吗?您可以在 ViewModel 构造函数中使用跟踪吗?
    • 您的所有程序集都是针对相同版本的事物构建的吗?您的所有组件都确定重建了吗? (检查“构建|配置管理器”)- 您在 VS 和 Mac 中运行的是哪个版本的 Xamarin.iOS?

    【讨论】:

    • 我将开始研究您发布的代码。谢谢。同时,跟踪将不起作用,因为如果我访问 Messages 属性,它就会挂起。它只会挂在跟踪线上。我在两者上都使用了 Xamarin 的 alpha 版本,尽管在我在 Windows 上切换频道后 Xamarin 不会运行,它的更新程序也不会运行。我正在使用VS。该版本是 iOS 的 1.4.2 和 Android 的 4.8.03015。我 Mac 上的 iOS Xamarin 版本是 7.0.2.7... 这似乎有点不对劲。
    • 看你的代码,可能是我没有注册视图。
    • 它现在运行。谢谢!我唯一需要更改的是将[Register("MessagesView")] 移动到cs 文件而不是designer.cs 文件。
    猜你喜欢
    • 2014-01-12
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多