2

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

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

MvxBindingLayoutInflatorFactory 第 133 行!

我需要一些帮助:)

我的来源:

数据存储接口:

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; }
    }
}

数据存储类:

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; }
        }

    }
}

基础视图模型:

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" />
4

1 回答 1

0

这很可能是您的 XML 中的一个错误……但仅从那一行跟踪就很难分辨。

你运行的是什么版本的 MvvmCross?

Master 和 vNext 的提示版本都将第 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());

错误将在某处:)


另一种很好的调试技术是逐行注释,直到问题消失——这有助于确定问题出在哪里。

你有一个工作的开发环境和一个非常强大的调试器——使用它是一项很好的学习技能。“代码完成”是我最喜欢的书之一 :)

于 2012-10-19T12:01:41.760 回答