Wpf使用和事件和命令完成页面跳转
背景
wpf页面跳转通常是使用页面或者导航,本次使用事件和命令配合完成。
代码
使用事件
主页面代码
View层
<Window x:Name="taskView" x:Class="WPFToDoList.View.TaskView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFToDoList.ViewModel"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d" IsEnabled="{Binding IsHide}"
Style="{DynamicResource MyWindowStyle}" >
View.cs
public TaskView()
{
InitializeComponent();
this.DataContext = new TaskViewModel();
this.IsEnabledChanged += taskView_IsEnabledChanged;
}
private void taskView_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.IsEnabled)
this.Show();
else this.Hide();
}
Viewmodel层
private RelayCommand pomoCommand;
public RelayCommand PomoCommand
{
get
{
if (pomoCommand == null)
pomoCommand = new RelayCommand(PomoCommand_Execute);
return pomoCommand;
}
set { pomoCommand = value; }
}
private void PomoCommand_Execute(object obj)
{
PomodoroTimerView view = new PomodoroTimerView(this);
if (view.ShowDialog() == true)
{
//SetVisible(true);
}
}
public void SetVisible(bool Status)
{
IsHide = Status;
}
子页面代码
view.xaml
<Window x:Class="WPFToDoList.View.PomodoroTimerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFToDoList.View"
mc:Ignorable="d"
Title="专注一下" Height="350" Width="525" Closing="Window_Closing" >
subView.cs
public PomodoroTimerView(TaskViewModel taskViewModel)
{
InitializeComponent();
mainViewModel= taskViewModel;
mainViewModel.SetVisible(false);
}
private TaskViewModel mainViewModel;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mainViewModel.SetVisible(true);
this.DialogResult = true;
}
使用导航的方式明天再更新。