wait for web service to complete

Topics: Prism v2 - Silverlight 3
Oct 29, 2009 at 8:22 PM

Hi,

I'm a newbie in Prism and I'm in a fix here. Below is my ViewModel class.

public class DataViewModel : INotifyPropertyChanged
{
private readonly IDataService DataService;
private string time;

public DataViewModel(IDataService dataService)
{
    DataService = dataService;
    Time = DataService.GetTime();
}

public string Time
{
    get
    {
        return time;
    }
    set
    {
        time = value;
        RaisePropertyChanged("Time");
    }
}

private void RaisePropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
My view has its DataContext set to this model. The issue I'm having here is that the model is getting bound to the view before the web service (DataService) completes the async task of getting the 'Time' data. How to I resolve this?

Oct 30, 2009 at 8:22 PM

hi

Try to inject your web service (DataService) in your viewModel by using Unity Container.

This should work.

Let me know

Thanks

 

 

Blochaou Francois

Oct 31, 2009 at 12:06 AM

Can you please give code snippet for this?

I do have the DataService injected into the DataViewModel (as per the code above). I know you're saying something sensible, but I'm missing it. Below is my ViewModule class:

public class ViewModule : IModule 
{
    private readonly IUnityContainer Container;
    private readonly IRegionManager RegionManager;

    public ViewModule(IUnityContainer container, IRegionManager regionManager)
    {
        Container = container;
        RegionManager = regionManager;
    }

    #region IModule Members
    public void Initialize()
    {
        RegisterService();
        RegisterViews();
    }
    #endregion

    private void RegisterViews()
    {
        RegionManager.RegisterViewWithRegion("FirstRegion",
                                             typeof (TabViewOne));

        RegionManager.RegisterViewWithRegion("SecondRegion",
                                             typeof(TabViewTwo));

        RegionManager.RegisterViewWithRegion("ThirdRegion",
                                             typeof(TabViewThree));
    }

    private void RegisterService()
    {
        Container.RegisterInstance<IDataService>(new DataService());
    }
}

Thanks
Arun

Oct 31, 2009 at 3:36 AM

Add an event handler to your DataService class: 

event EventHandler<TimeEventArgs> TimeCompleted;

Then add the following in your view model:

this.DataService.TimeCompleted += this.DataService_OnTimeCompleted;

Oct 31, 2009 at 5:28 AM

Robb, I tried your solution, but in the viewmodel, I do not get the TimeCompleted through intellisense. I even declare the event as public, but still did not work.

Is there an test application or an article that describes the whole process?

Thanks
Arun

Oct 31, 2009 at 11:36 PM

    Is this how your DataService is setup?

    //Interface

    public interface IDataService
    {
        void GetTime();

        event EventHandler<TimeEventArgs> TimeCompleted;
    }

   

    //Class


    public class DataService : IDataService
    {
        public event EventHandler<TimeEventArgs> TimeCompleted;

        public void GetTime()
        {

          //Get Time Stuff

            this.TimeCompleted(this, args);
        }
    }

Nov 1, 2009 at 5:01 PM

Thanks to the both of you, I found the solution here: http://channel9.msdn.com/posts/akMSFT/Creating-a-modular-application-using-Prism-V2-Part-1-of-4--Creating-a-shell-and-modules/

I'm not sure how to assign points or mark your posts as answers, but if there's a way to do this, can the moderators please give credit for both blochaou and RobbSmith?