c# - INavigation 无法在 Xamarin.Forms 中使用 Prism

标签 c# mvvm dependency-injection xamarin.forms prism-6

我是 xamarin 形式的棱镜新手。为了使用 xamarin 实现 prism,我安装了三个软件包:

  • 棱镜芯
  • Prism.Forms
  • Prism.Unity.Forms

  • 并像这样从 PrismApplication 继承 App:
      public partial class App : PrismApplication { }
    

    我想从文件后面的代码中将 View 与 ViewModel 绑定(bind),如下所示:
        this.BindingContext=ViewModelLocator.ViewModelName
    

    为此,我找到了这段代码。此代码在 App.xaml.cs 中:
    protected override void ConfigureViewModelLocator()
    {
        base.ConfigureViewModelLocator();
        BindViewModelToView<LoginRegisterViewModel, MainPage>();
    }
    public void BindViewModelToView<TViewModel, TView>()
    {
        ViewModelLocationProvider.Register(typeof(TView).ToString(), () => Container.Resolve<TViewModel>());
    }
    

    这是我的 ViewModel 代码:
    public class LoginRegisterViewModel : BindableBase
    {
        #region Private variables
        private readonly IPageDialogService _diglogService;
        private readonly INavigationService _navigationService;
        public  Action Test { get; set; }
        #endregion
    
        #region Constructor
        public LoginRegisterViewModel(IPageDialogService diglogService, INavigationService navigationService)
        {
            _diglogService = diglogService;
             _navigationService = navigationService;
    
        }
        #endregion
    }
    

    当我删除 INavigationService 时它工作正常.

    但它显示错误:

    Unhandled Exception:

    Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "DSMobile.ViewModels.LoginRegisterViewModel", name = "(none)".
    
    Exception occurred while: while resolving.
    
    Exception is: InvalidOperationException - The current type, Prism.Navigation.INavigationService, is an interface and cannot be constructed. Are you missing a type mapping?
    


    在异常发生时,容器是:
    Resolving DSMobile.ViewModels.LoginRegisterViewModel,(none)    
    
    Resolving parameter "navigationService" of constructor DSMobile.ViewModels.LoginRegisterViewModel(Prism.Services.IPageDialogService diglogService, Prism.Navigation.INavigationService navigationService)
    
    Resolving Prism.Navigation.INavigationService,(none)
    

    最佳答案

    我知道这是在提出问题很久之后,但我通过手动将 INavigationService 映射到 PageNavigationService 解决了这个问题,就像这样

    container.Register<INavigationService, PageNavigationService>();
    

    我希望这对遇到此问题的其他人有所帮助。

    关于c# - INavigation 无法在 Xamarin.Forms 中使用 Prism,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540910/

    相关文章:

    c# - EF 6 检索子自动生成的外键的父级

    c# - 如何使用 json.net 在 POST 请求上进行 asp.net core 模型绑定(bind)?

    c# - Type.GetType 失败但 property.PropertyType 工作正常

    c# - 真实项目依赖注入(inject)的 F# 模拟

    java - Setter 注入(inject)不适用于 Spring 的工厂方法注入(inject)

    c# - IDatabaseService 依赖注入(inject)失败?

    c# - 在命令中移动滚动条

    c# - 如何实现属性更改通知

    c# - 使用 MVVM 进行正确验证

    java - 当我有很多 SpringBootTest 类时,如何有效地使用嵌套配置类来注入(inject)依赖项