XamlReader of WPF & Silverlight dynamically creates controls and binds them

Source: Internet
Author: User
Tags silverlight

Use XamlReader. Load

XamlReader is a stateless static class with methods. It can create Objects Based on Input XAML tags. XamlReader provides parallel object building behavior for XAML analysis through the Silverlight runtime and the Silverlight application model. Analyze the runtime Object Tree of the managed object generated by XAML. This object tree provides methods to reference those named at run time (identified by Name or x: Name in the analyzed XAML) or those objects that are going through the complete tree.

When using the Load method to create a XAML object, there are several general concepts that need to be understood.

The XAML content string must define a single root element.

The XAML content string must be a well-formatted XML and must be a valid XAML.

The root element must declare the XAML namespace required for any entity referenced in XAML. This is true for the default Silverlight XAML namespace, and most strings of Load should specify xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" in the root element so that the Silverlight core XAML namespace is the default XAML namespace.

Any custom Assembly referenced in the XAML namespace ing must be available for applications (typically packaged through AssemblyPart ).

The Load XAML should not try to specify x: Class, or include any XAML-defined attributes of the event handler. The Load logic cannot integrate the loaded XAML with the code hiding class in the runtime era. If you want to add an event handler, you must do this in the Code: reference the object obtained in the object tree structure of the Load result, the language-specific syntax is used to append the handler (for example, + = ). For more information about additional events using code, see Silverlight event overview.

Connecting the created object tree to the primary Silverlight Object Tree demonstrates the relationship between the Silverlight object hierarchy and the newly created object tree from the XAML fragment.

Disconnected Silverlight Object Tree

All Silverlight content on the HTML page hosted by the browser is rendered at last, because it is connected to the object loaded as RootVisual in the Silverlight application model in the object relationship. Then, RootVisual is a composite Silverlight that will render the corresponding content and pass it to the HTML host that uses a plug-in/control access layer (Silverlight implemented in its local code. Initially, no object tree is displayed that is not connected to the primary object tree (based on the RootVisual tree.

When you create a disconnected Object Tree in the XAML fragment, you can add it to the main Silverlight Object Tree by calling an Api object that exists in the main object tree. You can set the disconnected object tree to the property value of an existing object, or add it to a set of other peer objects as a sub-object. (The latter is actually a set of attributes of the parent object .) When this snippet is part of the main Silverlight Object Tree, Silverlight checks changes made up of the primary object tree and presents the objects in the XAML fragment. Demonstrate the new relationship between the Silverlight Object Tree and the object tree from the analysis of the XAML fragment (after the connection ).

Connected Silverlight Object Tree

The following requirements apply to dynamically Add the content generated by XAML to the main Silverlight Object Tree:

The XAML content associated with the Silverlight plug-in must exist. The entire content tree cannot be replaced. At least the original root element must be retained.

Objects Created in The XAML analysis API can only be assigned to one of the primary objects. (This is similar to the initial XAML processing behavior, that is, the relationship between the xaml and the created object is, except for the special circumstances of the template .) If you want to add an object created in the same XAML to the application main object tree in different regions, you must analyze the XAML multiple times using the same input string and different return value targets.

The existing Silverlight object in the main object tree that is expected to be used as the connection point must support appropriate attributes to the set. In other words, the connection point object must have some form of configurable property values. Some examples are as follows:

The connection point object supports generalized sub-element attributes such as Child or Content, which requires a base type, such as UIElement. The root object of the new object tree must be assigned to this attribute.

The connection point object supports the set property, and the set type of the property also supports the Add method. The new object tree can be added as a project.

The connection point object supports the root object of a new tree with matching property values of a specific type. For example, you can create a new Brush and Load input and use it to set the Background value.

To protect Silverlight-based applications from security attacks, it is strongly recommended that you do not pass untrusted XAML strings to Load (or to JavaScript equivalent CreateFromXaml ). Untrusted XAML may contain camouflage interfaces of valid images, which may cause security spoofing threats. In addition, XAML may contain references to the script event handler. If you are not ready to declare the managed API in XAML by specifying the x: Class of the currently loaded XAML, note that the default programming model excluding x: Class is JavaScript. The result is that an untrusted XAML will be added to the master tree, which may lead to the Silverlight application object tree that does not intend to execute the script. Always ensure that the source of the used XAML content is trusted.

Dynamically load and bind instances

StringBuilder sb = new StringBuilder ();

Sb. Append (@ "<Paragraph xmlns =" "http://schemas.microsoft.com/winfx/2006/xaml/presentation" "> XXXXXX ");

Sb. append (@ "<Run Foreground =" Red "" FontWeight = "" Normal "" Text = "" {Binding Filed, mode = OneWay} ""> </Run> XXXXXX ");

Sb. Append (@ "</Paragraph> ");

Paragraph pg = (Paragraph) XamlReader. Load (sb. ToString ());

If (Paragraph) txtTopInfo. Blocks [0]! = Pg)

{

TxtTopInfo. Blocks. Clear ();

This.txt TopInfo. Blocks. Add (pg );

}

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.