寻找Prism示例,其中模块将自己加载到菜单中。

14

有没有人知道使用Prism的WPF代码示例,其中每个模块都在另一个模块中的菜单中注册自己作为菜单项?

(我目前有一个应用程序尝试使用EventAggregator完成此操作,因此一个模块侦听其他模块发布的事件,需要将其标题作为菜单项显示在菜单中,但我遇到了加载顺序和线程等问题。我想找到一个使用经典Prism结构来完成此操作的示例。)

我的想法是这样的:

Shell.xaml:

<DockPanel>
    <TextBlock Text="Menu:" DockPanel.Dock="Top"/>
    <Menu 
        Name="MenuRegion" 
        cal:RegionManager.RegionName="MenuRegion" 
        DockPanel.Dock="Top"/>
</DockPanel>

合同视图:

<UserControl x:Class="ContractModule.Views.AllContracts"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Contracts">
    </MenuItem>
</UserControl>

客户视角:

<UserControl x:Class="CustomerModule.Views.CustomerView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Customers">
    </MenuItem>
</UserControl>

迄今为止,我已经做了非Prism MVVM应用程序结构,并且菜单始终与ViewModel中的ObservableCollections很好地绑定,但上述方法似乎破坏了这种良好的模式。上述方法是在Prism中通常使用的吗?

2个回答

14

更新:

我为你创建了一个示例,它在这里:示例 (现在是失效链接)

它包含了一些你可能还没有考虑过的东西,例如一个允许你的模块控制你的 shell 的契约(这样你可以做像打开窗口之类的操作)。它是以 MVVM 为设计思路的,我不知道你是否使用过,但我会建议你考虑一下。

我尝试了几分钟来使标签页的标题正确,但最后只留下了 "A Tab"。如果你要采用带有选项卡的 UI,则需要自己解决这个问题。我将它设计成无外观的,因此你可以替换 Shell.xaml 中的 XAML 而不会影响任何内容。如果你使用得当,那么 RegionManager 是很有优势的。

祝你好运!


我从未见过这种情况的示例,但你需要自行实现。

你需要创建自己的接口,类似于这样:

public interface IMenuRegistry
{
     void RegisterViewWithMenu(string MenuItemTitle, System.Type viewType);
}

你的模块需要声明对 IMenuRegistry 的依赖并注册它们的视图。

你应该在托管 Bootstrapper 的同一项目中实现和注册 IMenuRegistry 接口,然后将菜单项添加到菜单或树形视图中(或者其他你用于菜单的组件中)。

当用户单击某个菜单项时,你需要使用 Bootstrapper.Container.Resolve(viewType) 方法创建一个视图实例,并将其放置在你想要显示它的任何占位符中。


你的CAGMenus示例看起来非常有趣,我会仔细研究一下。我通过创建一个LayoutManager来解决了这个问题,在其中每个模块都注册自己,然后在LayoutManager的Initialize()方法中,根据所有已注册的部分构建应用程序。 - Edward Tanguay
2
您的链接出现了404错误。 - Robert Harvey
2
@anderson - 链接已损坏,请分享一下代码好吗? - Rahul Jain

0
我正在使用MEF与prism 6.0和MVVM。
1.为Leafmenu创建Menuviewmodel类,为Toplevel menu创建TopLevel MenuViewmodel类。Menuviewmodel类将具有您想要绑定菜单的所有属性。实现此接口的Moduleui必须具有以下属性:[Export(typeof(IMenu))]。
  public class MenuViewModel:ViewModelBase
        {
            public String Name { get; private set; }
            public UIMenuOptions ParentMenu { get; private set; }
            private bool _IsToolTipEnabled;
            public bool IsToolTipEnabled
            {
                get
                {
                    return _IsToolTipEnabled;
                }
                set
                {
                    SetField(ref _IsToolTipEnabled, value);
                }
            }

            private String _ToolTipMessage;

            public String ToolTipMessage
            {
                get
                {
                    return _ToolTipMessage;
                }
                set
                {
                    SetField(ref _ToolTipMessage, value);
                }
            }
            private IExtensionView extensionView;
            public MenuViewModel(String name, UIMenuOptions parentmenu,
             bool isMenuCheckable = false, 
             IExtensionView extensionView    =null)
            {
                if(name.Contains('_'))
                {
                  name= name.Replace('_', ' ');
                }
                name = "_" + name;
                this.Name = name;
                this.ParentMenu = parentmenu;
                this.IsMenuCheckable = isMenuCheckable;
                this.extensionView = extensionView ;
            }

            private RelayCommand<object> _OpenMenuCommand;

            public ObservableCollection<MenuViewModel> MenuItems { get; set; }
            public ICommand OpenMenuCommand
            {
                get
                {
                    if(_OpenMenuCommand==null)
                    {
                        _OpenMenuCommand = new RelayCommand<object>((args =>  
                          OpenMenu(null)));
                    }
                    return _OpenMenuCommand;
                }
            }

            private void OpenMenu(object p)
            {

                if (extensionView != null)
                {
                    extensionView .Show();
                }
            }

            private bool _IsMenuEnabled=true;
            public bool IsMenuEnabled
            {
                get
                {

                    return _IsMenuEnabled;
                }
                set
                {
                    SetField(ref _IsMenuEnabled, value);
                }
            }

            public bool IsMenuCheckable
            {
                get;
                private set;
            }
            private bool _IsMenuChecked;
            public bool IsMenuChecked
            {
                get
                {
                    return _IsMenuChecked;
                }
                set
                {
                    SetField(ref _IsMenuChecked, value);
                }
             }
        }

         public class ToplevelMenuViewModel:ViewModelBase
         {
            public ObservableCollection<MenuViewModel> ChildMenuViewModels { 
              get; private set; } 
            public  String Header { get; private set; }
            public  ToplevelMenuViewModel(String header,         
             IEnumerable<MenuViewModel> childs)
            {

                this.Header ="_"+ header;
                this.ChildMenuViewModels =new 
                ObservableCollection<MenuViewModel>(childs);
            }
        }
    }

创建一个IMenu接口,它具有MenuViewModel属性。
    public interface IMenu
     {
         MenuViewModel ExtensionMenuViewModel
        {
            get;

        }

     }

3. 您需要在所有模块的ModuleUi中实现IMenu接口,以便将其加载到菜单中。
4. 实现MefBootstrapper 5. 重写Configure聚合目录方法 6. 向目录添加包含所有模块dll和IMenu接口dll的目录。代码如下:
protected override void ConfigureAggregateCatalog()
{
    base.ConfigureAggregateCatalog();
    AggregateCatalog.Catalogs.Add(new  
     AssemblyCatalog(typeof(Bootstrapper).Assembly));
       AggregateCatalog.Catalogs.Add(new 
      AssemblyCatalog(typeof(IMenu).Assembly));
     //create a directorycatalog with path of a directory conatining  
      //your module dlls                
    DirectoryCatalog dc = new DirectoryCatalog(@".\Extensions");
    AggregateCatalog.Catalogs.Add(dc);
}

7. 在你的主项目中添加对IMenu接口dll的引用。
8. 在mainwindow.xaml.cs类中声明一个属性。 public ObservableCollection ClientMenuViewModels { get; private set; }
声明一个私有字段。 private IEnumerable menuExtensions;
在你的主窗口或者 shell 构造函数中:
[ImportingConstructor] public MainWindow([ImportMany] IEnumerable menuExtensions) { this.menuExtensions = menuExtensions; this.DataContext=this;
} private void InitalizeMenuAndOwners() { if (ClientMenuViewModels == null) { ClientMenuViewModels = new ObservableCollection(); } else { ClientMenuViewModels.Clear(); }
if (menuExtensions != null) { var groupings = menuExtensions.Select(mnuext => mnuext.ClientMenuViewModel).GroupBy(mvvm => mvvm.ParentMenu);
foreach (IGrouping grouping in groupings) { UIMenuOptions parentMenuName = grouping.Key; ToplevelMenuViewModel parentMenuVM = new ToplevelMenuViewModel(parentMenuName.ToString(), grouping.Select(grp => { return (MenuViewModel)grp; })); ClientMenuViewModels.Add(parentMenuVM); } } }

}

在你的Shell.xaml或Mainwindow.xaml中定义一个菜单区域,并将itemssource属性绑定到ClientMenuViewModels。
       <Menu HorizontalAlignment="Left"
              Background="#FF0096D6"
              Foreground="{StaticResource menuItemForegroundBrush}"
              ItemsSource="{Binding ClientMenuViewModels}"
              TabIndex="3">
            <Menu.Resources>
                <Style x:Key="subMneuStyle" TargetType="{x:Type MenuItem}">
                    <Setter Property="Foreground" Value="#FF0096D6" />
                    <Setter Property="FontFamily" Value="HP Simplified" />
                    <Setter Property="FontSize" Value="12" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Command" Value="{Binding   
                     OpenMenuCommand}" />                 
                    <Setter Property="IsCheckable" Value="{Binding

                     IsMenuCheckable}" />
                    <Setter Property="IsChecked" Value="{Binding 
                         IsMenuChecked, Mode=TwoWay}" />
                    <Setter Property="IsEnabled" Value="{Binding 
                   IsMenuEnabled, Mode=TwoWay}" />
                    <Setter Property="ToolTip" Value="{Binding  
                ToolTipMessage, Mode=OneWay}" />
                    <Setter Property="ToolTipService.ShowOnDisabled" Value=" 
            {Binding IsToolTipEnabled, Mode=OneWay}" />
                    <Setter Property="ToolTipService.IsEnabled" Value="
            {Binding IsToolTipEnabled, Mode=OneWay}" />
                    <Setter Property="ToolTipService.ShowDuration" 
         Value="3000" />
                    <Setter Property="ToolTipService.InitialShowDelay" 
                Value="10" />
                </Style>

                <my:MyStyleSelector x:Key="styleSelector" ChildMenuStyle="   
                     {StaticResource subMneuStyle}" />
                <HierarchicalDataTemplate DataType="{x:Type 
                  plugins:ToplevelMenuViewModel}"
                 ItemContainerStyleSelector="{StaticResource styleSelector}"
                  ItemsSource="{Binding ChildMenuViewModels}">
                    <Label Margin="0,-5,0,0"
                           Content="{Binding Header}"
                           FontFamily="HP Simplified"
                           FontSize="12"
                    Foreground="{StaticResource menuItemForegroundBrush}" />
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type plugins:MenuViewModel}">
                    <Label VerticalContentAlignment="Center"
                           Content="{Binding Name}"
                           Foreground="#FF0096D6" />
                </DataTemplate>
            </Menu.Resources>
            <Menu.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Menu.ItemsPanel>
              </Menu>









 public class MyStyleSelector : StyleSelector
      {
        public Style ChildMenuStyle { get; set; }
        public Style TopLevelMenuItemStyle { get; set; }
        public override Style SelectStyle(object item, DependencyObject             
         container)                    
        {
            if (item is MenuViewModel)
            {
                return ChildMenuStyle;
            }
            //if(item is ToplevelMenuViewModel)
            //{
            //    return TopLevelMenuItemStyle;
            //}
            return null;

        }
    }

这里是ViewModelBase类。
public class ViewModelBase:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler =Volatile.Read(ref PropertyChanged);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        };
    }
    protected bool SetField<T>(ref T field, T value,[CallerMemberName] string propertyName="")
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

}

RelayCommand类如下:
 public class RelayCommand<T> : ICommand
    {
        #region Fields

        private readonly Action<T> _execute = null;
        private readonly Predicate<T> _canExecute = null;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }


        /// <summary>
        /// Creates a new command with conditional execution.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

        #endregion
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接