Dynamic loading of control instance code in XAML in WPF

  • 2021-11-01 04:21:36
  • OfStack

In this paper, an example is given to describe the method of dynamically loading controls in XAML in WPF. Share it for your reference, 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 WPF Dynamic loading in XAML Controls in 
 *     And added to the specified child node. 
 *  By: Kagula
 *  Time: 2012-09-20
 *  Environment: VS2008 .NET FRAMEWORK 3.5
 *  References: [1] " Application=Code+Markup  Reading notes  19 " 
 *        http://space.itpub.net/15123181/viewspace-423015
 *      [2] " Pack URIs in Windows Presentation Foundation " 
 *        http://technet.microsoft.com/en-US/library/aa970069(v=vs.90)
 */
namespace testXAMLLoad
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      //LoadEmbeddedXaml();
      //LoadEmbeddedXaml2();
      LoadEmbeddedXaml3();
    }
    // Load from a string 
    public void LoadEmbeddedXaml()
    {
      Title = "Load Embedded Xaml";
      string strXaml = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" +
        " Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'>" +
        " 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 from an external file  Button Control 
    public void LoadEmbeddedXaml2()
    {
      XmlTextReader xmlreader = new XmlTextReader("d:\\a.xaml");
      UIElement obj = XamlReader.Load(xmlreader) as UIElement;
      grid1.Children.Add((UIElement)obj);
    }
    // Load from a resource file 
    public void LoadEmbeddedXaml3()
    {
      //Build Action = Resource,Do not Copy, No correspondence cs Documents 
      Uri uri = new Uri("/LoadXamlResource.xaml",UriKind.Relative);
      Stream stream =Application.GetResourceStream(uri).Stream;
      //FrameworkElement Inherit from UIElement
      FrameworkElement obj =XamlReader.Load(stream) as FrameworkElement;
      grid1.Children.Add(obj);
    }
  }
}

Manifests in the xaml file


<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  Foreground='LightSeaGreen'
  FontSize='16pt'
  Width='128'
  Height='32'>
  From File Object!
</Button>

For more readers interested in C # related content, please check the topics on this site: "Summary of Thread Use Skills in C # Programming", "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: