The next line of code in the CM framework in .net wpf realizes multi-page management

  • I have done a wpf project practice before. The left side of the main page is a listbox, and the corresponding page will be displayed after each selection is changed. The interface diagram is as follows:

To achieve such a function, I used the traditional method to achieve it before. In this section, I use Conductor<T> under the CM framework to achieve it, so that the code amount can be greatly reduced, and the core code is only one line.

traditional way

  Background code:

① Define the collection and add data:

  public IViewModel ActiveWindowView { get; set; }
        public ObservableCollection<string> ListBoxItems { get; set; } 
        public string SelectedItem { get; set; }

ListBoxItems = new ObservableCollection<string>() { };
            ListBoxItems.Add("ShellView");
            ListBoxItems.Add("EventAggregatorView");
            ListBoxItems.Add("ConductorView");
            ListBoxItems.Add("MatchTemplateView");
            ListBoxItems.Add("IndicatorLightView");
            ListBoxItems.Add("MemorandumView");
            ListBoxItems.Add("FTPTestView");

②Switch the page after the listbox selection is changed:

public void ListBoxItems_SelectionChanged()
        {
           switch(SelectedItem)
            {
                case "ShellView":
                    ActiveWindowView = new ShellViewModel();break;
                case "EventAggregatorView":
                        ActiveWindowView = EventAggregatorViewModel.Instance; break;
                case "ConductorView":
                    ActiveWindowView = new ConductorViewModel(); break;
                case "MatchTemplateView":
                    ActiveWindowView = new MatchTemplateViewModel(); break;
                case "IndicatorLightView":
                    ActiveWindowView = new IndicatorLightViewModel(); break;
                case "MemorandumView":
                    ActiveWindowView = IoC.Get<MemorandumViewModel>(); break;
                case "FTPTestView":
                    ActiveWindowView = new FTPTestViewModel(new FTPConfig()); break;
                default:break;
            }
        }

③Foreground binding:

<ListBox Name="ListBoxItems" Grid.Column="0" 
   SelectedItem="{Binding SelectedItem}" Margin="2"
   cal:Message.Attach="[Event SelectionChanged] = [Action ListBoxItems_SelectionChanged]"/>
<ContentControl Name="ActiveWindowView"/>

Realize using Conductor<T> under CM framework

 ① Background code:

First of all, you need to inherit Conductor<IViewModel>.Collection.OneActive so that you can use the methods and properties under this class, and secondly, the constructor needs to add the receiving interface

IEnumerable<T>, the modified code is as follows:

 public MainWindowViewModel(IEnumerable<IViewModel> modules)
        {
            Items.AddRange(modules);
            ActivateItem(Items.FirstOrDefault(vm => vm.GetType() ==typeof(IndicatorLightViewModel)));
        }

If you don't consider the page activated for the first time, the core code is only one sentence:

 Items.AddRange(modules);

②Foreground code:​​​​​​​​

 <ListBox Name="Items" Grid.Column="0"  Margin="2" DisplayMemberPath="DisplayName"/>
  <ContentControl Name="ActiveItem"/>

In this way, the front and back are set up, and the multi-screen management class of a framework is inherited, which greatly simplifies the front and back codes, and the functions are not compromised. To be precise, it is more powerful. This is the advantage of the CM framework.

Guess you like

Origin blog.csdn.net/qq_41872328/article/details/126741338