上善若水

水善利万物而不争
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WPF 模块A、B、C 切换 (同项目)

Posted on 2022-08-06 13:15  董锡振  阅读(131)  评论(0编辑  收藏  举报

 

https://www.bilibili.com/video/BV1nY411a7T8?p=14

点击模块A、B、C 下方切换ABC对应的模块页面  

 

 

 

 

 

 

<prism:PrismApplication     x:Class="WPF01.App" 
        xmlns:prism="http://prismlibrary.com/"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPF01" >
    <Application.Resources> 
    </Application.Resources>
</prism:PrismApplication>

 

using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using System.Windows;
using WPF01.Views;

namespace WPF01
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
        }
    }
}

MainView.xaml

<Window x:Class="WPF01.Views.MainView"
        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:WPF01.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"  
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <WrapPanel>
            <Button Content="模块A" Margin="5"  Command="{Binding OpenCommand }" CommandParameter="ViewA" ></Button>
            <Button Content="模块B" Margin="5"  Command="{Binding OpenCommand }" CommandParameter="ViewB" ></Button>
            <Button Content="模块C" Margin="5"  Command="{Binding OpenCommand }" CommandParameter="ViewC" ></Button>
        </WrapPanel>
        <ContentControl Grid.Row="1"  Content="{Binding Body}" ></ContentControl>
    </Grid>
</Window>

 

Views:

创建用户控件页面UserControl  分别放置ABC三个

 

ViewModels下创建 MainViewModel  

using Prism.Commands;
using Prism.Mvvm;
using WPF01.Views;

namespace WPF01.ViewModels
{
    public class MainViewModel : BindableBase
    {
        /// <summary>
        /// 定义带参代理给前端调用
        /// </summary>
        public DelegateCommand<string> OpenCommand { get; private set; }
        public MainViewModel()
        {
            OpenCommand = new DelegateCommand<string>(Open);
        }

        private object body;
        public object Body { get { return body; } set { body = value; RaisePropertyChanged(); } }

        private void Open(string obj)
        {
            switch (obj)
            {
                case "ViewA": Body = new ViewA(); break;
                case "ViewB": Body = new ViewB(); break;
                case "ViewC": Body = new ViewC(); break;
            }
        }
    }
}