看效果可以用来做成根据菜单切换下面的内容区域
项目结构
代码
UI层
MainWindow.xaml
<Window x:Class="PrismWpfApp.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PrismWpfApp"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="{Binding Title}" Height="450" Width="800">
<Grid>
<!--<TextBlock Text="{Binding Title}"/>-->
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Content="区域A" Width="80" Height="30" Command="{Binding ShowContentCommand}" CommandParameter="UserControlA" ></Button>
<Button Content="区域B" Width="80" Height="30" Command="{Binding ShowContentCommand}" CommandParameter="UserControlB" ></Button>
<Button Content="区域C" Width="80" Height="30" Command="{Binding ShowContentCommand}" CommandParameter="UserControlC" ></Button>
</StackPanel>
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegions"></ContentControl>
</Grid>
</Window>
UserControlA.xaml
<UserControl x:Class="PrismWpfApp.Views.UserControlA"
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"
xmlns:local="clr-namespace:PrismWpfApp.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="我是A" FontSize="50"/>
</Grid>
</UserControl>
B和C都是一样的内容
UiViewModelBase.cs
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace PrismWpfApp.ViewModels
{
/// <summary>
/// UI 层的 ViewModelBase 基类
/// </summary>
public abstract class UiViewModelBase : BindableBase
{
public UiViewModelBase()
{
AppDataInstance = AppData.Instance;
initCommand();
}
public AppData _appDataInstance;
/// <summary>
/// 公共数据
/// </summary>
public AppData AppDataInstance
{
get { return _appDataInstance; }
set
{
SetProperty(ref _appDataInstance, value);
}
}
private string _systemName = "一体针焊接系统";
/// <summary>
/// 系统名称
/// </summary>
public string SystemName
{
get { return _systemName; }
set
{
SetProperty(ref _systemName, value);
}
}
private string _title = "标题";
/// <summary>
/// 标题
/// </summary>
public string Title
{
get { return _title; }
set
{
SetProperty(ref _title, value);
}
}
private void initCommand()
{
CloseCommand = new DelegateCommand<Window>(close);
}
/// <summary>
/// 关闭(取消) 命令
/// </summary>
public DelegateCommand<Window> CloseCommand { get; set; }
/// <summary>
/// 关闭(取消) 方法
/// </summary>
public void close(Window window)
{
if (window != null)
{
window.Close();
}
}
/// <summary>
/// 根据名称获取开关的值
/// </summary>
/// <param name="name">名称</param>
/// <returns>是否开启</returns>
public bool GetOnOffByName(string name)
{
var isOn = false;
//if (!string.IsNullOrEmpty(name))
//{
// isOn = _serviceSysConfig.GetOnOffByName(name);
//}
return isOn;
}
/// <summary>
/// 保存开关配置
/// </summary>
/// <param name="connfigName">配置名称</param>
/// <param name="value">是否开启</param>
/// <returns>是否成功</returns>
public bool SaveOnOff(string connfigName, bool value)
{
var isOk = false;
if (!string.IsNullOrEmpty(connfigName))
{
//isOk = _serviceSysConfig.SaveModelByName(new SysConfigModel()
//{
// Name = connfigName,
// ValueStr = value.ToString(),
// UpdatedBy = "ADMIN",
// UpdatedOn = DateTime.Now
//});
}
return isOk;
}
}
}
MainWindowViewModel.cs
using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrismWpfApp.ViewModels
{
public class MainWindowViewModel : UiViewModelBase
{
/// <summary>
/// Prism 提供 区域管理接口
/// </summary>
private readonly IRegionManager _regionManager;
public MainWindowViewModel(IRegionManager regionManager)
{
this.Title = "XXX系统";
_regionManager = regionManager;
}
/// <summary>
/// 显示区域内容
/// </summary>
public DelegateCommand<string> ShowContentCommand
{
get
{
return new DelegateCommand<string>((arg) =>
{
//
_regionManager.Regions["ContentRegions"].RequestNavigate(arg);
});
}
}
}
}
App.xaml.cs
重点 注册 导航 RegisterForNavigation
using Prism.Ioc;
using Prism.Unity;
using PrismWpfApp.ViewModels;
using PrismWpfApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace PrismWpfApp
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : PrismApplication
{
/// <summary>
/// 提供主窗口的对象
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
protected override Window CreateShell()
{
//打开第一个窗口
return Container.Resolve<MainWindow>();
//return new MainWindow();
}
/// <summary>
/// 业务中所有需要注入的对象,在这个方法里注册
/// </summary>
/// <param name="containerRegistry"></param>
/// <exception cref="NotImplementedException"></exception>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册 导航 RegisterForNavigation
containerRegistry.RegisterForNavigation<UserControlA>();
containerRegistry.RegisterForNavigation<UserControlB>();
containerRegistry.RegisterForNavigation<UserControlC>();
}
//protected override void OnStartup(StartupEventArgs e)
//{
// base.OnStartup(e);
// if (new LoginWindow().ShowDialog() == true)
// {
// new MainWindow().ShowDialog();
// }
// Application.Current.Shutdown();
//}
}
}