Frage

i have this situation. In the ShellView i have two datapicker:

<DatePicker Grid.Column="0" Margin="8" Padding="5" x:Name="from"/>
<DatePicker Grid.Column="1" Margin="8" Padding="5" x:Name="to"/>

and a button

<Button Grid.Row="2" cal:Message.Attach="[Action RunOtherView($dataContext)]"  Content="Go" Margin="8" HorizontalAlignment="Center"/>

I would like that when i choose the two dates, and after press the button, the two dates will be passed in the SecondViewModel for example in my case in List Parameters. But in the constructor of SecondViewModel the "Parameters" is always null. Where is the error? How can I pass these parameters from one screen to another? Thanks

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
{
    protected override void OnActivate()
    {
        base.OnActivate();
        ActivateItem(IoC.Get<IMainViewModel>());
    }

    public void RunOtherView()
    {
        var crvm = IoC.Get<ISecondViewModel>();
        crvm.Parameters = new List<DateTime>();
        crvm.Parameters.Add((DateTime)d.from);
        crvm.Parameters.Add((DateTime)d.to);
        ActivateItem(crvm);                                    
    }
}



public class MainViewModel : Screen, IMainViewModel
{
    public DateTime? from { get; set; }
    public DateTime? to { get; set; }
}



public class SecondViewModel : Screen, ISecondViewModel
{  
        public List<DateTime> Parameters { get; set; }

        protected override void OnActivate()
        {
            base.OnActivate();
        }

        public SecondViewModel()
        {
            //Parameters is always null
            PersonalList = CollectionViewSource.GetDefaultView(Db.UpdateInformation(Parameters));
        }


        public override void TryClose(bool? dialogResult)
        {
            TryClose(dialogResult);
        }
}
War es hilfreich?

Lösung

ViewModels can be comunicated through the EventAggregator. You need to create the events and tell at the listening viewmodels the events that they expect. I usually make a "Events" folder to put it.

Take a look at the Documentation.

Example:

The event:

    public class FooEvent
        {
           public FooEvent(bool foo) 
           {
               Foo = foo;
           }

         public bool Foo { get; private set; }
        }

The first screen

public class FirstViewModel : Screen
{
    private readonly IEventAggregator _events;

    [ImportingConstructor]
    public FirstViewModel(IEventAggregator events)
    {

       DisplayName = "First screen";
       _events = events;
    }


   public void PublishFooEvent()
   {
       _events.Publish(new FooEvent(true));
   }

The second screen

public class SecondViewModel : Screen, IHandle<FooEvent>
    {

    private readonly IEventAggregator events;

    [ImportingConstructor]
    public SecondViewModel(IEventAggregator events)
    {
        DisplayName = "Second screen";
        this.events = events;
        events.Subscribe(this);
    }

    public bool Bar{get;set;}

    public void Handle(FooEvent message)
    {
        Bar = message.Foo;
    }
}

Hope it helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top