canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

Activate item with params #159

Closed boop5 closed 3 years ago

boop5 commented 3 years ago

It looks like that IConductor<T> does not provide a way to activate items with some kind of parameters.

I want to have some kind of NavigationService which works like this

public void NavigateTo(Type t, IDictionary<string, object> @params) 
{
    var vm = _container.Get(t);
    var msg = new OpenPageEvent(vm, @params);
    _eventAggregator.Publish(msg);
}

and my conductor would do something like this

public void Handle(OpenPageEvent msg, IDictionary<string, object> @params) 
{
    ActivateItem(msg, @params)
}

Obviously I could create ViewModels manually and pass my parameters like that. But I'd like to do it as I explained above. Is there a way I can do that with Stylet?

canton7 commented 3 years ago

I don't follow I'm afraid. What would ActivateItem do with the params? It also looks like you're trying to activate the event, rather than the ViewModel?

When I do things like this, I have a navigation service, which defines a bunch of different methods which each take a set of strongly-typed parameters as necessary. So you'd call NavigateToLogin(param1, param2) rather than NavigateTo(typeof(Login ViewModel), new Dictionary<string, object>() { { "param1", param1}, { "param2", param2 } }). The navigation service would then instantiate the VM and pass in the parameters as constructor parameters. This had the advantage of not relying on weakly-typed dictionaries which only catch errors at runtime, and also means the caller doesn't need to know the type of the final ViewModel.

boop5 commented 3 years ago

Thanks for your input. You helped me a lot.