gpt4 book ai didi

c# - MvxBind :Error: 10. 40 View 绑定(bind)时抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:32 25 4
gpt4 key购买 nike

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

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

MvxBindingLayoutInflatorFactory 第 133 行!

我需要一些帮助:)

我的来源:

数据存储接口(interface):

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

}
}

基础 View 模型:

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

最佳答案

这很可能是您的 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”是我最喜欢的书之一:)

关于c# - MvxBind :Error: 10. 40 View 绑定(bind)时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com