国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

在WPF中動態加載XAML中的控件實例代碼

2019-10-29 21:24:49
字體:
來源:轉載
供稿:網友

本文實例講述了在WPF中動態加載XAML中的控件的方法。分享給大家供大家參考,具體如下:

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;/* * 功能:測試WPF中動態加載XAML中的控件 *    并加到指定的子節點中。 * 作者:Kagula * 時間:2012-09-20 * 環境:VS2008 .NET FRAMEWORK 3.5 * 參考資料:[1]《Application=Code+Markup 讀書筆記 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();    }    //從字符串中加載    public void LoadEmbeddedXaml()    {      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);    }    //從外部文件中加載 Button控件    public void LoadEmbeddedXaml2()    {      XmlTextReader xmlreader = new XmlTextReader("d://a.xaml");      UIElement obj = XamlReader.Load(xmlreader) as UIElement;      grid1.Children.Add((UIElement)obj);    }    //從資源文件中加載    public void LoadEmbeddedXaml3()    {      //Build Action = Resource,Do not Copy,無相應cs文件      Uri uri = new Uri("/LoadXamlResource.xaml",UriKind.Relative);      Stream stream =Application.GetResourceStream(uri).Stream;      //FrameworkElement繼承自UIElement      FrameworkElement obj =XamlReader.Load(stream) as FrameworkElement;      grid1.Children.Add(obj);    }  }}

xaml文件中的清單

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

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄城县| 来安县| 鄯善县| 通榆县| 安义县| 泾源县| 五寨县| 都匀市| 南安市| 唐山市| 上杭县| 阆中市| 英超| 闸北区| 新密市| 平原县| 彭阳县| 勐海县| 手机| 大港区| 靖边县| 禄劝| 工布江达县| 香格里拉县| 西吉县| 河北省| 炎陵县| 疏勒县| 邹城市| 合阳县| 油尖旺区| 邵东县| 永州市| 华容县| 吴川市| 锡林浩特市| 广德县| 深州市| 青川县| 永平县| 包头市|