我已經搜索並嘗試了幾天,最後必須在這裏提出問題。 我有一個Silverlight 5應用程序,使用MVVM Light,我希望能夠在主視圖中動態切換視圖。Silverlight在使用MVVM Light框架的視圖中切換視圖
爲了簡單起見,可以說我有2個按鈕。
Button1將切換到TestView1。
Button2將切換到TestView2。
<Button Content="TestView1" Grid.Column="1" Command="{Binding CallTestView1Command}" HorizontalAlignment="Left" Margin="185,17,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="TestView2" Grid.Column="1" Command="{Binding CallTestView2Command}" HorizontalAlignment="Left" Margin="280,17,0,0" VerticalAlignment="Top" Width="75"/>
我做它是由relaycommand結合的按鈕,然後instanciating對應視圖的新視圖模型的方式。 即:
private RelayCommand _callTestView1Command;
public RelayCommand CallTestView1Command
{
get
{
return _callTestView1Command ??
(_callTestView1Command = new RelayCommand(() =>
{
CurrentView = ViewModelLocator.NinjectKernel.Get<TestViewModel1>();
}));
}
}
的CurrentViewmodel然後設置爲新視圖模型。 在的MainView我已綁定了CurrentView到ContentControl中:
<Border x:Name="displayedView" Grid.Row="2">
<ContentControl Content="{Binding CurrentView}" />
</Border>
這將實際工作在一定程度上,因爲CurrentView會改變,但不是實際顯示視圖的內容也只是顯示出的命名空間ViewModel是instanciated。
到目前爲止,我主要使用從這些來源採取的知識:
http://rachel53461.wordpress.com/2011/05/28/switching-between-viewsusercontrols-using-mvvm/
Loading Views into ContentControl and changing their properties by clicking buttons
,但他們不解決我的問題,還是我不太明白如何實際顯示views :-(
因此,沒有人有一個很好的解釋,如何使用GalaSoft的MVVM Light在Silverlight 5中正確切換視圖。
感謝
在CallTestView1Command第一設定CurrentView = NULL;然後分配新的項目,它也許可以解決這個問題 – Masoomian