Caliburn.micro Learning Notes (i)----boot class and naming matching rules

Source: Internet
Author: User

Took a few days to look at the open source framework Caliburn.micro

This is his source address, http://caliburnmicro.codeplex.com/.

The document is also written in detail, I wrote some demos and notes while looking at its documents and code, and the principle of its implementation recorded

Learning Caliburn.micro should have the foundation of MEF and MVVM

Let's talk about his naming rules and the boot class .

In the future, I'll take Caliburn.micro's

Actions

Iresult,ihandle

Iconductor,conductor<t>

These commonly used functions are written down.

First look at the general flow of caliburn.micro, painting is not very good, first of all

Well, let's start today's notes.

From a small example. Demo Download : Bootstrapperandconventions.rar

This example is a small feature of a form that has a parent form open suddenly

Three class libraries to be introduced by the program

Caliburn.micro

System.Windows.Interactivity

And

System.ComponentModel.Composition

The top two caliburn.micro examples are provided below in VS can be found

Take a look at the boot class

1  Public InterfaceIshell2    {3  4    }5     Public classMybootstrapper:bootstrapper<ishell>6    {7  8        PrivateCompositioncontainer _container;9  Ten        //combining parts with MEF One        protected Override voidConfigure () A        { -_container =NewCompositioncontainer ( -                NewAggregatecatalog (AssemblySource.Instance.Select (x =NewAssemblycatalog (x)). Oftype<composablepartcatalog>())); the   -            ///If you have your own parts, add them to this place. -CompositionBatch _batch =NewCompositionBatch (); -_batch. Addexportedvalue<iwindowmanager> (NewWindowManager ()); +_batch. Addexportedvalue<ieventaggregator> (Neweventaggregator ()); - _batch. Addexportedvalue (_container); +   A   at _container.compose (_batch); -        } -        //get an instance based on the key or name passed in. -        protected Override Objectgetinstance (Type service,stringkey) -        { -            string_contract =string. IsNullOrEmpty (key)?attributedmodelservices.getcontractname (Service): key; in   -            var_exports = _container. getexportedvalues<Object>(_contract); to   +            if(_exports. Any ()) -            { the                return_exports. First (); *            } $            Throw NewException (string. Format ("{0} instance not found", _contract));Panax Notoginseng        } -        //get all instances of a specific type the        protected Overrideienumerable<Object>getallinstances (Type service) +        { A            return_container. getexportedvalues<Object>(Attributedmodelservices.getcontractname (service)); the        } +        //passing instances to the Ioc container, causing dependency injection -        protected Override voidBuildUp (Objectinstance) $        { $ _container. Satisfyimportsonce (instance); -        } -   the}

We're going to implement the Bootstrapper<t> class.

In general, I use my MEF as a container, rewrite this class of three methods, the wording is more fixed, like I wrote on the above

If you have some of your own things to configure can be written in config

In addition to the top of the three methods and Onstartup and OnExit respectively is the program entry and exit execution events, according to their own needs to do a corresponding rewrite

and add it to the App.xaml.

<application x:class="Calibrunmicaction.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Xmlns:booter="clr-namespace:calibrunmicaction"> <Application.Resources> <ResourceDictionary> <resourcedictionary.mergeddictionari es> <ResourceDictionary> <booter:mybootstrapper x:key="Appbooter"/> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </res Ourcedictionary> </Application.Resources></Application>

The program will open the form of export Ishell

Principle

is based on the reflection of the MEF to find whether the container has exprort Ishell ViewModel if there is a name to match the corresponding view mapping relationship and open,

Throws an exception if it is not found

1<window x:class="Wpfapplication1.mymainview"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"4title="Mymainview"height=" -"Width=" -">5<StackPanel>6<textblock x:name="Strmain"Fontsize=" -"/>7<button x:name="Openonechild"Content="Openawindow"Width=" -"height=" -"/>8</StackPanel>9</Window>

Mainviewmodel

1 usingCaliburn.micro;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.ComponentModel.Composition;5 usingSystem.Linq;6 usingSystem.Text;7  8 namespaceWpfApplication19 {Ten[Export (typeof(Ishell))] One      Public classMymainviewmodel A     { -         ReadOnlyIwindowmanager _MYWM; -          Public stringStrmain the         { -             Get; -             Private Set; -         } + [Importingconstructor] -          PublicMymainviewmodel (iwindowmanager wm) +         { AStrmain ="Main!!!!!!"; at_MYWM =WM; -         } -Mychildoneviewmodel _MYCHILDW =NewMychildoneviewmodel (); -          Public voidOpenonechild () -         { -               in _mywm.showdialog (_MYCHILDW); -         } to     } +}

You will find that Mainview's backstage code and front desk are not assigned ViewModel

It's a great caliburn.microj. name matching rule : It uses reflection and regular expressions to match view and ViewModel

The system has existing forms and classes that automatically match the end of the name View and ViewModel, PageView, and Pageviewmodel

If you want to define a matching rule for yourself, I'm not going to talk about it.

Run up and you'll find

The properties and events for the TextBlock and button also automatically match.

Principle:

After matching the view and ViewModel.

To find the element name in the view and the method or property in the ViewModel if there is one to the binding

! Attention! : When naming a control, such as TXT_ABC and underline CALIBRN will separate the name.

INTO TXT and ABC two properties it will go to the TXT attribute to find the ABC attribute binding

The code to open the subform is to use the Caliburn.micro own Iwindowmanager interface

This is a class that is designed to open a form

It can open the form in Show () ShowDialog and ShowPopup form

Let's talk about this today, and next time we'll write about Caliburn's actions.

Demo Download: Bootstrapperandconventions.rar

Li Peng Source: http://www.cnblogs.com/li-peng/The author and the blog Park share, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Caliburn.micro Learning Notes (i)----boot class and naming matching rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.