Wpf Prism初体验

发布时间 2023-12-01 10:44:23作者: 天才卧龙

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

1、项目引入 Prism.DryIoc 

 2、规则说明

窗体必须放在Views文件夹下而且必须以View结尾,ViewModel必须放在ViewModels文件夹下面,文件必须以ViewModel结尾。

在prism框架下,可以不为窗体设定数据上下文,但,在窗体中必须显示声明:

        xmlns:Prism="http://prismlibrary.com/"  
        Prism:ViewModelLocator.AutoWireViewModel="True"

在App.xaml的cs文件中要显示声明继承自 PrismApplication

    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        { 
        }
    }

app.xaml 更改如下:

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

注:xaml中必须显示引入 xmlns:Prism ,且修改窗体标签为:<Prism:PrismApplication>   </Prism:PrismApplication>  

 xmlns:Prism="http://prismlibrary.com/" 

3、新建Views文件夹,并创建 MainView.xaml

<Window x:Class="WpfApp3.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:WpfApp3"
        xmlns:Prism="http://prismlibrary.com/"  
        Prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button Content="viewA" Margin="5" Height="35" Width="100" Command="{Binding BtnCommand}" CommandParameter="viewA"></Button>
            <Button Content="viewB" Margin="5" Height="35" Width="100" Command="{Binding BtnCommand}" CommandParameter="viewB"></Button>
            <Button Content="viewC" Margin="5" Height="35" Width="100" Command="{Binding BtnCommand}" CommandParameter="viewC"></Button>
        </StackPanel>

        <ContentControl Grid.Row="1" Content="{Binding Body}"/>
    </Grid>
</Window>
View Code

4、新建 ViewModels 文件夹,并新建viewModel如下

 public class MainViewModel : BindableBase
    {
        public DelegateCommand<string> BtnCommand { get;private set; }
       


        public MainViewModel()
        {
             BtnCommand = new DelegateCommand<string>(GoUrl);
             Body = new viewA();
        }

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

        private void GoUrl(string param)
        {
            switch (param)
            {
                case "viewA": Body = new viewA(); break;
                case "viewB": Body = new viewB(); break;
                case "viewC": Body = new viewC(); break;
                default: Body = new viewA(); break;
            }
        }
    }

说明:viewModel 继承自 BindableBase,属性双向绑定需设置: RaisePropertyChanged(); ,Command命令使用: DelegateCommand,带参数的使用  DelegateCommand<T>

5、新建 UserControls 文件夹,并创建用户控件 ViewA/ViewB/ViewC

以ViewA为例

<UserControl x:Class="WpfApp3.UserControls.viewA"
             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:WpfApp3.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="I am ViewA" FontSize="18"/>
    </Grid>
</UserControl>

5、效果如下:

 

@天才卧龙的博客