2016-06-08 35 views
0

我試圖在ViewModel導航到時將服務注入。儘管我在Brian Lagunas的博客,示例代碼等上看過這方面的參考資料,但似乎並不正確。Xamarin Forms使用Unity註冊客戶服務以查看模型構造函數

鑑於我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>(); 
    } 

}

注:using報表。IUserAuthentication和UserAuthenticationService命名空間到位。

SignInPageViewModel.cs

private INavigationService _navigationService; 
private IUserAuthentication _userAuthenticationService; 

public SignInPageViewModel(INavigationService navigationService, IUserAuthentication userAuthenticationService) 
{ 
    _navigationService = navigationService; 
    _userAuthenticationService = userAuthenticationService; 
} 

Container.RegisterType似乎並不接受通用的。我見過的所有例子似乎都支持它,但我得到以下錯誤。

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

這是一個構建錯誤。

軟件包信息

  • 使用Prism.Unity.Forms V6.2.0-pre5
  • Prism.Forms 6.1.0-pre5
  • Prism.Core 6.1.0
  • Xamarin.Forms 2.3 .0.46-P​​RE3
  • 統一4.0.1

回答

9

RegisterType的通用版本是擴展方法,位於Microsoft.Practices.Unity名稱空間中。要使用它,或者把它明確

Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType<ISomething, SomeService>(Container); 

或添加

using Microsoft.Practices.Unity; 
+0

Gah!謝謝。仍然是.NET新手。我認爲(錯誤地)認爲Prism的Unity擴展會將其餘的Unity內容拉入。 有時,找到我想要的使用語句是最具挑戰性的一點。 – Danomite

-1

需要使用Microsoft.Practices.Unity的;

+0

花點時間閱讀[我怎樣寫一個很好的答案(http://stackoverflow.com/help/how-回答)。就目前而言,這個答案几乎是毫無用處的。 –

+1

謝謝你的反饋 –

相關問題