C# EventToCommand异步?
我正在编写一个WPF程序,其中我使用EventToCommand处理第三方控件中的事件,并将它们连接到ICommand。我的视图模型方法都是异步的,我遇到的问题是事件没有等待处理程序完成。这个问题在WPF中说明有点长,但我在下面的小控制台应用程序中对其进行了总结 我的一般问题是,是否存在允许我使用异步处理程序的EventToCommand实现?是的,我用谷歌搜索了一下,但什么也没找到。mvvm light文档没有提到异步 我愿意接受任何能完成工作的工作。请注意,在下面的代码中,我使用了名称“ButtonClick”,这可能会导致您询问我为什么不直接绑定到命令。。。。对于这个例子来说,这是一个糟糕的命名选择。在我的WPF应用程序中,我正在处理没有命令的事件C# EventToCommand异步?,c#,wpf,async-await,mvvm-light,C#,Wpf,Async Await,Mvvm Light,我正在编写一个WPF程序,其中我使用EventToCommand处理第三方控件中的事件,并将它们连接到ICommand。我的视图模型方法都是异步的,我遇到的问题是事件没有等待处理程序完成。这个问题在WPF中说明有点长,但我在下面的小控制台应用程序中对其进行了总结 我的一般问题是,是否存在允许我使用异步处理程序的EventToCommand实现?是的,我用谷歌搜索了一下,但什么也没找到。mvvm light文档没有提到异步 我愿意接受任何能完成工作的工作。请注意,在下面的代码中,我使用了名称“Bu
class Program
{
public event EventHandler ButtonClickEvent;
static void Main(string[] args)
{
Console.WriteLine("Program");
var p = new Program();
}
public Program()
{
ButtonClickEvent += ButtonClickEventHandler;
Console.WriteLine("Start fire event");
ButtonClickEvent(this, new EventArgs()); // In WPF app this event is handled by EventToCommand, which executes an ICommand
Console.WriteLine("End fire event");
Console.Write("Press any key...");
Console.ReadKey();
// Output
// Start fire event
// Start GetStringAsync
// End fire event <-- not waiting
// End GetStringAsync
}
// In WPF app this method is wired up to a DelegateCommand
public async void ButtonClickEventHandler(object sender, EventArgs e)
{
await DownloadStuffAsync();
}
public async Task DownloadStuffAsync()
{
Console.WriteLine("Start GetStringAsync");
string page = await new HttpClient().GetStringAsync("http://www.ibm.com");
Console.WriteLine("End GetStringAsync");
}
}
类程序
{
公共事件处理程序按钮ClickEvent;
静态void Main(字符串[]参数)
{
Console.WriteLine(“程序”);
var p=新程序();
}
公共计划()
{
ButtonClickEvent+=ButtonClickEventHandler;
控制台写入线(“启动火灾事件”);
ButtonClickEvent(this,new EventArgs());//在WPF应用程序中,此事件由执行ICommand的EventToCommand处理
控制台写入线(“结束火灾事件”);
控制台。写入(“按任意键…”);
Console.ReadKey();
//输出
//点火事件
//启动GetStringAsync
//End fire事件EventHandler不支持异步方法。您需要一些任务
返回方法作为解决方法,并等待调用事件的任务
您可以使用Stephen Cleary关于此主题的文章中解释的延迟。还有其他解决方案,如覆盖SynchronizationContext
,但使用上面文章中所示的方法更好。事件处理程序不支持异步方法。您需要一些任务
返回方法作为解决方法d并等待调用事件的任务
您可以使用Stephen Cleary关于此主题的文章中解释的延迟。还有其他解决方案,如覆盖SynchronizationContext
,但使用上面文章中所示的方法更好。事件处理程序不支持异步方法。您需要一些任务
返回方法作为解决方法d并等待调用事件的任务
您可以使用Stephen Cleary关于此主题的文章中解释的延迟。还有其他解决方案,如覆盖SynchronizationContext
,但使用上面文章中所示的方法更好。事件处理程序不支持异步方法。您需要一些任务
返回方法作为解决方法d并等待调用事件的任务
您可以使用Stephen Cleary在关于此主题的文章中解释的延迟。还有其他解决方案,如覆盖同步上下文
,但使用上面文章中所示的方法更好。我在WPF中的评论中尝试了Sriram给出的链接上的解决方案,它确实有效
我的视图模型
public class ViewModel
{
public ICommand DoSomethingCommand { get; set; }
public ViewModel()
{
DoSomethingCommand = new RelayCommand(DoSomething);
}
private async void DoSomething()
{
await DownloadStuffAsync();
Debug.WriteLine("Button Call Completed");
}
public async Task DownloadStuffAsync()
{
Debug.WriteLine("Start GetStringAsync");
await Task.Run(() =>
{
Debug.WriteLine("Thread sleeping");
Thread.Sleep(5000);
}).ConfigureAwait(false);
string page = await new HttpClient().GetStringAsync("http://www.ibm.com").ConfigureAwait(false);
Debug.WriteLine("End GetStringAsync");
}
}
我的看法
<StackPanel>
<Button Content="Do Something" Margin="0,2,2,2" Command="{Binding DoSomethingCommand}"/>
</StackPanel>
以及调试窗口上的输出
我在WPF的评论中尝试了Sriram提供的链接上的解决方案,它确实有效
我的视图模型
public class ViewModel
{
public ICommand DoSomethingCommand { get; set; }
public ViewModel()
{
DoSomethingCommand = new RelayCommand(DoSomething);
}
private async void DoSomething()
{
await DownloadStuffAsync();
Debug.WriteLine("Button Call Completed");
}
public async Task DownloadStuffAsync()
{
Debug.WriteLine("Start GetStringAsync");
await Task.Run(() =>
{
Debug.WriteLine("Thread sleeping");
Thread.Sleep(5000);
}).ConfigureAwait(false);
string page = await new HttpClient().GetStringAsync("http://www.ibm.com").ConfigureAwait(false);
Debug.WriteLine("End GetStringAsync");
}
}
我的看法
<StackPanel>
<Button Content="Do Something" Margin="0,2,2,2" Command="{Binding DoSomethingCommand}"/>
</StackPanel>
以及调试窗口上的输出
我在WPF的评论中尝试了Sriram提供的链接上的解决方案,它确实有效
我的视图模型
public class ViewModel
{
public ICommand DoSomethingCommand { get; set; }
public ViewModel()
{
DoSomethingCommand = new RelayCommand(DoSomething);
}
private async void DoSomething()
{
await DownloadStuffAsync();
Debug.WriteLine("Button Call Completed");
}
public async Task DownloadStuffAsync()
{
Debug.WriteLine("Start GetStringAsync");
await Task.Run(() =>
{
Debug.WriteLine("Thread sleeping");
Thread.Sleep(5000);
}).ConfigureAwait(false);
string page = await new HttpClient().GetStringAsync("http://www.ibm.com").ConfigureAwait(false);
Debug.WriteLine("End GetStringAsync");
}
}
我的看法
<StackPanel>
<Button Content="Do Something" Margin="0,2,2,2" Command="{Binding DoSomethingCommand}"/>
</StackPanel>
以及调试窗口上的输出
我在WPF的评论中尝试了Sriram提供的链接上的解决方案,它确实有效
我的视图模型
public class ViewModel
{
public ICommand DoSomethingCommand { get; set; }
public ViewModel()
{
DoSomethingCommand = new RelayCommand(DoSomething);
}
private async void DoSomething()
{
await DownloadStuffAsync();
Debug.WriteLine("Button Call Completed");
}
public async Task DownloadStuffAsync()
{
Debug.WriteLine("Start GetStringAsync");
await Task.Run(() =>
{
Debug.WriteLine("Thread sleeping");
Thread.Sleep(5000);
}).ConfigureAwait(false);
string page = await new HttpClient().GetStringAsync("http://www.ibm.com").ConfigureAwait(false);
Debug.WriteLine("End GetStringAsync");
}
}
我的看法
<StackPanel>
<Button Content="Do Something" Margin="0,2,2,2" Command="{Binding DoSomethingCommand}"/>
</StackPanel>
以及调试窗口上的输出
Sam事件只不过是一个触发器,它会通知发生了按钮单击或鼠标移动之类的事件。如果您想开始执行此操作,请执行此操作。但在此之后,请处理您的异步需求。您能否解释您需要的确切场景。以便我能提供更多帮助
谢谢
AnkitSam事件只不过是一个触发器,它会通知发生了诸如按钮单击或鼠标移动之类的事件。如果您想开始执行此操作,请执行此操作。但在此之后处理您的异步需求。您能否解释您需要的确切场景。以便我可以提供更多帮助
谢谢
AnkitSam事件只不过是一个触发器,它会通知发生了诸如按钮单击或鼠标移动之类的事件。如果您想开始执行此操作,请执行此操作。但在此之后处理您的异步需求。您能否解释您需要的确切场景。以便我可以提供更多帮助
谢谢
AnkitSam事件只不过是一个触发器,它会通知发生了诸如按钮单击或鼠标移动之类的事件。如果您想开始执行此操作,请执行此操作。但在此之后处理您的异步需求。您能否解释您需要的确切场景。以便我可以提供更多帮助
谢谢
安基特
我遇到的问题是,事件没有等待处理程序完成
我的一般问题是,是否存在允许我使用异步处理程序的EventToCommand实现
好吧,从第三方控件的角度来看问题。它引发了一个类似以下内容的事件:
void ControlEvent(object sender, ControlEventArgs args);
当该委托返回时,就完成了。事件处理程序签名返回void
,所有参数都是从控件传递到事件处理程序的数据。没有“返回通信”。委托无法通知第三方控件“实际上,我还没有完全完成;我只是回来释放线程,因为我感觉不舒服