菜单管控模块EntityFW

菜单的加载采用MEF技术,程序实现思路:

1 .主菜单加载页面MainMenuView.xaml指向MenuRegion

2. 菜单Item点击及内容加载,采用订阅模式,即菜单item点击时发布消息,shell负责订阅并过滤加载子模块

MainMenuView.xaml

<UserControl x:Class="EntityFW.Views.MainMenuView"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">

<Grid x:Name="LayoutRoot" Background="White">

<ItemsControl x:Name="MenuItems" BorderBrush="Black">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <RadioButton Command="{Binding ViewMenuCommand}" CommandParameter="{Binding}" GroupName="MenuItem"
                                     Style="{StaticResource RadioButtonStyle}" VerticalContentAlignment="Center" Height="35" Padding="15,0,0,0" Margin="2,1,2,1">
                            <StackPanel>
                                <TextBlock Text="{Binding NameFL}" FontSize="10" Foreground="Gray" />
                                <TextBlock Text="{Binding NameCH}"  FontSize="14" />
                            </StackPanel>
                        </RadioButton>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

后台代码实现

using EntityFW.ViewModels;
using MyGlobal.Infrustructure;
using MyGlobal.Infrustructure.Behaviors;
using MyGlobal.Infrustructure.Interfaces;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Controls;
namespace EntityFW.Views
{
   
    /// <summary>
    /// MainMenu.xaml 的交互逻辑
    /// </summary>
    [ViewExport(RegionName = RegionNames.MenuRegion)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class MainMenuView : UserControl,IPartImportsSatisfiedNotification
    {
        private readonly ObservableCollection<MainMenuViewModel> _MenuList =
            new ObservableCollection<MainMenuViewModel>();
        [Import]
        IRegionManager _regionManager;
        public MainMenuView()
        { 
            InitializeComponent();          
        }
       
        public void OnReceiveNewMenu(MainMenuViewModel menu)
        {
            _MenuList.Insert(0, menu);
        }

public void OnImportsSatisfied()
        {

//模块加载成功
            MainMenuViewModel mmvm = new MainMenuViewModel();

//初始化主菜单
            MenuItems.ItemsSource = mmvm.InitPopMenuListData();
        }
    }
}

MainMenuViewModel.cs

using EntityFW.Events;
using EntityFW.Models;
using Microsoft.Practices.ServiceLocation;
using MyGlobal.Infrustructure;
using MyGlobal.Infrustructure.Events;
using MyGlobal.Infrustructure.Interfaces;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace EntityFW.ViewModels
{
    [Export(typeof(MainMenuViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class MainMenuViewModel : MyBindableBase
    {

[ImportMany]
        IEnumerable<Lazy<IMainMenu, IMainMenuDepict>> _MenuList = null;
        LoginUser _User;
        public ICollection<MainMenuViewModel> InitPopMenuListData()
        {
            CompositeComponents();
            ICollection<MainMenuViewModel> list = new List<MainMenuViewModel>();
        
            //判断登录用户是否有权限
            foreach (var o in _MenuList.Where(item => (item.Metadata.Name != null || true)))
            {
                MainMenuViewModel mm = new MainMenuViewModel(o.Value.NameCH,o.Value.NameFL,o.Value.Url,o.Value.ParentUrl,o.Value.PowerKey);
              
                mm.ViewMenuCommand = new RelayCommand<MainMenuViewModel>(OnViewMainMenu);
                list.Add(mm);
            }
            return list;

}
        public void Initialize()
        {
           
        }
        void CompositeComponents()
        {

var assemblylog = new AssemblyCatalog(this.GetType().Assembly);

var aggregatelog = new AggregateCatalog();
           aggregatelog.Catalogs.Add(assemblylog);

aggregatelog.Catalogs.Add(new DirectoryCatalog("../../Modules"));          
            var container = new CompositionContainer(aggregatelog);
            container.ComposeParts(this);

//var assemblylog = new AssemblyCatalog(this.GetType().Assembly);
            ////AppDomain.CurrentDomain.BaseDirectory + "\\Parts", "*.dll"
            //var directoryCatalog = new DirectoryCatalog("../../Modules");
            ////var typeCatalog = new TypeCatalog(typeof(Class6), typeof(Class7));
            //var aggregateCatalog = new AggregateCatalog(assemblylog, directoryCatalog);

//var _container = new CompositionContainer(aggregateCatalog);

//var exports = _container.GetExports<object>();
            //string x = "";
            //foreach (var exportValue in exports)
            //{
            //    // x += exportValue.Value.GetType().ToString();
            //    Console.WriteLine(exportValue.Value.GetType());
            //}

}
        public string ModuleName
        {
            get { return "MainMenu"; }
        }
        public ICommand ViewMenuCommand { get; private set; }
     
        public MainMenuViewModel() : base("", "", "", "", 1) { }
        [ImportingConstructor]
        public MainMenuViewModel(string chName, string flName, string url, string parentUrl, int powerKey)
            : base(chName, flName, url, parentUrl, powerKey)
        {
            _User = new LoginUser();         
           
        }
   
        public void OnViewMainMenu(MainMenuViewModel obj)
        {

//发布消息
            EventAggregatorRepository.EventAggregator
                .GetEvent<ViewMainMenuEvent>()
                .Publish(obj);
            //string x = new Uri(obj.ViewUri, UriKind.Relative).AbsolutePath;

// _regionManager.RequestNavigate(RegionNames.MCWrapRegion, new Uri(obj.ViewUri, UriKind.Relative));
        }
        /****************************************/

string _MenuName;
        public string MenuName
        {
            get
            {
                return _MenuName;
            }
            set
            {
                base.SetProperty(ref _MenuName, value);
            }
        }

string _ViewUri;
        public string ViewUri
        {
            get
            {
                return _ViewUri;
            }
            set
            {
                base.SetProperty(ref _ViewUri, value);
            }
        }

}
}

Prism&MEF构建开发框架 (三)的更多相关文章

  1. Prism&MEF构建开发框架 (一)

    Shell框架XECA shell.xaml主要起到是一个容器或壳的作用 <Window x:Class="XECA.Shell"      xmlns="http ...

  2. Prism&MEF构建开发框架

    系统框架构想效果图 平台简单由左侧菜单和右侧内容区以及顶部系统和用户信息区构成 菜单根据系统模块动态加载 右侧,根据左侧选中菜单动态加载子模块,子模块集合以tab选项卡方式布局 系统模块划分为Shel ...

  3. 一步步实现 Prism + MEF(一)--- 搭建框架

    第一步:构建一个名为Bootstrapper的类作为引导程序. class Bootstrapper : MefBootstrapper { } 第二步:在MainWindow窗体中添加一个Coont ...

  4. Prism 文档 第三章 管理组件之间的依赖关系

                                                                          第3章:管理组件之间的依赖关系 基于Prism库的复合应用程 ...

  5. Xamarin+Prism开发详解三:Visual studio 2017 RC初体验

    Visual studio 2017 RC出来一段时间了,最近有时间就想安装试试,随带分享一下安装使用体验. 1,卸载visual studio 2015 虽然可以同时安装visual studio ...

  6. UWP应用程序使用Prism框架构建MVVM

    在我们创建的UWP解决方案中选择引用->管理NuGet包程序包 NuGet管理包 2. 搜索Prism.Core,并安装 搜索Prism.Core 3. 搜索Prism.Unity,并安装 搜索 ...

  7. Docker基本命令与使用 —— Dockerfile指令与构建(三)

    一.Dockerfile指令上 1.指令格式 # Comment 注释, 以#开头 INSTRUCTION argument 以大写的指令+参数 #First Dockerfile 注释 FROM u ...

  8. 一步步实现 Prism + MEF(二)--- 绑定命令

    Prism程序集为我们提供了DelegateCommand命令,使用该命令可实现窗口直接绑定.第一步:在ViewModel中定义一个DelegateCommand属性. public Delegate ...

  9. S3C6410嵌入式应用平台构建(三)

    构建了好久的系统,由于工作原因,没有及时写记录,目前我已经进展到构建yaffs2文件系统,启动Linux内核了.Uboot移植基本功能已经完成. 由于Uboot移植方法大致是一样的,我主要参考这位博友 ...

随机推荐

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. Open Xml SDK 引文

    什么是Open Xml SDK? 什么是Open Xml? 首先,我们得知道,Open Xml为何物? 我们还是给她起个名字——就叫 “开放Xml”,以方便我们中文的阅读习惯.之所以起开放这个名字,因 ...

  3. android 进程什么时候被销毁

    http://wear.techbrood.com/guide/components/processes-and-threads.html 每一个 android 应用默认会起一个进程,除非你用 an ...

  4. python 代码片段9

    #coding=utf-8 # 字符串指示符号 r表示raw u表示unicode mystring=u'this is unicode!--by' print mystring # 'raw'表示告 ...

  5. sqlserver linkserver

    --创建链接服务器exec sp_addlinkedserver    'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'exec sp_addlinkedsrvlogin ' ...

  6. TYVJ 1011 NOIP 2008&&NOIP 2000 传纸条&&方格取数 Label:多线程dp

    做题记录:2016-08-15 15:47:07 背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...

  7. BZOJ1224: [HNOI2002]彩票

    Description 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同.每次抽奖将抽出两个自然数X和 ...

  8. The Storage Situation: Removable Storage

    http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html There is a lot of co ...

  9. Java 读写图像

    Java中进行图像I/O(即读图片和写图片,不涉及到复杂图像处理)有三个方法:1. Java Image I/O API,支持常见图片,从Java 2 version 1.4.0开始就内置了.主页:h ...

  10. JAVA String.format 方法使用介绍

    1.对整数进行格式化:%[index$][标识][最小宽度]转换方式        我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...