Wpf使用和事件和命令完成页面跳转

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;
        }

使用导航的方式明天再更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值