[WPF] Chapter learn to use other types of namespaces

    As already described how to create a basic user interface in WPF class in XAML. But XAML is a general method to instantiate .NET objects, including those located in other non-target WPF namespaces and create their own space in.

  Create objects that are not used in XAML window displays sounds like excess, but in many cases it is needed. One example is, but using data binding and hope that when the information extracted from other objects displayed on a control. Another example is to use a non-WPF WPF object set properties for the object.

  For example, you can be filled using the WPF ListBox control data objects. ListBox control will call ToString () method to get the text to display for each entry in the list.

  Not to use the class defined in the WPF namespace. Requires .NET namespace mapped to XML namespaces. There is a special XAML syntax can be used to complete the job, the syntax is as follows:

  xmlns:Prefix="clr-namespace:Namespace;assembly=AssemblyName"

  Typically, the root element XAML document, place the namespace declaration immediately after the characteristics of WPF and XAML namespaces. The appropriate information needed to fill three italics, the meaning of the following three parts:

  • Prefix is ​​hope for XML namespace prefix indicates in XAML markup. For example, XAML syntax x prefix.
  • Namespace .NET namespace name is fully qualified.
  • AssemblyName is a statement of the type of assembly, no expansion .dll name. The assembly must be referenced in the project. If you want to use the project assembly, you can ignore this part.

  For example, the following demonstrates how to mark the type of the basic scope of the System namespace and prefix mapped sys:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

  Now, in order to create an instance of one class namespace, namespace prefix may be used:

<sys:DateTime>2019-12-30 12:20:00</sys:DateTime>

   Ideally, it is desirable for each class used in XAML has no argument constructor. If the constructor with no arguments, the XAML parser can create corresponding object, set its properties, and associated with any event handler provided. XAML does not support there is argument constructor, and all the elements in WPF contains no argument constructor. In addition, the need to be able to use public property to expect all the details. XAML allowed to set public field or method call.

  If you do not want to use the class constructor with no arguments, there are some restrictions. If the attempt to create a simple basic types (e.g., string, date, or digital type), the data may be provided as a string representation of the contents of the tag. XAML parser then use type converter to convert the string to an appropriate target. As shown in the above code segment, for example, it is an example of using DateTime structure.

  Since the DateTime TypeConverter used to associate itself to the characteristics of DateTimeConverter class, so the above mark may be ridiculous. DateTimeConverter class knows this string is legal DateTime object and to convert them. When using this technique, the characteristics can not be used to set any property of the object.

  The following example sets all of these concepts together. The sys prefix mapped to the System namespace, and create three DateTime object using the System namespace, and then use these three DateTime object fill a list:

<Window x:Class="WpfApplication1.DateTimeWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="DateTimeWindow" Height="300" Width="300">
    <ListBox>
        <ListBoxItem>
            <sys:DateTime>2019-12-30 12:20:00</sys:DateTime>
        </ListBoxItem>
        <ListBoxItem>
            <sys:DateTime>2019-12-31 12:20:00</sys:DateTime>
        </ListBoxItem>
        <ListBoxItem>
            <sys:DateTime>2019-12-3 12:20:00</sys:DateTime>
        </ListBoxItem>
    </ListBox>
</Window>

 

Guess you like

Origin www.cnblogs.com/Peter-Luo/p/12147286.html