加载和编译Xaml(宝典2)

Posted Ant-double

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加载和编译Xaml(宝典2)相关的知识,希望对你有一定的参考价值。

三个方式来实现wpf的应用程序

1/只使用代码

好处:就是随意的定制应用程序,相比之下xaml文档,它们只能作为固定的不变的资源嵌入到程序集中

坏处:因为wpf控件包含没有参数的构造函数,所以一个简单的控件就可写很多代码

demo:

新建一个普通类(不是窗口类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace WPF_Valuable
{
    class Window1:Window
    {
        private Button button1;
        public Window1()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            //configure the form
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Code-only Window";

            //creater a container to hold a button
            DockPanel panel = new DockPanel();

            //Creater a button.
            button1 = new Button();
            button1.Content = "Please click me";
            button1.Margin = new System.Windows.Thickness(30);
            //Attach  the event handler
            button1.Click += button1_Click;

            IAddChild container = panel;
            container.AddChild(button1);

            container = this;
            container.AddChild(panel);

        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            button1.Content = "Thank you ";
        }
    }
}
View Code

 二,使用代码xaml

 1  public class Window1 : Window
 2     {
 3         private Button button1;        
 4 
 5         public Window1(string xamlFile)
 6         {
 7             InitializeComponent(xamlFile);
 8         }                
 9 
10         private void InitializeComponent(string xamlFile)
11         {
12             // Configure the form.
13             this.Width = this.Height = 285;
14             this.Left = this.Top = 100;
15             this.Title = "Dynamically Loaded XAML";
16 
17             // Get the XAML content from an external file.
18             DependencyObject rootElement;
19             using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
20             {
21                 rootElement = (DependencyObject)XamlReader.Load(fs);
22             }
23 
24             // Insert the markup into this window.
25             this.Content = rootElement;                       
26 
27             // Find the control with the appropriate name.
28             //button1 = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
29             FrameworkElement frameworkElement = (FrameworkElement)rootElement;
30             button1 = (Button)frameworkElement.FindName("button1");
31 
32             // Wire up the event handler.
33             button1.Click += new RoutedEventHandler(button1_Click);
34         }
35         
36         private void button1_Click(object sender, RoutedEventArgs e)
37         {
38             button1.Content = "Thank you.";
39         }

// First approach: window with XAML content.
Window1 window1 = new Window1("Window1.xaml");
window1.Show();

 

以上是关于加载和编译Xaml(宝典2)的主要内容,如果未能解决你的问题,请参考以下文章

WPF 获取xaml

Java面试宝典每日3题:day28

损坏的顶点和片段着色器

解读WPF中的Xaml

面试宝典之Java程序运行原理

捕捉WPF应用程序中XAML代码解析异常