WPF入门之WPF加载和编译xaml

环境:vs2013

示例代码:http://pan.baidu.com/s/1miA6aNY

虽然WPF中主要使用xaml来写界面,但是程序依然可以脱离xaml而独自运行,下面说明使用三种不同的编码方式来创建WPF应用程序:

1.只使用代码

这种方式比较极端,但也存在优势。

代码示例:新建一个wpf应用程序,将工程中的xaml文件以及xaml.cs文件删除,只留下App.config文件,然后新建两个类文件,一个是Program.cs,另一个是MainWindow.cs

工程代码结构如图:


Program.cs

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

namespace 只使用代码
{
    public class Program : Application
    {
        [STAThread]
        public static void Main()
        {
            Program app = new Program();
            app.MainWindow = new MainWindow();
            app.MainWindow.ShowDialog();
        }
    }
}

MainWindow.cs

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 只使用代码
{
    class MainWindow : Window
    {
        private Button button1;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            //Configure the form
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Code-Only Window";

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

            //Create the button
            button1 = new Button();
            button1.Content = "Please click me";
            button1.Margin = new Thickness(30);

            //Attach the event handler
            button1.Click += button1_Click;

            //Place the button in the panel
            IAddChild container = panel;
            container.AddChild(button1);

            container = this;
            container.AddChild(panel);
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.Content = "Thank you";
        }

    }
}

运行效果:


2.使用代码和未经编译的XAML

2.1 创建一个新wpf工程然后添加一个Page1.xaml文件(注意没有Page1.xaml.cs文件)

  注意设置Page1.xaml文件的属性(生成为空,复制为总是复制)

工程结构如图:


2.2 文件代码内容:

    MainWindow.xaml

<Window x:Class="使用代码和未经编译的XAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Click="Button_Click">给新窗口状态xmal代码</Button>
    </Grid>
</Window>
    MainWindow.xaml.cs

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.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.Windows.Markup;

namespace 使用代码和未经编译的XAML
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window win1 = new Window();
            win1.Width = 400;
            win1.Height = 400;
            win1.Left = win1.Top = 200;
            win1.Title = "动态创建的元素";
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Page1.xaml"), FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }
            win1.Content = rootElement;
            win1.ShowDialog();
        }
    }
}

   Page1.xaml

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button>haha</Button>
</Grid>
编译后,将生成的文件拷贝出来,如图:


双击exe运行,关闭程序,修改Page1.xaml文件的内容,然后重新运行,示例效果:


3.使用代码和编译过的xaml文件,一般的创建wpf应用程序的方式,不再赘述。





  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值