0

WinForms の代わりに WPF を使用するようにアプリを更新しようとしていますが、何が問題なのかがわからないため、いくつかのポインターが必要です....

以下のコードは、正常に動作する私の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)

this.InvokeRequired と this.Invoke を WPF で置き換えるための正しい構文を取得するのを手伝ってもらえますか、または私が欠けている明らかなものを指摘できますか?

ありがとうございます

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 に答える