2012-08-16 42 views
0

我試圖更新我的應用程序,而不是使用的WinForms WPF和有一個問題,我需要一些指點,我無法弄清楚什麼是錯....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或指出一些明顯的我失蹤?

感謝 Ø

回答

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

應該是:

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