2012-08-16 109 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);