菜单管控模块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);
}
}
}
}