简体   繁体   中英

WPF Prsim MVVM application , multiple views , Mess with commands and Events

please advise on following problem. I am pretty new to WPF , Prism , and MVVM.

We've started to plan A dirty scratch version of Our old application , written on C++ , WPF port.

We currently have following UI Structure of the application , Main Window , Has A Menu and A toolbar with (Save , Add , Edit etc commands) , It has also MainRegion where we are loading different Views binded to their ViewModels using prism AutoWire vm feature (Navigation)

Now the problem is , that all views should accept the Toolbar commands for Save Create Edit. And Each command should be executed only in Current View.

Two problems appear now :

  1. Binding CanExecute for each of the Commands for each Active View,

  2. Performing different actions on Save on each View.

Lets say we have following MainWindow:

MainWindow
    - Menu
    - ToolBar
    - ContentControl (Region Manager)

Lets say user selected some Menu Item , that caused Navigation to lest say settings.xaml

At this moment Settings arent changed , so save btn on toolbar should be disabled. Then we are changing some settings , ViewModel isDirty property changed , how to send this info to the button state ?

Second problem , Save command executed on MainWindow ViewModel , not on Settings ViewModel , Currently we are using PubSubEvents and each viewModel subscribes to SaveEvent , :

   if (viewName.Contains("Settings"))
        {
            this.Settings = _settingsService.Save();
        }

where active view name is sent via _eventAggregator.GetEvent().Publish(ActiveView);

Can someone point us to proper way of this kind of UI implementation ? Thanks

You want to use CompositeCommands for your high level commands. Make sure when you define the composit ecommand to register it to monitor the IsActiveAware. Then each of your VIew's VM's should implement the IActiveAware interface. In your implementation, when the IsActive value changes, update the IsActive property of the DelegateCommands. Also, make sure you register your VM commands with the CompsoiteCommands. Now it will work exactly as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM