[英]Trouble with Async Task and RoutedEventHandler in WPF/C#
我有一個帶有 Button 的用戶控件,它從帶有RoutedEventHandler
的窗口中使用:
用戶控件:
public event RoutedEventHandler IniciarPLC_Click;
private void BtnIniciarPLC_Click(object sender, RoutedEventArgs e)
{
.....
if (IniciarPLC_Click != null)
{
IniciarPLC_Click(this, new RoutedEventArgs());
}
......
}
窗戶:
XAML:
<ucUserControl x:Name="cntBarraHerramientas" IniciarPLC_Click="CntBarraHerramientas_IniciarPLC_Click"/>
C#:
private void CntBarraHerramientas_IniciarPLC_Click(object sender, RoutedEventArgs e)
{
....
}
但是我需要調用CntBarraHerramientas_IniciarPLC_Click
的async
方法,所以我通過async Task
更改了void
返回類型並使用await
調用該方法:
private async Task CntBarraHerramientas_IniciarPLC_Click(object sender, RoutedEventArgs e)
{
await AsyncMethod(...);
}
我有這個錯誤:
Could not create a 'IniciarPLC_Click' from the text 'CntBarraHerramientas_IniciarPLC_Click'. '
ArgumentException: You cannot link to the target method because its security transparency or signature is not compatible with that of the delegated type.
問題是如何使用RoutedEventHandler
調用async
方法? 因為從按鈕單擊事件調用的async
方法有效。
事件處理程序應該是async void
。 這是少數幾個可以使用async void
而不是async Task
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.