简体   繁体   English

如何防止命令在应用程序负载上运行

[英]How do I prevent my command to run on Application Load

So I have this simple application that has a button, that's it. 因此,我有一个具有按钮的简单应用程序,仅此而已。 And the button has a command property bound to a command. 并且按钮具有绑定到命令的command属性。

This is the command it is bound to. 这是它绑定的命令。

public class StartAsyncCommand : ICommand
    {
        private Task _execute;
        public StartAsyncCommand(Task Execute)
        {
            _execute = Execute;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _execute.Start();
        }

        public event EventHandler CanExecuteChanged;
    }

And in the ViewModel this is what I got. 而在ViewModel中,这就是我得到的。

public StartAsyncCommand StartCommand { get; }

        public MoveMouseModel()
        {
            StartCommand = new StartAsyncCommand(MoveMove());
        }

        public async Task MoveMove()
        {
            MessageBox.Show("First Message..");
            await Task.Delay(2000);
            MessageBox.Show("Second Message..");
        }

XAML XAML

<Grid>
    <Button Width="100"
            Height="25"
            Content="Async?"
            Command="{Binding MoveMouseModel.StartCommand}"/>
</Grid>

When I start the application, that those messageboxes show even though I didnt click the button. 当我启动应用程序时,即使我没有单击按钮,这些消息框也会显示。 What can I do to prevent this? 我该怎么做才能避免这种情况?

You are passing the result of MoveMove to your StartAsyncCommand class, not the actual method 你是路过的结果MoveMoveStartAsyncCommand类,而不是实际的方法

 public MoveMouseModel()
 {
      StartCommand = new StartAsyncCommand(MoveMove()); //<-- MoveMove() is executed
 }

Should work when you actually pass the method instead and call it inside of StartAsyncCommand instead 当您实际传递方法并在StartAsyncCommand内部调用它时,应该可以使用

eg. 例如。 public StartAsyncCommand(Func<Task> fnc) {...} as constructor and then just executing the func when the actual command is used public StartAsyncCommand(Func<Task> fnc) {...}作为构造函数,然后在使用实际命令时仅执行func

wondering why your visual studio is not displaying you some "this method is not awaited" info message though .. 想知道为什么您的视觉工作室没有向您显示一些“未等待此方法”的信息消息..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建应用程序域并在其中运行我的应用程序? - How do I create an application domain and run my application in it? 如何从 web 应用程序运行命令行进程? - How do I run a command line process from a web application? 如何在Windows窗体应用程序中运行PowerShell命令? - How do I Run PowerShell command in windows form application? 如何在命令行中注册要执行的应用程序? - How do I register my application to be executed in the command line? 如何强制我的 .NET 应用程序以管理员身份运行? - How do I force my .NET application to run as administrator? 我有一个dll形式的应用程序,我想在我的控制台应用程序中运行它。 我怎么做 - i have a form application as a dll and i want to run it in my console application. how do i do that 如何防止在我运行 WPF MVVM 应用程序时引发“System.StackOverFlowException”异常? - How to prevent the "System.StackOverFlowException" exception being thrown when i run my WPF MVVM application? 如何在页面加载asp.net中运行命令sql插入代码? - How do I run a command sql insert code in page load asp.net? 使用ClickOnce更新后,如何防止加载应用程序的两个实例? - How do I prevent two instances of my application from loading after updating using ClickOnce? 如何防止用户在我的 C# 应用程序中调整窗口大小? - How do I prevent a user from resizing a window in my C# application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM