我可以在棱鏡的源代碼中看到這個棱鏡XF:問題與創建「孩子」視圖模型
Container.RegisterType<INavigationService, UnityPageNavigationService>(_navigationServiceName);
爲什麼使用特定的名字呢?爲什麼不能有這樣的:
Container.RegisterType<INavigationService, UnityPageNavigationService>();
我問,因爲我有一個艱難的時間與孩子視圖模式:
class ItemViewModel : BindableBase
{
public ItemViewModel(INavigationService navigationService)
{
}
}
和我創建一個頁面上的項目:
class MainPageViewModel : BindableBase
{
public IEnumerable<ItemViewModel> Items { get; private set; }
public MainPageViewModel(Funct<ItemViewModel> itemFactory)
{
}
public void OnNavigatedTo(NavigationParameters parameters) {
Items = ... // Create items, where each item is created using itemFactory
}
}
但DI失敗,它會拋出異常,因爲它無法創建ItemViewModel
:
Microsoft.Practices.Unity.ResolutionFailedException: 依賴項的解決失敗,type =「MyApp.ViewModels.ItemViewModel」,name = 「(none)」。 發生異常時:解析時。 異常是:InvalidOperationException - 當前類型Prism.Navigation.INavigationService是一個接口,不能由 構造。
您是否缺少類型映射?
謝謝!我沒有在視圖模型之外使用NavigationService。 「ItemViewModel」是一個視圖模型。這不是頁面的視圖模型,如果這是你所指的。你有任何解決方案爲我的情況?我不認爲我的情況很特殊。 –
目前正在考慮中。但除非Xamarin能夠讓我們獲得View的父頁面,否則我們無法支持它。 –
但在我的情況下,它與視圖無關。我正在創建一個子視圖模型,我需要DI到navigationService才能工作。 –