创建WPF程序——内容(3)

一.新建项目

1.XAML文档创建窗体程序

创建步骤如下:

1.打开VS2013 程序

2.单击文件→新建→项目

3.按自己创建的要求进行创建项目,再单击确定

4.设计窗体,并添加关联事件,最后再编写后台代码

2.其他创建程序方法

1.只使用代码创建WPF项目

1.进入界面之后删除的结果如图所示。

将第二个项目中的红线框内的三个文件删除。

2.新建一个类,名为Window1。打开,然后添加名称空间,之后编写代码如下图所示。

using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

 

 

namespace WpfApplication2
{
    class Window1:Window
    {
        private Button button1;
        public Window1()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        { 
        //设置窗体大小
            this.Width = 285;
            this.Height = 250;
            this.Left = this.Top = 100;
            this.Title = "Code-Only Window";

            //创建停靠面板
            DockPanel panel = new DockPanel();
            //创建按钮对象
            button1 = new Button();
            button1.Content = "Please click me.";
            button1.Margin = new Thickness(40);

            button1.Click +=button1_Click;

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

            container = this;
            container.AddChild(panel);
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.Content = "Thank you.";
        }
    }
}

 

 3.新建一个启动类,名为Program,其中需要添加一个名称空间,即

using System.Windows;

 

using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
namespace WpfApplication2
{
    class Program:Application
    {
        [STAThread()]
        static void Main()
        {
            Program app = new Program();
            app.MainWindow = new Window1();
            app.MainWindow.ShowDialog();

        }
    }
}

 

4.修改启动项为第二个项目,即可运行

结果如下:

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

1.打开用记事本创建的文件Window1.xaml,如下图

内容如下所示,见下图

2.将文件移到项目文件夹内部,然后删除和添加一个Program类。文件框架如下:

 

3.编写MainWindow的代码如下所示:

using System.Windows;
using System.Windows.Markup;
using System.Windows.Controls;
using System.IO;

namespace CodeAndXaml
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button my_button;
        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(string xamlFile)
        { 
            //设置窗体
            this.Width = this.Height = 285;
            this.Left = this.Top = 200;
            this.Title = "Dynamically Loaded XAML Window";

            //从外面文件获取内容
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }
            this.Content = rootElement;
            //-------------------------------------以上部分可以读出窗体控件内容
            my_button = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
            my_button.Click+=my_button_Click;
        }
        private void my_button_Click(object sender, RoutedEventArgs e)
        {
            my_button.Content = "Thank you.";
        }
    }
}

3.编写添加的类Program,代码如下所示:

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

namespace CodeAndXaml
{
    class Program:Application
    {
        [STAThread()]
        static void Main()
        {
            Program app = new Program();
            app.MainWindow = new MainWindow("Window1.xaml");
            app.MainWindow.ShowDialog();
        }
    }
}

4.程序的运行结果如图所示

单击之后,结果如下:

5.总结

①外面的XAML文件传入进来,需要一个桥梁,这个工作主要由文件流来完成的。

②查找外部文件的元素,主要通过逻辑树的节点来查询的。

③窗体与外部文件的交互使用了DependencyObject类。

 

写的不好,有什么地方不对的请指正!

3.使用代码和编译过的XAML

待续

posted @ 2016-08-17 17:36  誓ぎ食  阅读(123)  评论(0编辑  收藏  举报