簡體   English   中英

Silverlight 4訪問WCF數據服務:BeginInvoke失敗

[英]Silverlight 4 accessing WCF Data Services: BeginInvoke frustrations

我正在嘗試遵循一種使用Silverlight 4 Beta執行WCF數據服務查詢的模式。 以下是我的代碼:

    public CodeTables()
    {
        CodeCountries = new ObservableCollection<dsRealHomes.CodeCountries>();

        dsRealHomes.RealHomesEntities myClient = null;
        myClient = staticGlobals.RealHomesContext();

        object userState = null;


        myClient.BeginExecute<dsRealHomes.CodeCountries>(new Uri("CodeCountries"),
        (IAsyncResult asyncResult) =>
        {
            Dispatcher.BeginInvoke(
                 () =>
                 {
                     var test = myClient.EndExecute<dsRealHomes.CodeCountries>asyncResult).ToList();
                 }
             );
        }, userState);
    }

這源自我在Silverlight上為WCF數據服務所遇到的許多示例。 不幸的是,無論我如何嘗試實現代碼,最終在“ Dispatcher.BeginInvoke”上都會出現以下錯誤:

'非靜態字段,方法或屬性(System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)的對象引用是必需的”

好吧,我想我現在有答案。 似乎是因為我從類文件而不是從UI文件(例如頁面)實例化BeginInvoke,所以未使用UI分派器(如果有任何意義)。 使用本文的線索:

http://gen5.info/q/2008/06/25/getting-back-to-the-ui-thread-in-silverlight-2/

我使用了受支持的UIThread靜態類,並為其分配了RootVisual.Dispatcher。 現在在我的代碼中而不是'Dispatcher.BeginInvoke'中,我正在使用'UIThread.Run'。 而且有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM