菜单管控模块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. NGUI创建Camera参数为Simple 2D的UI UI对象的结构UI Root(2D)

    NGUI创建Camera参数为Simple 2D的UI UI对象的结构UI Root(2D) 使用NGUI创建的Camera参数为Simple 2D的UI,会在游戏的场景中生成1个名为UI Root( ...

  2. Visual Studio 2013 EF5实体数据模型 EDMX 使用 T4模板生成后使用 ObjectContext对象

    Visual Studio 2013 EF5实体数据模型 EDMX 使用 T4模板生成后的继承对象为DbContext,以前的熟悉的ObjectContext对象不见了,当然使用ObjectConte ...

  3. winform学习1-----理解小概念-20160506

    panel属性,dock:获取或设置控件停靠到父容器的哪一个边缘. none,right,left,fill(完全填充),top C#默认窗体大小设置:maximumsize 窗体最大值 minimu ...

  4. CSS对浏览器的兼容性(IE和Firefox)技巧整理

    CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理技巧并整理了一下.对于web2.0的过度,请尽量用xhtml格 ...

  5. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  6. style在进行图形绘制前,要设置好绘图的样式

    是html5出现的新标签,像所有的dom对象一样它有自己本身的属性.方法和事件,其中就有绘图的方法,js能够调用它来进行绘图 ,最近在研读<html5与css3权威指南>下面对其中最好玩的 ...

  7. FZU 2028 BFS+vector

    一个普通的bfs 如果不看样例和input的解释... 四个0真是神样例 又被input误导 以为每个点都按顺序有标号 传送门的终点给的是一个点的标号 然后结果是什么呢?无尽的runtime erro ...

  8. mongodb 手动分片的命令汇总

    手动分片的操作 自动分片会带来性能的下降. 所以要合理使用手动分片. 并且配合Tag一起使用. # 对于4个shard的程序, 预先处理的指令1. 加入分片服务器sh.addShard( " ...

  9. Ubuntu/linux 安装 kernel-devel

    这个问题,需要安装与系统内核配套的开发包. 查看内核: uname -r 内核头文件C header files下载地址: http://rpmfind.net/linux/rpm2html/sear ...

  10. spring对dao层的支持(datasource的作用)

    本文大多数内容转自“http://www.cnblogs.com/liunanjava/p/4412408.html”感谢原作者 在做一个项目时,持久层并没有使用spring jpa和hibernat ...