The example in this article describes the methods for dynamically loading controls in XAML in WPF. Share to everyone for your reference, specific as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Imaging;
Using System.Windows.Navigation;
Using System.Windows.Shapes;
Using System.IO;
Using System.Xml;
Using System.Windows.Markup;
* * Function: Test the control in XAML dynamically loading in WPF and add it to the specified child node. * Author: Kagula * Time: 2012-09-20 * Environment: VS2008. NET FRAMEWORK 3.5 * Reference: [1] "Application=code+markup reading notes 19" * http:/ /SPACE.ITPUB.NET/15123181/VIEWSPACE-423015 * [2] "Pack URIs in Windows presentation Foundation" * Http://tech net.microsoft.com/en-us/library/aa970069 (v=vs.90) * * namespace Testxamlload {///<summary>///interaction Log IC for Window1.xaml///</summary> public partial class Window1:window {public Window1 () {in
Itializecomponent (); Loadembeddedxaml ();
LOADEMBEDDEDXAML2 ();
LOADEMBEDDEDXAML3 ();
///Load public void Loadembeddedxaml () {Title = ' load Embedded Xaml ' from string; String Strxaml = "<button xmlns= ' http://schemas.microsoft.com/winfx/2006/xaml/presentation '" + "foreground= ' Li
Ghtseagreen ' fontsize= ' 16pt ' width= ' 128 ' height= ' > ' + ' from String object!</button> ';
StringReader strreader = new StringReader (STRXAML);
XmlTextReader xmlreader = new XmlTextReader (Strreader);
Object obj = Xamlreader.load (XmlReader); Grid1.
Children.add ((UIElement) obj); ///Load button control public void LoadEmbeddedXaml2 () {XmlTextReader xmlreader = new XmlTextReader from external file ("D:\
\a.xaml ");
UIElement obj = Xamlreader.load (XmlReader) as UIElement; Grid1.
Children.add ((UIElement) obj); ///Load public void LOADEMBEDDEDXAML3 () from resource file//build Action = Resource,do not Copy, no corresponding CS file Uri u RI = new Uri ("/loadxamlresource.xaML ", urikind.relative); Stream Stream =application.getresourcestream (URI).
Stream;
FrameworkElement inherits from UIElement FrameworkElement obj =xamlreader.load (stream) as FrameworkElement; Grid1.
Children.add (obj);
}
}
}
Manifests in a XAML file
<button xmlns= ' http://schemas.microsoft.com/winfx/2006/xaml/presentation '
foreground= ' Lightseagreen '
fontsize= ' 16pt '
Width= ' 128 '
height= ' > from
File object!
</Button>
Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming
I hope this article will help you with C # programming.