WCF/WF - Error in pulling data from WCF every 30 seconds from a SL Application

Asked By Debashish Gupta on 14-Jul-10 08:25 AM

Hi am new in SL . I am trying to develop an MVVM application. But i am not sure why it is not able to pull the data.

I am trying to pull data every 30 seconds from WCF to my silverlight application.

Page.xmal.cs  ( View )
---------------
public partial class Page : UserControl
    {
        DispatcherTimer dt = new DispatcherTimer();
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            dt.Interval = new TimeSpan(0, 0, 0, 0, 15000);
            dt.Tick += new EventHandler(dt_Tick);
             dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
            MinerViewModel mdata = new MinerViewModel();
            mdata.FetchMiners();
            MinerDataView.DataContext = mdata.MinerList;
        }
    }

-----------------------
Calling the ViewModel Class

----------------------

public void FetchMiners()
        {
            Service1Client svc = new Service1Client();            
            svc.FetchMinersCompleted += new EventHandler<FetchMinersCompletedEventArgs>(svc_FetchMinersCompleted);
            svc.FetchMinersAsync();             
}


        void svc_FetchMinersCompleted(object sender, FetchMinersCompletedEventArgs e)
         {
             try
  {_MinerList = e.Result;   }
             catch
             {
    throw new NotImplementedException();    }
        }

------------------------

I can see it it calling the WCF to pull the Data , But not in correct order .
Some times it pull & when it assigns the _MinerList object
It then again calls
mdata.FetchMiners();

So not able to show the data pulled in the _MinerList in the XMAL Page.

Please suggest me , if i am doing wrong.

Regards
Debashish

Why do this? - Robbe Morris replied to Debashish Gupta on 14-Jul-10 09:40 PM

   MinerDataView.DataContext = mdata.MinerList;

 

    You are resetting the data context to the object mdata.MinerList

    that goes out of scope because mdata is a local variable.

    The WCF call is async, and I see it populate _MinerList but I don't

    see where you bind the results.  Ideally, you should bind the MinerDataView.DataContext

    to a class level object instead of a local variable.  Then, in your async callback, repopulate

    the class level object's collection.  If you use an ObservableCollection<T> as your collection, then Silverlight

    will automatically adjust your UI each time the data is loaded.

Debashish Gupta replied to Robbe Morris on 15-Jul-10 01:57 AM
Hi Robbe , I got the solution , i did not put the

OnPropertyChanged(

"MinerList");


in the

public


void svc_FetchMinersCompleted(object sender, FetchMinersCompletedEventArgs e)

{

try

{_MinerList = e.Result;

OnPropertyChanged(

"MinerList");}

catch{throw new NotImplementedException();}

}


Method.


But Not sure ... i am doinig any redundant Call .

But i have also changed what u have suggested

Now i am directly Binding the View Model Class to the dataContext.


//MinerDataView.DataContext = mdata.MinerList;



MinerDataView.DataContext = mdata;


And in the XMAL i changed to


<!-- <ItemsControl ItemsSource="{Binding}">-->

<


ItemsControl ItemsSource="{Binding Path=MinerList}">


<ItemsControl.ItemTemplate>


<DataTemplate>


<Canvas>


<Rectangle x:Name="{Binding Path=Name}" Canvas.Left="{Binding Path=XCordinate}"

Canvas.Top="{Binding Path=YCordinate}"

Height="10" Width="10" Stroke="Black" Fill="{Binding Path=Fill}"

ToolTipService.ToolTip="{Binding Path=ToolTip}"/>


</Canvas>


</DataTemplate>


</ItemsControl.ItemTemplate>


</ItemsControl>


Thanks for your Help.

Please Suggest me if i need to change anything if i am doing some redundant code.


Regards

Debashish.