我正試圖學習如何正確使用Autofac與wpf和mvvm場景的接口並綁定一個contentcontrol座標在我的主窗口到一個'CurrentView'屬性關閉mainViewModel,但我不知道我是否應該使用具體的BaseViewModel類來做到這一點,或者如果我應該有CurrentView屬性作爲接口?我想在我的MainWindowViewModel構造函數中設置CurrentView屬性。新手學習如何正確使用IoC與wpf,mvvm,並跟蹤CurrentView屬性以便與DataTemplates一起使用
簡單定位:
public class ViewModelLocator
{
public IContainer Container { get; set; }
public IMainWindowViewModel MainWindowViewModel
{
get
{
return Container.Resolve<IMainWindowViewModel>();
}
}
public IOtherPageViewModel OtherPageViewModel
{
get
{
return Container.Resolve<IOtherPageViewModel >();
}
}
}
在我MainWindow.xaml坐在
<ContentControl
Content="{Binding Path=CurrentView}"
Grid.Row="0"
VerticalContentAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch" >
</ContentControl>
MainWindowViewModel *這是我關心的,應該baseViewModel不是一個具體的,我應該在這裏實現接口,或什麼是當你需要顯示不同的視圖,爲多個viewModels設置currentView的正確方法?我問,因爲我想讓Autofac在MainWindowViewModel構造函數中注入我的當前視圖。這可能嗎?
private baseViewModel currentView;
public baseViewModel CurrentView
{
get
{
return currentView;
}
set
{
currentView = value;
NotifyPropertyChanged(m => m.CurrentView);
}
}
在我用baseViewModels然後過去的DataTemplates的看法,但我不知道如果多數民衆贊成狠抓落實
所以我去了baseViewModel的路線,用Autofac注入一個IBaseInterface到MainViewModel的構造函數中,然後設置CurrentView。然後使用xaml中的DataTemplate進行視圖切換。這個實現可以嗎? – cjsmith