[Summary] Code precipitation finishing - dapper framework MV * mode - Caliburn.Micro

 

 

Caliburn.Micro - Xaml made easy. 

web: https://github.com/Caliburn-Micro/Caliburn.Micro
document: http://caliburnmicro.com/documentation/
nuget: Install-Package Caliburn.Micro

XAML platforms used to build various applications such as WPF, Silverlight, WP, support MV * mode


 More related projects, please visit the code precipitation directory

Important: Please note that the version of the project, the latest official version shall prevail.


 Before long blog post, I generally translated a series of articles, but due to the CM framework update too fast, plus a main reason that he is too slow translation of the article, a word search, so only translated a more than half, but enough people use. This article describes a version of the V2 and V3 will be a little new number. But the new version and the old version of the main uses almost see the official demonstration below.

Basic Configuration, Actions and Conventions

1. Start Visual Studio, Create a Solution "Caliburn.Micro.Hello", using nuget mode (recommended) or manual reference dll way to add class libraries and System.Windows.Interactivity.dll CM frame.
Delete "MainPage.xaml", modify "App.xaml.cs", like the one below:

Copy the code
namespace Caliburn.Micro.Hello {
    using System.Windows;

    public partial class App : Application {
        public App() {
            InitializeComponent();
        }
    }
}
Copy the code

2. CM framework also recommend using View-Model-First way, we have to do this, first create a VM, called "ShellViewModel", the code is as follows:

Copy the code
namespace Caliburn.Micro.Hello {
    using System.Windows;

    public class ShellViewModel : PropertyChangedBase {
        string name;

        public string Name {
            get { return name; }
            set {
                name = value;
                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSayHello);
            }
        }

        public bool CanSayHello {
            get { return !string.IsNullOrWhiteSpace(Name); }
        }

        public void SayHello() {
            MessageBox.Show(string.Format("Hello {0}!", Name)); //Don't do this in real life :)
        }
    }
}
Copy the code

You can see, ShellViewModel inherited from PropertyChangedBase, simple base class that defines the properties of notification, the notification interface enables simple update function.

3. With a simple VM, Here we define what bootstrapper, create a new class of "HelloBootstrapper", the code is as follows:

Copy the code
namespace Caliburn.Micro.Hello {
    public class HelloBootstrapper : BootstrapperBase {
        public HelloBootstrapper() {
            Initialize();
        }

        protected override void OnStartup(object sender, StartupEventArgs e) {
            DisplayRootViewFor<ShellViewModel>();
        }
    }
}
Copy the code

This Bootsrapper by calling a method that defines a "top-ViewModel". When the program starts, CM framework at initialization will load the top VM, and VM display the corresponding View.
In some common mvvm Bootsrapper frame, it can be seen as an inlet configured, such as configuration settings, configuration into the container, and the like.

4. Next, we replace the default startup procedures HelloBootstrapper, modifications "App.xaml", as follows:

Copy the code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Hello"
             x:Class="Caliburn.Micro.Hello.App">
    <Application.Resources>
        <local:HelloBootstrapper x:Key="bootstrapper" />
    </Application.Resources>
</Application>
Copy the code

 WPF:

Copy the code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Hello"
             x:Class="Caliburn.Micro.Hello.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:HelloBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Copy the code

 Here, we have added a resource for the App, resource named "bootstrapper", need to be reminded, here has been deleted value "Application.StartupUri" attribute, the form does not need to start by default, instead by CM frame the bootstrapper way to start the "top ViewModel" configured earlier.

Next, you can try to start your program, you will see the interface has the following words. 


Caliburn.Micro.Hello.ShellView not found.

Caliburn.Micro has established a ShellViewModel, but do not know how to show it, so we need a corresponding View, then we create a user control (User Control), called "ShellView", xaml code is as follows: 

Copy the code
<UserControl x:Class="Caliburn.Micro.Hello.ShellView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox x:Name="Name" />
        <Button x:Name="SayHello"
                Content="Click Me" />
    </StackPanel>
</UserControl>
Copy the code

Start your program again, this time you can see just defined the UI interface.

Try to enter text in the TextBox, when Button is available, click on it, you can see a pop-up message window.

CM framework defines a set of simple and effective "naming convention" function is used to match the link between View and ViewModel.
In fact, it is to get to the full name of the specified VM, and remove the "Model" section, the rest is a corresponding View.
For example, for "MyApp.ViewModels.MyViewModel", you will get "MyApp.Views.MyView".

Compare View and ViewModel, can be found, there is a TextBox control View, [x: Name = "Name"] section, is bound to implement the ViewModel in the "Name" property. Also found that, View has a Button control, [x: Name = "SayHello"] section, is bound to implement the ViewModel in the "SayHello" method. Finally, the availability of methods ViewModel There is also a "CanSayHello" property, it will be linked to the "SayHello", such as binding function so that the corresponding button is unavailable.

These features are the VM ActionMessage (bound to method) function and Conventions (agreement) reflects the function.

 

Caliburn.Micro - Xaml made easy. 

web: https://github.com/Caliburn-Micro/Caliburn.Micro
document: http://caliburnmicro.com/documentation/
nuget: Install-Package Caliburn.Micro

XAML platforms used to build various applications such as WPF, Silverlight, WP, support MV * mode


 More related projects, please visit the code precipitation directory

Important: Please note that the version of the project, the latest official version shall prevail.


 Before long blog post, I generally translated a series of articles, but due to the CM framework update too fast, plus a main reason that he is too slow translation of the article, a word search, so only translated a more than half, but enough people use. This article describes a version of the V2 and V3 will be a little new number. But the new version and the old version of the main uses almost see the official demonstration below.

Basic Configuration, Actions and Conventions

1. Start Visual Studio, Create a Solution "Caliburn.Micro.Hello", using nuget mode (recommended) or manual reference dll way to add class libraries and System.Windows.Interactivity.dll CM frame.
Delete "MainPage.xaml", modify "App.xaml.cs", like the one below:

Copy the code
namespace Caliburn.Micro.Hello {
    using System.Windows;

    public partial class App : Application {
        public App() {
            InitializeComponent();
        }
    }
}
Copy the code

2. CM framework also recommend using View-Model-First way, we have to do this, first create a VM, called "ShellViewModel", the code is as follows:

Copy the code
namespace Caliburn.Micro.Hello {
    using System.Windows;

    public class ShellViewModel : PropertyChangedBase {
        string name;

        public string Name {
            get { return name; }
            set {
                name = value;
                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSayHello);
            }
        }

        public bool CanSayHello {
            get { return !string.IsNullOrWhiteSpace(Name); }
        }

        public void SayHello() {
            MessageBox.Show(string.Format("Hello {0}!", Name)); //Don't do this in real life :)
        }
    }
}
Copy the code

You can see, ShellViewModel inherited from PropertyChangedBase, simple base class that defines the properties of notification, the notification interface enables simple update function.

3. With a simple VM, Here we define what bootstrapper, create a new class of "HelloBootstrapper", the code is as follows:

Copy the code
namespace Caliburn.Micro.Hello {
    public class HelloBootstrapper : BootstrapperBase {
        public HelloBootstrapper() {
            Initialize();
        }

        protected override void OnStartup(object sender, StartupEventArgs e) {
            DisplayRootViewFor<ShellViewModel>();
        }
    }
}
Copy the code

This Bootsrapper by calling a method that defines a "top-ViewModel". When the program starts, CM framework at initialization will load the top VM, and VM display the corresponding View.
In some common mvvm Bootsrapper frame, it can be seen as an inlet configured, such as configuration settings, configuration into the container, and the like.

4. Next, we replace the default startup procedures HelloBootstrapper, modifications "App.xaml", as follows:

Copy the code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Hello"
             x:Class="Caliburn.Micro.Hello.App">
    <Application.Resources>
        <local:HelloBootstrapper x:Key="bootstrapper" />
    </Application.Resources>
</Application>
Copy the code

 WPF:

Copy the code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Hello"
             x:Class="Caliburn.Micro.Hello.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:HelloBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Copy the code

 Here, we have added a resource for the App, resource named "bootstrapper", need to be reminded, here has been deleted value "Application.StartupUri" attribute, the form does not need to start by default, instead by CM frame the bootstrapper way to start the "top ViewModel" configured earlier.

Next, you can try to start your program, you will see the interface has the following words. 


Caliburn.Micro.Hello.ShellView not found.

Caliburn.Micro has established a ShellViewModel, but do not know how to show it, so we need a corresponding View, then we create a user control (User Control), called "ShellView", xaml code is as follows: 

Copy the code
<UserControl x:Class="Caliburn.Micro.Hello.ShellView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox x:Name="Name" />
        <Button x:Name="SayHello"
                Content="Click Me" />
    </StackPanel>
</UserControl>
Copy the code

Start your program again, this time you can see just defined the UI interface.

Try to enter text in the TextBox, when Button is available, click on it, you can see a pop-up message window.

CM framework defines a set of simple and effective "naming convention" function is used to match the link between View and ViewModel.
In fact, it is to get to the full name of the specified VM, and remove the "Model" section, the rest is a corresponding View.
For example, for "MyApp.ViewModels.MyViewModel", you will get "MyApp.Views.MyView".

Compare View and ViewModel, can be found, there is a TextBox control View, [x: Name = "Name"] section, is bound to implement the ViewModel in the "Name" property. Also found that, View has a Button control, [x: Name = "SayHello"] section, is bound to implement the ViewModel in the "SayHello" method. Finally, the availability of methods ViewModel There is also a "CanSayHello" property, it will be linked to the "SayHello", such as binding function so that the corresponding button is unavailable.

These features are the VM ActionMessage (bound to method) function and Conventions (agreement) reflects the function.

 

Guess you like

Origin www.cnblogs.com/aijiao/p/11078615.html