首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MvxBind:错误: 10.40视图绑定过程中抛出异常

MvxBind:错误: 10.40视图绑定过程中抛出异常
EN

Stack Overflow用户
提问于 2012-10-19 19:46:38
回答 1查看 543关注 0票数 2

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

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

MvxBindingLayoutInflatorFactory第133行!

我需要一些帮助:)

我的来源:

DataStore接口:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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" />
EN

回答 1

Stack Overflow用户

发布于 2012-10-19 20:01:42

这很可能是XML中的错误...但仅仅从那一行痕迹就很难分辨出来。

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

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

代码语言:javascript
复制
  MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception during creation of {0} from type {1} - exception {2}", name, viewType.FullName, exception.ToLongString());

因此,希望如果你使用这个技巧,那么它应该会给你更多关于哪里出了问题的信息。

除此之外,您始终可以尝试在有问题的行上设置断点,以提取更多信息。

如果异常位于第99行,则将错误记录从以下位置更改为:

代码语言:javascript
复制
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception thrown during the view binding ", exception.ToLongString());

至:

代码语言:javascript
复制
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception thrown during the view binding {0}", exception.ToLongString());

错误会出现在某个地方:)

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

您已经有了一个工作的开发环境和一个非常强大的调试器-使用它是一项很好的学习技能。“'Code”是我最喜欢的书之一:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12973594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档