none
wpf 异步调用接口 DataGrid 数据控件数据源绑定问题, 非常郁闷..... RRS feed

  • 问题

  • 在WPF 应用程序中... 用异步委托的方式去调用接口函数, 返回的结果去 赋值给DataGrid 控件,   现在的问题是第一次取到得值赋给DataGrid ,这时候UI界面不呈现, 第二次取值在去赋给DataGrid 就显示了, 太郁闷了, 异步调用看不出来有问题啊,都是UI的线程来操作,附代码.....请高手指点下,等待中....
                
                Func<string, student> asyncAction = studentBLL.SingleEntity;//声明委托调用
                #region 回调函数
                Action<IAsyncResult> resultHandler = delegate(IAsyncResult asyncResult)
                {
    
                    try
                    {
                        student rsObj = asyncAction.EndInvoke(asyncResult);
                        if (asyncResult.IsCompleted)
                        {
                            if (rsObj != null)
                            {
                                //lock (_DataBindLock)
                                //{
                                var rs = rsObj.JsonObj;
    
                                 this.DataGrid.ItemsSource = rs ; //就是hi在这里赋值,第一次取值(数据也取到了)不显示,第二次就行,是不是异步调用这样写不行呀....
                                //}
                            }
                            //Thread.Sleep(500);
                            
                        }
    
                    }
                    catch (Exception ex)
                    {
                        
                        LogHelper.Error(typeof(ProcessDesign), "", ex);
                        
                        return;
                    }
    
    
                };
                #endregion
                AsyncCallback asyncActionCallback = delegate(IAsyncResult asyncResult)
                {
                    this.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, resultHandler, asyncResult);
                };
                asyncAction.BeginInvoke("123", asyncActionCallback, null);



    efforts..

    2012年12月3日 10:04

答案

  • 你试着刷新一下你的DataGrid

     private delegate void NoArgDelegate();
    
     public static void Refresh(DependencyObject obj)
    
     {
    
                obj.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,
    
                    (NoArgDelegate)delegate { });
    
     }
    
     private void Button_Click(object sender, RoutedEventArgs e)
    
     {
    
                 Refresh(testDtGrid);           
    
           
    
    }


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年12月4日 5:58
    版主

全部回复

  • 你试着刷新一下你的DataGrid

     private delegate void NoArgDelegate();
    
     public static void Refresh(DependencyObject obj)
    
     {
    
                obj.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,
    
                    (NoArgDelegate)delegate { });
    
     }
    
     private void Button_Click(object sender, RoutedEventArgs e)
    
     {
    
                 Refresh(testDtGrid);           
    
           
    
    }


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年12月4日 5:58
    版主
  • 非常感谢回复....原来是样式的问题....

    efforts..

    2012年12月6日 1:15