0

I have a view and corresponding view model for adding or editing a customer.

If I click the Add new customer button, unity wires me up a nice shiny new CustomerView and passes in my datacontext and view model, and a customer object that are contructor parameters.

I save my customer and now want to view another, existing customer record, from my datagrid.

I have a command that accepts the selectedItem (Customer) from the datagrid and I now want to pass this in to my CustomerView as a parameter, in my Constructor for the View Model.

How do I tell Unity that I now want to build my Views view model with my Client parameter and then navigate to the view?

edit I register my views in CustomerModule and add a Customers launch button to navigation region like this:

public void Initialize()
{
    _container.RegisterType<ICustomerMainView, CustomerMainView>();
    _container.RegisterType<ICustomerMainViewViewModel, CustomerMainViewViewModel>();
    _container.RegisterType<ICustomerView, CustomerView>();
    _container.RegisterType<ICustomerViewViewModel, CustomerViewViewModel>();
    _container.RegisterType<ICustomerNavIconView, CustomerNavIconView>();
    _container.RegisterType<ICustomerNavIconViewModel, CustomerNavIconViewModel>();
    //var view = _container.Resolve<ICustomerMainView>();
    //var view1 = _container.Resolve<ICustomerView>();

    _regionManager.RegisterViewWithRegion("NavigationRegion", typeof(Views.CustomerNavIconView));
}

To add a new customer I have a command with the following:

private void OnAddNewCustomer()
{
    try
    {
        _regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.CustomerView));
        _regionManager.RequestNavigate("MainRegion", "CustomerView");
    }
    catch (Exception ex)
    {

    }
}

This is fine and I can add a new customer and save details. Now if I want to view the customer I have saved by double clicking the record in my grid I have a command as follows but don't know how to set the CustomerViewViewModel "customer" parameter to be my selectedItem(Customer)????

private void OnViewCustomer(Customer customer)
{
    try
    {
        _unityContainer.Resolve<ICustomerViewViewModel>(new ParameterOverride("customer", customer));
        _regionManager.RequestNavigate("MainRegion", "CustomerView");
    }
    catch (Exception ex)
    {

    }
}

Thanks.

CheGuevarasBeret
  • 1,364
  • 2
  • 14
  • 33

3 Answers3

0

I don't think Prism supports passing an object/class into to navigation request. You can, however, pass in string/id into the receiving method. Here's how you do that:

var query = new UriQuery();
query.Add("Customer", Customer.Id);
_regionManager.RequestNavigate("MainRegion",
         new Uri("CustomerView" + query.ToString(), UriKind.Relative));

Now, you can use this id and get your object from the database or wherever.

Big Daddy
  • 5,160
  • 5
  • 46
  • 76
0

So what I did was the following:

var clientView = _unityContainer.Resolve<IClientView>(new ParameterOverride("clientId", client.ClientId).OnType<ClientViewViewModel>());
                _regionManager.Regions["MainRegion"].Add(clientView);
                _regionManager.RequestNavigate("MainRegion", "ClientView");

Essentially:

  1. Had my ClientViewViewModel implement the IRegionMemberLifetime interface and have the KeepAlive property return False to ensure I create a new ViewModel every time I navigate to my view.
  2. Having unity resolve me a new ClientView overriding the "client" parameter for the ClientViewViewModel.
  3. Add my newly resolved view to the region I want to display in
  4. Navigate to my view
CheGuevarasBeret
  • 1,364
  • 2
  • 14
  • 33
0

Here are two links ,I think that're you want to know.

Prism/Unity Navigation question

ParameterOverride in WPF/PRISM/MVVM

huoxudong125
  • 1,966
  • 2
  • 26
  • 42