Xamarin表单使用Unity为View模型构造器注册客户服务

Dan*_*ite 1 c# prism unity-container xamarin.forms

我正试图在导航到ViewModel时将服务注入.尽管我已经看过Brian Lagunas的博客,示例代码等,但有些东西似乎不对.

在我的App.cs中给出

public class App : PrismApplication
{
    protected override void OnInitialized ()
    {
        NavigationService.NavigateAsync ("SignInPage", animated: false);
    }

    protected override void RegisterTypes ()
    {
        Container.RegisterTypeForNavigation<SignInPage>();
        Container.RegisterType<IUserAuthentication, UserAuthenticationService>();
    }
Run Code Online (Sandbox Code Playgroud)

}

注意:usingIUserAuthentication和UserAuthenticationService命名空间的语句已到位.

SignInPageViewModel.cs

private INavigationService _navigationService;
private IUserAuthentication _userAuthenticationService;

public SignInPageViewModel(INavigationService navigationService, IUserAuthentication userAuthenticationService)
{
    _navigationService = navigationService;
    _userAuthenticationService = userAuthenticationService;
}
Run Code Online (Sandbox Code Playgroud)

Container.RegisterType似乎不接受通用.我在Unity中看到的所有示例似乎都支持它,但是我收到以下错误.

The non-generic method 'IUnityContainer.RegisterType(Type, Type, string, LifetimeManager, params InjectionMember[])' cannot be used with type arguments
Run Code Online (Sandbox Code Playgroud)

这是一个构建错误.

包装信息

  • 使用Prism.Unity.Forms v6.2.0-pre5
  • Prism.Forms v6.1.0-pre5
  • Prism.Core 6.1.0
  • Xamarin.Forms 2.3.0.46-pre3
  • Unity 4.0.1

Hau*_*ger 9

通用版本RegisterType是扩展方法,位于Microsoft.Practices.Unity命名空间中.要使用它,请显式调用它

Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType<ISomething, SomeService>( Container );
Run Code Online (Sandbox Code Playgroud)

或添加

using Microsoft.Practices.Unity;
Run Code Online (Sandbox Code Playgroud)