Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF上下文菜单命令未正确设置IsEnabled_C#_Wpf - Fatal编程技术网

C# WPF上下文菜单命令未正确设置IsEnabled

C# WPF上下文菜单命令未正确设置IsEnabled,c#,wpf,C#,Wpf,我正在尝试实现和使用WPF“”项目 我已经在我的项目中安装了NuGet包,并逐字复制了一些文件,而其他文件则以我自己的方式实现。然而,我使用的是.NETCore3.0,而不是最初为其设计的.NetFramework。我确实认为它支持.NETCore3.0 问题是,当我右键单击托盘图标时,“显示命令”和“隐藏命令”菜单项未正确启用或禁用,即使我测试它们的“CanExecute”方法返回的值与它们的“enabled”属性设置的值不同(也就是说,“Show Window”已启用,但它的“CanExec

我正在尝试实现和使用WPF“”项目

我已经在我的项目中安装了NuGet包,并逐字复制了一些文件,而其他文件则以我自己的方式实现。然而,我使用的是.NETCore3.0,而不是最初为其设计的.NetFramework。我确实认为它支持.NETCore3.0

问题是,当我右键单击托盘图标时,“显示命令”和“隐藏命令”菜单项未正确启用或禁用,即使我测试它们的“CanExecute”方法返回的值与它们的“enabled”属性设置的值不同(也就是说,“Show Window”已启用,但它的“CanExecute”方法返回
false

我的更改是使用以下“RelayCommand”类代替“DelegateCommand”:

使用系统;
使用System.Windows.Input;
命名空间用户接口
{
类中继命令:ICommand
{
行动目标执行方法;
Func-targetCanExecuteMethod;
公共事件事件处理程序CanExecuteChanged=委托{};
公共中继命令(操作执行方法、函数执行方法)
{
targetExecuteMethod=执行方法;
targetCanExecuteMethod=canExecuteMethod;
}
public void Execute(对象参数)
{
targetExecuteMethod?.Invoke();
}
公共布尔CanExecute(对象参数)
{
if(targetCanExecuteMethod!=null)
返回targetCanExecuteMethod();
if(targetExecuteMethod!=null)
返回true;
返回false;
}
public void raisecancecutechanged()
{
CanExecuteChanged(此为EventArgs.Empty);
}
}
}
同样,我的“NotifyIconViewModel”是:

使用System.Windows;
使用System.Windows.Input;
命名空间用户接口
{
/// 
///为NotifyIcon提供可绑定的属性和命令
///视图模型被分配给XAML中的NotifyIcon
///在App.xaml.cs中,可以创建此视图模型,并将其分配给NotifyIcon。
/// 
公共类NotifyIconViewModel
{
/// 
///如果尚未打开任何窗口,则显示一个窗口。
/// 
公共ICommand ShowWindowCommand
{
得到
{
返回新的DelegateCommand
{
CanExecuteFunc=()=>Application.Current.MainWindow==null,
CommandAction=()=>
{
Application.Current.MainWindow=新用户界面.MainWindow();
Application.Current.MainWindow.Show();
}
};
}
}
/// 
///隐藏主窗口。此命令仅在窗口打开时启用。
/// 
公共ICommand和隐藏Windows命令
{
得到
{
返回新的DelegateCommand
{
CommandAction=()=>Application.Current.MainWindow.Close(),
CanExecuteFunc=()=>Application.Current.MainWindow!=null
};
}
}
/// 
///关闭应用程序。
/// 
公共ICommand ExitApplicationCommand
{
得到
{
返回新的DelegateCommand{CommandAction=()=>Application.Current.Shutdown()};
}
}
}
}
其他所有内容都与项目页面中的内容一模一样。但可以肯定的是:

NotifyIconResources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:tb="http://www.hardcodet.net/taskbar"
                        xmlns:local="clr-namespace:UserInterface">

    <ContextMenu x:Shared="false" x:Key="SysTrayMenu">
        <MenuItem Header="Show Window" Command="{Binding ShowWindowCommand}"/>
        <MenuItem Header="Hide Window" Command="{Binding HideWindowCommand}"/>
        <Separator/>
        <MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}"/>
    </ContextMenu>

    <tb:TaskbarIcon x:Key="NotifyIcon"
                        IconSource="/Icons/Bulb.ico"
                        ToolTipText="{Binding ToolTipText}"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        ContextMenu="{StaticResource SysTrayMenu}">
        <tb:TaskbarIcon.DataContext>
            <local:NotifyIconViewModel/>
        </tb:TaskbarIcon.DataContext>
    </tb:TaskbarIcon>
</ResourceDictionary>

事件处理程序中的问题已被更改

我的什么都不做,项目最初使用的
DelegateCommand
执行以下操作:

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}
使用此
CanExecuteChanged
可以修复问题。 但我不能告诉你为什么会这样

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:tb="http://www.hardcodet.net/taskbar"
                        xmlns:local="clr-namespace:UserInterface">

    <ContextMenu x:Shared="false" x:Key="SysTrayMenu">
        <MenuItem Header="Show Window" Command="{Binding ShowWindowCommand}"/>
        <MenuItem Header="Hide Window" Command="{Binding HideWindowCommand}"/>
        <Separator/>
        <MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}"/>
    </ContextMenu>

    <tb:TaskbarIcon x:Key="NotifyIcon"
                        IconSource="/Icons/Bulb.ico"
                        ToolTipText="{Binding ToolTipText}"
                        DoubleClickCommand="{Binding ShowWindowCommand}"
                        ContextMenu="{StaticResource SysTrayMenu}">
        <tb:TaskbarIcon.DataContext>
            <local:NotifyIconViewModel/>
        </tb:TaskbarIcon.DataContext>
    </tb:TaskbarIcon>
</ResourceDictionary>
public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}