c# - 从 MainWindow.xaml.cs 调用 ShowWindowCommand()
问题描述
我想从我的 MainWindow() 中调用函数 ShowWindowCommand() 和 HideWindowCommand()。
我有以下课程:
public class NotifyIconViewModel
{
public ICommand ShowWindowCommand
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () => Application.Current.MainWindow == null,
CommandAction = () =>
{
Application.Current.MainWindow = new WorkingTimer.MainWindow();
Application.Current.MainWindow.Show();
}
};
}
}
public ICommand HideWindowCommand
{
get
{
return new DelegateCommand
{
CommandAction = () => Application.Current.MainWindow.Close(),
CanExecuteFunc = () => Application.Current.MainWindow != null
};
}
}
通常,从 NotifyIconRessource.xaml 中的上下文菜单调用 ShowWindowCommand() 和 HideWindowsCommand()
<ContextMenu x:Shared="false" x:Key="SysTrayMenu">
<MenuItem Header="Show Window" Command="{Binding ShowWindowCommand}"/>
<MenuItem Header="Hide Window" Command="{Binding HideWindowCommand}"/>
<Separator />
<MenuItem Header="Reset Timer" Command="{Binding ResetTimerCommand}"/>
<Separator />
<MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}" />
</ContextMenu>
我怎样才能做到这一点 ?
解决方案
您首先必须创建该类的公共属性,然后将其设置为视图的数据上下文:
public NotifyIconViewModel NotifyIconVM { get; set; }
MainWindow()
{
InitializeComponent();
NotifyIconVM = new NotifyIconViewModel();
DataContext = NotifyIconVM;
}
设置数据上下文将告诉视图在哪里查找命令。
推荐阅读
- javascript - JQuery - 表单仅在单击按钮时出现
- spring-boot - 如何在春季转义用于sql注入的输入字符串?
- rabbitmq - 在 RabbitMQ 中长按 ack 可以吗?
- python - 尝试在 RobotStudio 上打开工作站时出错
- javascript - 当我向 HTML 寻址时,它会导致属性错误
- terragrunt - 如何在亚特兰蒂斯的一个目录上运行多个工作流
- docker - 针对不同 CUDA 架构的 Pytorch 安装
- laravel - 注销 laravel 账号
- php - 如何检查复选框是否被选中php
- pyspark - 在 Azure Databricks 中同时运行笔记本