从MainWindow.xaml.cs调用ShowWindowCommand()

时间:2018-11-27 10:30:35

标签: c#

我想从MainWindow()中调用函数ShowWindowCommand()和HideWindowCommand()。

我有以下课程:

string line = "Rok rok irrelevant text irrelevant;text.irrelevant,text"; 
string NewLine = Regex.Replace(line, @"\b[rR]\w*", "");

通常,从NotifyIconRessource.xaml的上下文菜单中调用ShowWindowCommand()和HideWindowsCommand()

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您首先必须创建该类的公共属性,然后将其设置为视图的数据上下文:

public NotifyIconViewModel NotifyIconVM { get; set; }

MainWindow() 
{ 
    InitializeComponent();
    NotifyIconVM = new NotifyIconViewModel();
    DataContext = NotifyIconVM;
}

设置数据上下文将告诉视图在哪里查找命令。