为MvvmCross提供额外的View程序集

时间:2017-01-16 04:25:18

标签: c# xamarin.forms mvvmcross

在Xamarin Forms应用程序中,我想提供另一个View程序集(而不是默认程序集),MvvmCross可以在其中查找Views。

在关注MvvmCross Wiki后,在UWP的安装程序类中,我override GetViewAssemblies()添加其他程序集:

    protected override IEnumerable<Assembly> GetViewAssemblies()
    {
        var viewAssemblies = new List<Assembly>();
        viewAssemblies.AddRange(base.GetViewAssemblies());
        viewAssemblies.Add(typeof(MovementPage).GetTypeInfo().Assembly);

        return viewAssemblies;
    }

但是应用抱怨,它无法找到视图:

mvx:Diagnostic: 10.46 Setup: Secondary end
mvx:Diagnostic: 10.51 Showing ViewModel MovementViewModel
Exception thrown: 'System.Collections.Generic.KeyNotFoundException' in MvvmCross.Core.dll
mvx:Diagnostic: 10.82 Page not found for MovementPage
mvx:Error: 10.84 Skipping request for MovementViewModel
Exception thrown: 'System.ArgumentNullException' in Xamarin.Forms.Platform.UAP.dll
Exception thrown: 'System.ArgumentNullException' in MovementTimer.UWP.exe

当此页面出现在默认程序集(包含ViewModels,App.cs的程序集)中时,将显示页面。

这里可以做些什么?

1 个答案:

答案 0 :(得分:0)

覆盖InitializeViewLookup()有效,但这很乏味:(

protected override void InitializeViewLookup()
{
    var viewsLookup = new Dictionary<Type, Type>
    {
        [typeof(MovementViewModel)] = typeof(MovementPage)
    };

    var container = Mvx.Resolve<IMvxViewsContainer>();
    container.AddAll(viewsLookup);
}