当前位置:网站首页>WPF prism framework

WPF prism framework

2022-06-25 10:21:00 @@Mr.Fu

Prism frame
1、 About Prism frame

​ Official address :http://prismlibrary.com

​ Official source code :https://github.com/PrismLibrary/Prism

​ edition :8.1

2、 Functional specifications

​ Prism Provides an implementation of a set of design patterns , It is helpful to write well structured maintainable XAML Applications .

​ Include MVVM Dependency injection command Event aggregator

​ Prism Weight Loss

​ Autofac 、Dryloc 、 Mef 、Niniject 、StruyctureMap、Unity.

3、Prism Key procedures

​ Prism.Core Realization MVVM Core functions , Belongs to a platform independent project .

​ Prism.Wpf Contains DialogService【 Popup 】 Region【 register 】 Module 【 modular 】 Navigation【 Navigation 】 Others WPF function .

​ Prism.Unity Prism.Unity.Wpf.dll Prism.Dryloc.Wpf.dll

4、 obtain Prism frame

​ Prism.dll Prism.core

​ Prism.Wpf.dll Prism.Wpf

​ Prism.Unity.Wpf.dll Prism.Unity

​ Prism.Dryloc.Wpf.dll Prism.Dryloc

5、 Data processing , Five ways of data notification

​ The new class :MainViewModel Inherit :BindableBase

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp2
{
    public class MainViewModel : BindableBase
    {
        private int _value = 100;
        public int Value
        {
            get { return _value; }
            set
            {
                // Five ways of notification 
                // The first way 
                //SetProperty(ref _value, value);  
                // The second way 
                //this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Value"));
                // The third way 
                //this.RaisePropertyChanged();
                // The fourth way    Value : Other properties can be notified   
                SetProperty(ref _value, value, "Value");
                // The fifth way 
                SetProperty(ref _value, value, OnPropertyChanged);
            }
        }
        public void OnPropertyChanged()
        {

        }
        public MainViewModel()
        {
        }
    }
}

xaml Code :

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

6、 Behavioral processing

​ 1、Prism The behavior of

​ DelegateCommand

​ Basic usage :

The new class :MainViewModel Inherit BindableBase

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
         public ICommand BtnCommand { get => new DelegateCommand(doExecute); } 
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        public MainViewModel() 
        {
             
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
      
    }
}

​ Check status

The first check state :

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
       
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value);
                 // Trigger the executable check logic related to the command  
                BtncheckCommand.RaiseCanExecuteChanged();
            }
        }
        public MainViewModel() 
        {
            BtncheckCommand = new DelegateCommand(doExecute,doCheckExecute);
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
        public bool doCheckExecute()
        {
            return this.Value == "Hellow  Word!";
        }
    }
}

The second check state :

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
       
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        public MainViewModel() 
        {
            //ObservesProperty :  Listen for changes in a property , When the value of this attribute changes , Do a status check 
            // When Value Take the initiative to trigger a status check when a change occurs 
            // You can listen to multiple properties    Back .ObservesProperty(()=>Value)
            BtncheckCommand = new DelegateCommand(doExecute, doCheckExecute).ObservesProperty(()=>Value); 
        }  Well 
        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
        public bool doCheckExecute()
        {
            return this.Value == "Hellow  Word!";
        }
    }
} 

The third way to check :

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
         public ICommand BtnCommand { get => new DelegateCommand(doExecute); }
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        private bool _state;

        public bool State
        {
            get { return Value == "Hellow  Word!"; }
            set {
                SetProperty(ref _state, value); 
            }
        }

        public MainViewModel() 
        { 
             // The third way to check    adopt   ObservesCanExecute   Make an attribute value observation , Conduct dynamic state processing 
             BtncheckCommand = new DelegateCommand(doExecute).ObservesCanExecute(() => State);
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
            this.State =!this.State;
        } 
    }
}

xaml Code :

<Window x:Class="WpfApp4.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:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
            <Button Content=" Basic commands " Height="30" Command="{Binding BtnCommand}"></Button>
            <Button Content=" Property check command " Height="30" Command="{Binding BtncheckCommand}"></Button>
        </StackPanel>
    </Grid>
</Window>
			

​ Asynchronous processing

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!"; 
        public ICommand BtnAsyncCommand { get => new DelegateCommand(doExecuteAsync); } 
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
      

        public MainViewModel() 
        {
       
        }

 
        public async void doExecuteAsync() 
        {
            await Task.Delay(5000);
            this.Value = " bye !";
        }
    }
}

xaml Code :

<Window x:Class="WpfApp4.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:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock> 
            <Button Content=" Asynchronous command " Height="30" Command="{Binding BtnAsyncCommand}"></Button>
        </StackPanel>
    </Grid>
</Window>

​ The generic parameter

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!"; 
        public ICommand BtnAsyncCommand { get => new DelegateCommand<string>(doExecuteAsync); }

         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value);
                // Trigger the executable check logic related to the command 
               // BtncheckCommand.RaiseCanExecuteChanged();
            }
        }
         
        public async void doExecuteAsync(string str) 
        {
            await Task.Delay(5000);
            this.Value = str;
        }
    }
}

xaml Code :

<Window x:Class="WpfApp4.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:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock> 
            <Button Content=" Asynchronous command " Height="30" Command="{Binding BtnAsyncCommand}" CommandParameter=" bye "></Button>
        </StackPanel>
    </Grid>
</Window>

​ Event command [ Event to order ] InvokeCommandAction

​ The new class :MainViewModel:BindableBase

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    { 
        public ICommand BtnEventCommand { get => new DelegateCommand<object>(doEventExecute); } 
        public void doEventExecute(object args) 
        {
        
        }
    }
}

xaml Code :

<Window x:Class="WpfApp4.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:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
            <Button Content=" Basic commands " Height="30" Command="{Binding BtnCommand}"></Button>
            <Button Content=" Property check command " Height="30" Command="{Binding BtncheckCommand}"></Button>
            <Button Content=" Asynchronous command " Height="30" Command="{Binding BtnAsyncCommand}" CommandParameter=" bye "></Button>

            <ComboBox  SelectedIndex="0">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <prism:InvokeCommandAction Command="{Binding BtnEventCommand}"
                                                   TriggerParameterPath=""></prism:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBoxItem Content="111"></ComboBoxItem>
                <ComboBoxItem Content="222"></ComboBoxItem>
                <ComboBoxItem Content="333"></ComboBoxItem>
                <ComboBoxItem Content="444"></ComboBoxItem>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window> 
 remarks :
 The top refers to two namespaces :
  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  xmlns:prism="http://prismlibrary.com/"
   Prism Frame reference initialization 

​ PrismBootstrapper starter

​ 1、 newly build Startup class , Inherit :PrismBootstrapper

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace WpfApp5
{
    public class Startup : PrismBootstrapper
    {  
        /// <summary>
        ///  Return to one Shell  : main window 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override DependencyObject CreateShell()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="containerRegistry"></param>
        /// <exception cref="NotImplementedException"></exception>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            throw new NotImplementedException();
        }
    }
}

2、 take App.xaml code annotation

<Application x:Class="WpfApp5.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp5">
             <!--StartupUri="MainWindow.xaml"    Comment out -->  
    <Application.Resources>
         
    </Application.Resources>
</Application>

3、 stay App.xaml.cs Start... In the constructor

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp5
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            new Startup().Run();
        }
    }
}

use PrismApplication starter

modify App.xaml Code

<prism:PrismApplication x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             xmlns:prism="http://prismlibrary.com/">
             <!--StartupUri="MainWindow.xaml"   Comment out -->
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
 remarks :  quote     xmlns:prism="http://prismlibrary.com/" 
        Change the head and tail labels to  :prism:PrismApplication 

App.xaml.cs Inherit PrismApplication, Code

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        ///  Return to one shell 【 window 】
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override Window CreateShell()
        {
             return Container.Resolve<MainWindow>();    
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }
    }
}

be based on Prism Login jump of framework

App.xaml Code :

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

App.xaml.cs Code :

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        ///  Return to one shell 【 window 】
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
        /// <summary>
        ///  initialization  shell   main window 
        /// </summary>
        /// <param name="shell"></param>
        protected override void InitializeShell(Window shell)
        {
            var loginWindow = Container.Resolve<Login>();
            if (loginWindow == null || loginWindow.ShowDialog() == false)
            {
                Application.Current.Shutdown();
            }
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
    }
}

​ Login code :Login.xaml

<Window x:Class="WpfApp1.Login"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Login" Height="450" Width="800">
    <Grid>
        <Button Click="Button_Click"></Button>
    </Grid>
</Window>

Login.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Login.xaml  Interaction logic of 
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}

MainWindow.xaml Code :

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

ViewModelLocator object To help carry out View And ViewModel The binding of

​ Standard status :

​ AutoWireViewModel, Default behavior true

​ ViewModel In the same assembly as the view type

​ ViewModel be located .ViewModel In the child namespace

​ View in .Views In the child namespace

​ ViewModel The name corresponds to the view name , With “ViewModel” ending

​ The example code is as follows :

​ New subfolder :ViewModels/MainWindowViewModel class , The code is as follows :

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp6.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _value = "  Hellow Word";

        public string Value
        {
            get { return _value; }
            set
            {
                SetProperty(ref _value, value);
            }
        }

    }
}

New subfolder ,Views/MainWindow.xaml, The code is as follows :

<Window x:Class="WpfApp6.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:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:WpfApp6.Views"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Text="{Binding Value}"></TextBlock>
    </Grid>
</Window>

Set boot entry :

App.xaml, The code is as follows :

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

App.xaml.cs, The code is as follows :

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp6
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
             return  Container.Resolve<Views.MainWindow>();   
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }
    }
}

The table of contents is as follows :【 The new folder must conform to Framework requirements 】
 Insert picture description here

原网站

版权声明
本文为[@@Mr.Fu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250915502151.html