我试图从单独的程序集中使用视图和查看模型 . 我已经阅读了答案:https://stackoverflow.com/a/17200358/4550962和https://stackoverflow.com/a/35358504/4550962并没有让我感到满意 .
GetViewAssemblies()和GetViewModelAssemblies()方法没有任何效果 - 未加载视图 . InitializeViewLookup()方法按预期工作,但它需要手动配对所有视图和视图模型 .
-
为重现问题而采取的步骤:
-
运行MvvmCross_Forms_Examples解决方案
For 001XAML, Example.W81:
-
将mvvmcross更新为4.2.3,将Xamarin.Forms更新为2.3.1.114
-
创建两个新的可移植项目库:Views,ViewModels
-
为库添加Silverlight目标
-
将目录 Pages 和 ViewModels 移动到apriopriate可移植库
-
添加必要的参考资料 .
-
向Setup.cs添加代码:
protected override IEnumerable<Assembly> GetViewAssemblies()
{
var list = new List<Assembly>();
list.AddRange(base.GetViewAssemblies());
list.Add(typeof(Example.Pages.FirstPage).GetTypeInfo().Assembly);
return list;
}
protected override IEnumerable<Assembly> GetViewModelAssemblies()
{
var list = new List<Assembly>();
list.AddRange(base.GetViewModelAssemblies());
list.Add(typeof(Example.ViewModels.FirstViewModel).GetTypeInfo().Assembly);
return list;
}
- 运行 - 效果为
ArgumentNullException
:值不能为空 . 参数名称:newRoot
Xamarin.Forms.Platform.WinRT.Platform.SetPage(Page newRoot)位于Example.W81.Example_W81_XamlTypeInfo的Example.W81.MainPage..ctor()中的Xamarin.Forms.Platform.WinRT.WindowsBasePage.LoadApplication(应用程序应用程序) Example.W81.Example_W81_XamlTypeInfo.XamlUserType.ActivateInstance()中的.XamlTypeInfoProvider.Activate_4_MainPage()
- 评论代码来自6)并添加:
protected override void InitializeViewLookup()
{
var viewModelLookup = new Dictionary<Type, Type>
{
{typeof(ViewModels.AboutViewModel), typeof(Pages.AboutPage)},
{typeof(ViewModels.FirstViewModel), typeof(Pages.FirstPage)}
};
var container = Mvx.Resolve<IMvxViewsContainer>();
container.AddAll(viewModelLookup);
}
- Run - 就像魅力一样