WPF与Prism一并学习(四)

创建WPF程序的三种方式:

1.只使用代码

2.使用代码和未经编译的标记(XAML)

3.使用代码和编译过的标记(BAML)


第一种不写了,比较简单

第二种:

所谓的第二种方法就是直接读一个XAML文件,解析它并创建界面元素,要注意的是要读的XAML文件属性要设置成如下的样子


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

        public Window1(string xamlFile)
        {
            InitializeComponent(xamlFile);
        }                

        private void InitializeComponent(string xamlFile)
        {
            // Configure the form.
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Dynamically Loaded XAML";

            // Get the XAML content from an external file.
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }

            // Insert the markup into this window.
            this.Content = rootElement;                       

            // Find the control with the appropriate name.
            //button1 = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
            FrameworkElement frameworkElement = (FrameworkElement)rootElement;
            button1 = (Button)frameworkElement.FindName("button1");

            // Wire up the event handler.
            button1.Click += new RoutedEventHandler(button1_Click);
        }
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.Content = "Thank you.";
        }
    }
上面直接把Window1.xaml从硬盘读到文件流中,再转化成DependencyObject对象,DependencyObject是所有WPF控件继承的基类.

上面被注释的代码

  1. button1 = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");  
button1 = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");

  1. FrameworkElement frameworkElement = (FrameworkElement)rootElement;  
  2.             button1 = (Button)frameworkElement.FindName("button1");  
FrameworkElement frameworkElement = (FrameworkElement)rootElement;
            button1 = (Button)frameworkElement.FindName("button1");
是等效的, LogicalTreeHelper是一个辅助类,具有查找一棵完整控件对象树的能力,这个示例中XAML文件中的根元素是DockPanel,DockPanel又继承自 FrameworkElement类,所以使用FrameworkElement.FindName()方法同样可以找到控件对象.

第三种:

这种方法是VS自己支持的,所有代码都是自动生成的,不用我们人为去编码,即把XAML编译成BAML文件,再嵌入dll中作为资源保存,程序运行时再从dll中读出来. 读BMAL比直接读XMAL文件效率要高,因为BMAL已经经过了优化,文件体积也比XMAL小. 编译器在编译成会生成一个BMAL文件,和一个分部类,它们会放在项文件夹的obj\Debug子文件夹中.

下面来看看这个VS自动生成的部分类,Window1.g.cs (只呈现主要逻辑代码,省略掉非关键部分)

  1. /// <summary>  
  2. /// Window1  
  3. /// </summary>  
  4. public partial class Window1 : System.Windows.Window, System.Windows.Markup.IComponentConnector {  
  5.       
  6.      
  7.     internal System.Windows.Controls.TextBox txtQuestion;  
  8.       
  9.     internal System.Windows.Controls.Button cmdAnswer;  
  10.       
  11.     internal System.Windows.Controls.TextBox txtAnswer;  
  12.      
  13.     #line default  
  14.     #line hidden  
  15.       
  16.     private bool _contentLoaded;  
  17.       
  18.     /// <summary>  
  19.     /// InitializeComponent  
  20.     /// </summary>  
  21.     public void InitializeComponent() {  
  22.         if (_contentLoaded) {  
  23.             return;  
  24.         }  
  25.         _contentLoaded = true;  
  26.         System.Uri resourceLocater = new System.Uri("window1.baml", System.UriKind.Relative);  
  27.           
  28.         System.Windows.Application.LoadComponent(this, resourceLocater);  
  29.          
  30.         #line default  
  31.         #line hidden  
  32.     }  
  33.       
  34.    ......  
  35.     void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {  
  36.         switch (connectionId)  
  37.         {  
  38.         case 1:  
  39.         this.txtQuestion = ((System.Windows.Controls.TextBox)(target));  
  40.         return;  
  41.         case 2:  
  42.         this.cmdAnswer = ((System.Windows.Controls.Button)(target));  
  43.           
  44.         this.cmdAnswer.Click += new System.Windows.RoutedEventHandler(this.cmdAnswer_Click);  
  45.           
  46.   
  47.         return;  
  48.         case 3:  
  49.         this.txtAnswer = ((System.Windows.Controls.TextBox)(target));  
  50.         return;  
  51.         }  
  52.         this._contentLoaded = true;  
  53.     }  
  54. }  
    /// <summary>
    /// Window1
    /// </summary>
    public partial class Window1 : System.Windows.Window, System.Windows.Markup.IComponentConnector {
        
       
        internal System.Windows.Controls.TextBox txtQuestion;
        
        internal System.Windows.Controls.Button cmdAnswer;
        
        internal System.Windows.Controls.TextBox txtAnswer;
        
        #line default
        #line hidden
        
        private bool _contentLoaded;
        
        /// <summary>
        /// InitializeComponent
        /// </summary>
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("window1.baml", System.UriKind.Relative);
            
            System.Windows.Application.LoadComponent(this, resourceLocater);
            
            #line default
            #line hidden
        }
        
       ......
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            switch (connectionId)
            {
            case 1:
            this.txtQuestion = ((System.Windows.Controls.TextBox)(target));
            return;
            case 2:
            this.cmdAnswer = ((System.Windows.Controls.Button)(target));
            
            this.cmdAnswer.Click += new System.Windows.RoutedEventHandler(this.cmdAnswer_Click);
            

            return;
            case 3:
            this.txtAnswer = ((System.Windows.Controls.TextBox)(target));
            return;
            }
            this._contentLoaded = true;
        }
    }

方法二中的XamlReader用处很大,下面的代码摘自 这个地方

通过XamlReader 动态构建并实例化一个Window

<span style="color: green;">//XamlReader  
</span><span style="color: rgb(43, 145, 175);">StringBuilder </span>strXMAL = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">StringBuilder</span>(<span style="color: rgb(163, 21, 21);">"<Window "</span>);
strXMAL.Append(<span style="color: rgb(163, 21, 21);">"xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" "</span>);
strXMAL.Append(<span style="color: rgb(163, 21, 21);">"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" "</span>);
strXMAL.Append(<span style="color: rgb(163, 21, 21);">"Title=\"Window2\" Height=\"600\" Width=\"600\">"</span>);
strXMAL.Append(<span style="color: rgb(163, 21, 21);">"</Window>"</span>);
<span style="color: blue;">var </span>window = (<span style="color: rgb(43, 145, 175);">Window</span>)<span style="color: rgb(43, 145, 175);">XamlReader</span>.Parse(strXMAL.ToString());
window.ShowDialog();

作者还自己实现了BMAL文件的Reader和Writer,有兴趣可以研究研究,一般情况下也用不上的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值