开发者

Silverlight 4 accessing WCF Data Services: BeginInvoke frustrations

开发者 https://www.devze.com 2022-12-25 19:47 出处:网络
I\'m attempting to follow a pattern for performing WCF data service queries using the Silverlight 4 beta.The following is my code:

I'm attempting to follow a pattern for performing WCF data service queries using the Silverlight 4 beta. The following is my code:

    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);
    }

This is derived from a number of examples I've come across for WCF data services with silverlight. Unfortunately no开发者_高级运维 matter how I try to implement the code i end up with the following error on 'Dispatcher.BeginInvoke':

'An object reference is required for the non-static field, method, or property (System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)'


Well, I think I have the answer now. It appears that because I was instantiating the BeginInvoke from a class file and not from a UI file (such as a page) that the UI dispatcher was not being used (if that makes any sense). Using a lead from this article:

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

I used the propsed UIThread static class and assigned the RootVisual.Dispatcher to it. Now in my code instead of 'Dispatcher.BeginInvoke' I am using 'UIThread.Run'. And it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消