views:

570

answers:

1

Hi,

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)'

Thanks

+1  A: 

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.

Gatmando