- 在MvvmLight框架下
- MainView.xmal中放置ContentControl 控件,两个button按钮
<ContentControl x:Name="contentcontrol1" Content="{Binding MainContent}"/>
<Button
Name="tab1"
Command="{Binding tabCommand}"
CommandParameter="tab1"
Content="tab1" />
<Button
Name="tab2"
Command="{Binding tabCommand}"
CommandParameter="tab2"
Content="tab2" />
- 创建Page1.xmal 和Page2.xmal, 在Page1.xmal.cs中添加
public static Page1 instance;
public static Page1 Instance
{
get
{
if (instance == null)
{
Console.WriteLine("new Page1.................");
instance = new Page1();
}
return instance;
}
}
- 在 MainView.xmal对应的 MainViewModel.cs中添加
public RelayCommand<object> tabCommand { get; set; }
this.tabCommand = new RelayCommand<object>(ChangeTab);
private void ChangeTab(object o)
{
string parameter = (string)o;
switch (parameter)
{
default:
break;
case "tab1":
this.MainContent = new Frame() { Content = Page1.Instance };
break;
case "tab2":
this.MainContent = new Frame() { Content = Page2.Instance };
break;
}
}