当这种情况发生时我讨厌它,但是在发布我的问题后,我离开了电脑一会儿,开始做其他事情。在那一点上,一切都到位了。 Stuart,在回答您的问题时,TodayPanel 不是 MvxModelView,这是问题的症结所在。我所做的是将 TodayPanels 列表传递到列表视图中,这是一个 SQLite 实体对象,而不是 MvxModelView 对象。
对于其他可能遇到此问题的人,我将在此处发布我的解决方案。
所以这就是我最终要做的。我首先为继承自 MvxModelView 的抽象基类的每个 TodayPanel 实体对象创建了一个类。
public abstract class TodayBaseViewModel : MvxViewModel
{
protected TodayViewModel TodayViewModel { get; set; }
protected IDataService DataService { get; set; }
public String Name { get; set; }
public String Title { get; set; }
public Boolean CanHide { get; set; }
public Boolean Visible { get; set; }
public Int32 SortOrder { get; set; }
public String View { get; set; }
protected abstract void SetEventHandlers();
protected BaseViewModel(IDataService dataService)
{
DataService = dataService;
}
public void Init(TodayViewModel todayViewModel)
{
TodayViewModel = todayViewModel;
SetEventHandlers();
}
}
我将其抽象化,因为我希望在最终类中附加 0 个或更多事件处理程序。这是通过抽象的 SetEventHandlers() 方法完成的:
public class CoachSaysViewModel : TodayBaseViewModel
{
public CoachSaysViewModel(IDataService dataService)
: base(dataService)
{
}
protected override void SetEventHandlers()
{
TodayViewModel.ConnectionUpdated += TodayViewModelConnectionUpdated;
TodayViewModel.NewActivityReceived += TodayViewModelNewActivityReceived;
}
protected void TodayViewModelNewActivityReceived(Object sender, EventArgs.ActivityReceivedEventArgs e)
{
}
protected void TodayViewModelConnectionUpdated(Object sender, EventArgs.ConnectionUpdatedEventArgs e)
{
}
}
然后我创建了一个扩展方法,将 TodayPanel 实体转换为继承自 TodayBaseViewModel 的类之一。
public static BaseViewModel ToBaseViewModel(this TodayPanel todayPanel, TodayViewModel todayViewModel)
{
BaseViewModel model = null;
switch (todayPanel.View)
{
case "Today_QuickView":
model = Mvx.IocConstruct<QuickViewViewModel>();
break;
case "Today_CoachSays":
model = Mvx.IocConstruct<CoachSaysViewModel>();
break;
}
if (model == null)
return null;
model.CanHide = todayPanel.CanHide;
model.Name = todayPanel.Name;
model.SortOrder = todayPanel.SortOrder;
model.Title = todayPanel.Title;
model.View = todayPanel.View;
model.Visible = todayPanel.Visible;
model.Init(todayViewModel);
return model;
}
然后允许我创建一个 MvxViewModels 列表,然后绑定到 MvxListView 并因此允许执行我想要执行的附加处理。
我确信我可以对最终结果进行一些改进,如果您看到任何内容,请随时指出。 :)