MvxBind:错误:10.40视图绑定期间抛出异常

时间:2012-10-19 11:46:38

标签: c# xamarin.android mvvmcross

我正在尝试使用提供的代码创建一个简单的服务,但我不明白为什么在绑定时会出现异常。

10-19 11:42:09.148 I / mono-stdout(1622):MvxBind:错误:10.40视图绑定期间抛出异常

MvxBindingLayoutInflatorFactory第133行!

我需要一些帮助,请:)

我的来源:

DataStore接口:

using Core.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Core.Interfaces
{
    public interface IDataStore
    {
        void UpdateFeed(FeedModel feedModel);
        void DeleteFeed(FeedModel feedModel);
        void CreateFeed(FeedModel feedModel);
        FeedModel GetFeed(Uri uri);
        ObservableCollection<FeedModel> Feeds { get; }
    }
}

DataStore类:

using System;
using System.Collections.Generic;
using System.Linq;

using Core.Interfaces;
using System.Collections.ObjectModel;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.Interfaces.Platform;
using Cirrious.MvvmCross.Interfaces.Localization;
using Cirrious.MvvmCross.ExtensionMethods;
using Core.Helpers;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;

namespace Core.Models
{
    public class DataStore
        : IDataStore
        , IMvxServiceConsumer<IMvxSimpleFileStoreService>
        , IMvxServiceConsumer<IMvxResourceLoader>
    {
        public DataStore()
        {
            Load();
        }

        public void UpdateFeed(FeedModel feedModel)
        {
            var toUpdate = this.m_feeds.First(feed => feed.Url == feedModel.Url);
            toUpdate.CloneFrom(feedModel);
            Save();
        }

        public void DeleteFeed(FeedModel feedModel)
        {
            this.m_feeds.Remove(this.m_feeds.First(feed => feed.Url == feedModel.Url));
            Save();
        }

        public void CreateFeed(FeedModel feedModel)
        {
            this.m_feeds.Add(feedModel);
            Save();
        }

        public FeedModel GetFeed(Uri uri)
        {
            return this.m_feeds.First(feed => feed.Url == uri);
        }

        private void Load()
        {
            var fileService = this.GetService<IMvxSimpleFileStoreService>();
            if (!fileService.TryReadBinaryFile(LocationDataService.StoreFileName, LoadFrom))
            {
                var resourceLoader = this.GetService<IMvxResourceLoader>();
                resourceLoader.GetResourceStream(LocationDataService.ResourceFileName, (inputStream) => LoadFrom(inputStream));
            }
        }

        private bool LoadFrom(Stream inputStream)
        {
            try
            {
                var loadedData = XDocument.Load(inputStream);
                if (loadedData.Root == null)
                    return false;

                using (var reader = loadedData.Root.CreateReader())
                {
                    var list = (List<FeedModel>)new XmlSerializer(typeof(List<FeedModel>)).Deserialize(reader);
                    this.m_feeds = new ObservableCollection<FeedModel>(list);
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        private void Save()
        {
            var fileService = this.GetService<IMvxSimpleFileStoreService>();
            fileService.WriteFile(LocationDataService.StoreFileName, (stream) =>
            {
                var serializer = new XmlSerializer(typeof(List<FeedModel>));
                serializer.Serialize(stream, m_feeds.ToList());
            });
        }

        private ObservableCollection<FeedModel> m_feeds;
        public ObservableCollection<FeedModel> Feeds
        {
            get { return this.m_feeds; }
        }

    }
}

BaseViewModel:

using Cirrious.MvvmCross.Commands;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Commands;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.ViewModels;
using Core.Interfaces;

namespace Core.ViewModels
{
    public class BaseViewModel
        : MvxViewModel
        , IMvxServiceConsumer<IDataStore>
    {
        protected IDataStore DataStore
        {
            get { return this.GetService<IDataStore>(); }
        }
    }
}

FeedManagerViewModel:

using Cirrious.MvvmCross.Commands;
using Cirrious.MvvmCross.Interfaces.Commands;
using Core.Controls;
using Core.Models;
using System;
using System.Collections.ObjectModel;

namespace Core.ViewModels
{
    public class FeedsManagerViewModel
        : BaseViewModel
    {

        public ObservableCollection<FeedModel> Feeds { get { return this.DataStore.Feeds; } }

       ...
    }
}

查看xml:

<Mvx.MvxBindableListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            local:MvxBind="{'ItemsSource':{'Path':'Feeds'}, 'ItemClick':{'Path':'DisplayItemCommand'}}"
            local:MvxItemTemplate="@layout/feedlist_viewmodel" />

1 个答案:

答案 0 :(得分:0)

这很可能是你的XML中的一个错误......但很难从这一行跟踪中看出来。

你在运行什么版本的MvvmCross?

Master和vNext的tip版本将第133行显示为

  MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception during creation of {0} from type {1} - exception {2}", name, viewType.FullName, exception.ToLongString());

所以希望如果您使用提示,那么这应该会为您提供更多关于出错的信息。

除此之外,您总是可以尝试在违规行上设置断点以提取更多信息。


如果异常在第99行,则更改从那里记录的错误:

MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception thrown during the view binding ", exception.ToLongString());

为:

MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception thrown during the view binding {0}", exception.ToLongString());

错误将出现在某处:)


另一个好的调试技术是逐个注释掉行,直到问题消失为止 - 这有助于确定问题所在。

你有一个有效的开发环境和一个非常强大的调试器 - 使用它是一项很好的学习技巧。 'Code Complete'是我最喜欢的书之一:)