0

我正在尝试更新我的应用程序以使用 WPF 而不是 WinForms 并且有一个问题我需要一些指针,因为我无法弄清楚出了什么问题......

下面的代码来自我的 Winform 应用程序,它运行良好

private delegate void LoginAsyncCallbackDelegate(IAsyncResult result);
private void LoginAsyncCallback(IAsyncResult result)
{
    if (this.InvokeRequired)
    {
        try
        {
            LoginAsyncCallbackDelegate d = new LoginAsyncCallbackDelegate(LoginAsyncCallback);
            this.Invoke(d, new object[] { result });
        }
        catch (Exception ex)
        {
            AddErrorToList(ex.ToString());
        }
    }
    else
    {
        DRVis.upx.api.global.LoginResp resp = APIWrapper.UPGlobalService.Endlogin(result);

        var state = (CustomAsyncStateContainer)result.AsyncState;

        // Session manager
        if (resp != null && resp.header != null && resp.header.sessionToken != null)
            SessionTokenManager.ReturnSessionToken(resp.header.sessionToken);

        DisplayLogin(resp, state);
    }
}

以下是我为使其在 WPF 中工作而进行的更改,但如果 (!this.Dispatcher.CheckAccess())应用程序在线崩溃

private delegate void LoginAsyncCallbackDelegate(IAsyncResult result);
private void LoginAsyncCallback(IAsyncResult result)
{
    if (!this.Dispatcher.CheckAccess()) //Progam Crashes here!!
    {
        try
        {
            LoginAsyncCallbackDelegate d = new LoginAsyncCallbackDelegate(LoginAsyncCallback);
            this.Dispatcher.Invoke(DispatcherPriority.Normal, d, new object[] { result });
        }
        catch (Exception ex)
        {
            AddErrorToList(ex.ToString());
        }
    }
    else
    {
        DRVis.upx.api.global.LoginResp resp = APIWrapper.UPGlobalService.Endlogin(result);

        var state = (CustomAsyncStateContainer)result.AsyncState;

        // Session manager
        if (resp != null && resp.header != null && resp.header.sessionToken != null)
            SessionTokenManager.ReturnSessionToken(resp.header.sessionToken);

        DisplayLogin(resp, state);
    }
}

带有堆栈跟踪....

The error time: 16/08/2012 13:06
Exception: System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 

'System.IAsyncResult'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, 

CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, 

Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 

numArgs, Delegate catchHandler)

有人可以帮助我获得正确的语法来替换 WPF 中的 this.InvokeRequired 和 this.Invoke 或指出我缺少的明显内容吗?

谢谢哦

4

1 回答 1

0
this.Dispatcher.Invoke(DispatcherPriority.Normal, d, new object[] { result });

应该 :

this.Dispatcher.Invoke(DispatcherPriority.Normal, d, result);

?

于 2012-08-16T13:18:58.903 回答