xiao
xiao

Reputation: 651

How to pass Data to different Views in MVVM Light?

I am kinda unclear on how to pass data to other views. I gathered there are 3 ways to do this but I not sure how to do 2 of them.

  1. Send data through messenger(not sure if you can send objects otherwise I can see thing getting pretty messy if you have to send like 10 pieces of data or something like that along).

  2. Somehow pass the data through the constructor of the new view model. I say "somehow" as I am unclear how to do that when using an IOC container(in this case the built in one).

  3. Passing the data through a parameter. I seen a couple of tutorials that show you how to do navigation for the windows phone but none really talk about parameter passing. I am guessing this would still be an option.

From what I heard is that Option 1 is sort of the old way of doing it. Option 2 seems to be a newer way and better way to do it but I cannot find any examples on people showing how to do it.

I don't know how to do it because the Ioc should be creating the instance of the view so how do you pass in data into it when that data does not exist?

Upvotes: 0

Views: 653

Answers (1)

Faster Solutions
Faster Solutions

Reputation: 7005

The two main ways I do this is:

1)Use the messenger:

Sender class:

public class TrafficLight
{
    public string Color{get;set;}
    public TimeSpand Duration{get;set;}
}
public class TrafficLightService
{
     public void SendLight(TrafficLight light)
     {
         Messenger.Default.Send(light);
     }
}

Receiver:

public class MyViewModel
{
     public MyViewModel()
     {
        Messenger.Default.Register<TrafficLight>(DoSomethingWithTrafficLight);
     }
     private void DoSomethingWithTrafficLight(TrafficLight light)
     {
     }
}

What happens here is that the source object is using the Messenger as an event broker. Object A doesn't need to know about object B, they just both need to know about the messenger.

2)

Just use dependency injection:

     public class TrafficLight
        {
            public string Color{get;set;}
            public TimeSpand Duration{get;set;}
        }
    public class LightEventArgs:EventArgs
    {
        public LightEventArgs(TrafficLight light)
        {
            _light=light;
        }
        public TrafficLight Light{get{return _light;}}
    }

        public interface ITrafficLightService
        {
            void SendLight(TrafficLight light);
            public event EventHandler<LightEventArgs> TrafficLightSet;
        }
        public class TrafficLightService
        {
             public void SendLight(TrafficLight light)
             {
                 Messenger.Default.Send(light);
             }
             public event EventHandler<LightEventArgs> TrafficLightSet;
        }

public class TrafficLightSenderViewModel
{
    public TrafficLightSenderViewModel(ITrafficLightService trafficLightService)
    {
        _trafficLightService=trafficLightService;
        _trafficLightService.Send(new TrafficLight{Color="Red"});
    }
}

public class TrafficLightReceiverViewModel
{
    public TrafficLightReceiverViewModel(ITrafficLightService trafficLightService)
    {
        _trafficLightService=trafficLightService;
        _trafficLightService.TrafficLightSet+= TrafficLightNotification;
    }

    private void TrafficLightNotification(TrafficLightEventArgs args)
    {
         DoSomethingWithTheLight(args.Light);
    }
}

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<ITrafficLightService, Design.TrafficLightService>();
        }
        else
        {
            SimpleIoc.Default.Register<ITrafficLightService, TrafficLightService>();         
        }

        SimpleIoc.Default.Register<TrafficLightSenderViewModel>();
        SimpleIoc.Default.Register<TrafficLightReceiverViewModel>();
    }

    public MainViewModel Sender
    {
        get { return SimpleIoc.Default.GetInstance<TrafficLightSenderViewModel>(); }
    }
    public MainViewModel Receiver
    {
        get { return SimpleIoc.Default.GetInstance<TrafficLightReceiverViewModel>(); }
    }
}

This is a bigger example and more complex.

Lets walk through this step by step:

  1. In MVVM Light we use the ViewModelLocator for 2 things: 1)To register all our viewmodels and services.
  2. Provide a way to allow the View to get a viewmodel in XAML

When we try to resolve a ViewModel

SimpleIoc.Default.GetInstance<TrafficLightReceiverViewModel>();

SimpleIoc looks at whether the viewmodel has any dependencies. In our case we do, we need an ITrafficLightService for both our viewmodels. What happens is that SimpleIoc sees if it can resolve that class and, in the process, checks to see if ITrafficLightService has any dependencies that need resolving as well. If SimpleIoc can resolve the chain of dependencies required to instantiate your viewmodel it does so and then hands back a fully built object.

Upvotes: 2

Related Questions