Core 3 WPF MVVM data binding framework Prism Series

A. Installation Prism

 

1. Use Package Manager Console #

Install-Package Prism.Unity -Version 7.2.0.1367
You can also remove the '-Version 7.2.0.1367' to obtain the latest version

 2. Use the package management solutions Nuget #

 


In the above perhaps we have a question? Why install prism will have relations with Prism.Unity, we know that Unity is a IOC container, and Prism itself IOC support, and currently supports several official IOC container:

1. Microsoft and unity because it is official, and supports component-based prism, thus I recommend using prism.unity, prism7 does not support prism.Mef in official documents, Prism 7.1 will not support prism.Autofac
2. After installing prism.unity already contains all the prism of the core library, and architecture as follows:

II. Data binding

We create folders and ViewModels Views folder, MainWindow placed Views folder, then create the following folder in ViewModels MainWindowViewModel categories, as follows:

 

xmal code is as follows:
Copy the code
<Window x:Class="PrismSample.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:PrismSample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" prism:ViewModelLocator.AutoWireViewModel="True">
    <StackPanel>
        <TextBox Text="{Binding Text}" Margin="10" Height="100" FontSize="50" Foreground="Black" BorderBrush="Black"/>
        <Button  Height="100" Width="300" Content="Click Me" FontSize="50" Command="{Binding ClickCommnd}"/>
    </StackPanel>
</Window>
Copy the code

 

ViewModel code is as follows:
Copy the code
using Prism.Commands;
using Prism.Mvvm;

namespace PrismSample.ViewModels
{
   public class MainWindowViewModel:BindableBase
    {
        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetProperty(ref _text, value); }
        }

        private DelegateCommand _clickCommnd;
        public DelegateCommand ClickCommnd =>
            _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));

        void ExecuteClickCommnd()
        {
            this.Text = "Click Me!";
        }

        public MainWindowViewModel()
        {
            this.Text = "Hello Prism!";
        }
    }
}
Copy the code

 

starting program:

 
Click click Me button:


You can see, we have successfully used the prism data binding, and View and ViewModel perfect separation of the front and rear end

But now we also raises another question, when we do not want to follow the provisions of the prism will insist on the View and ViewModel and Views ViewModels inside, and perhaps your own project named rules vary from how to do, this time we should use In addition several ways:

1. Change the naming #


If the company naming rules are abnormal, leading to project structure becomes so (this company resigned forget):

First, we need to introduce prism in App, modify the 'Application' as 'prism: PrismApplication' and delete StartupUri
xmal code is as follows:
 
Copy the code
<prism:PrismApplication x:Class="PrismSample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:local="clr-namespace:PrismSample">
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
Copy the code
 
Background cs code is as follows:
Copy the code
using Prism.Unity;
using Prism.Ioc;
using Prism.Mvvm;
using System.Windows;
using PrismSample.Viewsb;
using System;
using System.Reflection;

namespace PrismSample
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        //设置启动起始页
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        //配置规则
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
            ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            {
                var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
                var viewModelName = $"{viewName}Test, {viewAssemblyName}";
                return Type.GetType(viewModelName);
            });
        }
    }
}
Copy the code

 

These two are the key above:

".Viewsb." View represents the folder where the namespace, ". ViewModelsa.OhMyGod." Indicate where the namespace ViewModel
1
var  viewName = viewType.FullName.Replace( ".Viewsb." ".ViewModelsa.OhMyGod." );

  

Test represents the suffix ViewModel
var viewModelName = $"{viewName}Test, {viewAssemblyName}";

 

2. Custom ViewModel registration #


As we create a custom category class Foo, as follows:
Copy the code
using Prism.Commands;
using Prism.Mvvm;

namespace PrismSample
{
   public class Foo:BindableBase
    {

        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetProperty(ref _text, value); }
        }

        public Foo()
        {
            this.Text = "Foo";
        }

        private DelegateCommand _clickCommnd;
        public DelegateCommand ClickCommnd =>
            _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));

        void ExecuteClickCommnd()
        {
            this.Text = "Oh My God!";
        }
    }
}
Copy the code

 


Modify App.cs Code:
Copy the code
protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
            ViewModelLocationProvider.Register<MainWindow, Foo>();
            //ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            //{
            //    var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
            //    var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
            //    var viewModelName = $"{viewName}Test, {viewAssemblyName}";
            //    return Type.GetType(viewModelName);
            //});
        }
Copy the code

 

 
run:
 
Click the button:
 

Even if the comment is not naming modify code, we find that the result is still the same run, so we can conclude,

This direct, not through reflection registered custom registration methods will be high priority in the official document also shows that in this way the efficiency will be high

And the official four ways, the other three are registered as follows:

ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), typeof(MainWindowTest)); 
ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), () => Container.Resolve<Foo>());
ViewModelLocationProvider.Register<MainWindow>(() => Container.Resolve<Foo>());

 

 
 

A. Installation Prism

 

1. Use Package Manager Console #

Install-Package Prism.Unity -Version 7.2.0.1367
You can also remove the '-Version 7.2.0.1367' to obtain the latest version

 2. Use the package management solutions Nuget #

 


In the above perhaps we have a question? Why install prism will have relations with Prism.Unity, we know that Unity is a IOC container, and Prism itself IOC support, and currently supports several official IOC container:

1. Microsoft and unity because it is official, and supports component-based prism, thus I recommend using prism.unity, prism7 does not support prism.Mef in official documents, Prism 7.1 will not support prism.Autofac
2. After installing prism.unity already contains all the prism of the core library, and architecture as follows:

II. Data binding

We create folders and ViewModels Views folder, MainWindow placed Views folder, then create the following folder in ViewModels MainWindowViewModel categories, as follows:

 

xmal code is as follows:
Copy the code
<Window x:Class="PrismSample.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:PrismSample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" prism:ViewModelLocator.AutoWireViewModel="True">
    <StackPanel>
        <TextBox Text="{Binding Text}" Margin="10" Height="100" FontSize="50" Foreground="Black" BorderBrush="Black"/>
        <Button  Height="100" Width="300" Content="Click Me" FontSize="50" Command="{Binding ClickCommnd}"/>
    </StackPanel>
</Window>
Copy the code

 

ViewModel code is as follows:
Copy the code
using Prism.Commands;
using Prism.Mvvm;

namespace PrismSample.ViewModels
{
   public class MainWindowViewModel:BindableBase
    {
        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetProperty(ref _text, value); }
        }

        private DelegateCommand _clickCommnd;
        public DelegateCommand ClickCommnd =>
            _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));

        void ExecuteClickCommnd()
        {
            this.Text = "Click Me!";
        }

        public MainWindowViewModel()
        {
            this.Text = "Hello Prism!";
        }
    }
}
Copy the code

 

starting program:

 
Click click Me button:


You can see, we have successfully used the prism data binding, and View and ViewModel perfect separation of the front and rear end

But now we also raises another question, when we do not want to follow the provisions of the prism will insist on the View and ViewModel and Views ViewModels inside, and perhaps your own project named rules vary from how to do, this time we should use In addition several ways:

1. Change the naming #


If the company naming rules are abnormal, leading to project structure becomes so (this company resigned forget):

First, we need to introduce prism in App, modify the 'Application' as 'prism: PrismApplication' and delete StartupUri
xmal code is as follows:
 
Copy the code
<prism:PrismApplication x:Class="PrismSample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:local="clr-namespace:PrismSample">
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
Copy the code
 
Background cs code is as follows:
Copy the code
using Prism.Unity;
using Prism.Ioc;
using Prism.Mvvm;
using System.Windows;
using PrismSample.Viewsb;
using System;
using System.Reflection;

namespace PrismSample
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        //设置启动起始页
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        //配置规则
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
            ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            {
                var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
                var viewModelName = $"{viewName}Test, {viewAssemblyName}";
                return Type.GetType(viewModelName);
            });
        }
    }
}
Copy the code

 

These two are the key above:

".Viewsb." View represents the folder where the namespace, ". ViewModelsa.OhMyGod." Indicate where the namespace ViewModel
1
var  viewName = viewType.FullName.Replace( ".Viewsb." ".ViewModelsa.OhMyGod." );

  

Test represents the suffix ViewModel
var viewModelName = $"{viewName}Test, {viewAssemblyName}";

 

2. Custom ViewModel registration #


As we create a custom category class Foo, as follows:
Copy the code
using Prism.Commands;
using Prism.Mvvm;

namespace PrismSample
{
   public class Foo:BindableBase
    {

        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetProperty(ref _text, value); }
        }

        public Foo()
        {
            this.Text = "Foo";
        }

        private DelegateCommand _clickCommnd;
        public DelegateCommand ClickCommnd =>
            _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));

        void ExecuteClickCommnd()
        {
            this.Text = "Oh My God!";
        }
    }
}
Copy the code

 


Modify App.cs Code:
Copy the code
protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
            ViewModelLocationProvider.Register<MainWindow, Foo>();
            //ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            //{
            //    var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
            //    var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
            //    var viewModelName = $"{viewName}Test, {viewAssemblyName}";
            //    return Type.GetType(viewModelName);
            //});
        }
Copy the code

 

 
run:
 
Click the button:
 

Even if the comment is not naming modify code, we find that the result is still the same run, so we can conclude,

This direct, not through reflection registered custom registration methods will be high priority in the official document also shows that in this way the efficiency will be high

And the official four ways, the other three are registered as follows:

ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), typeof(MainWindowTest)); 
ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), () => Container.Resolve<Foo>());
ViewModelLocationProvider.Register<MainWindow>(() => Container.Resolve<Foo>());

 

 

Guess you like

Origin www.cnblogs.com/cider/p/11929221.html