WPF入门之WPF加载和编译xaml
2017-08-20 21:53
302 查看
环境:vs2013
示例代码:http://pan.baidu.com/s/1miA6aNY
虽然WPF中主要使用xaml来写界面,但是程序依然可以脱离xaml而独自运行,下面说明使用三种不同的编码方式来创建WPF应用程序:
代码示例:新建一个wpf应用程序,将工程中的xaml文件以及xaml.cs文件删除,只留下App.config文件,然后新建两个类文件,一个是Program.cs,另一个是MainWindow.cs
工程代码结构如图:
Program.cs
MainWindow.cs
运行效果:
工程结构如图:
Page1.xaml
双击exe运行,关闭程序,修改Page1.xaml文件的内容,然后重新运行,示例效果:
示例代码: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应用程序的方式,不再赘述。
相关文章推荐
- 8天入门wpf—— 第二天 xaml详解
- WPF自学入门(一)WPF-XAML基本知识
- WPF入门(三)-使用XAML创建玻璃按钮
- 8天入门wpf—— 第二天 xaml详解
- 8天入门wpf—— 第二天 xaml详解
- WPF中动态加载XAML中的控件
- WPF中Xaml编译正常而Designer Time时出错的解决办法
- 8天入门wpf—— 第二天 xaml详解
- WPF - 运行时加载XAML文件
- 小麦子-WPF学习系列2:XAML入门
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
- XAML开发WPF程序入门之开发环境介绍
- WPF入门之XAML初窥
- WPF中Xaml编译正常而Designer Time时出错的解决办法
- WPF入门之XAML初窥
- XAML开发WPF程序入门之附加属性和绑定属性
- WPF入门:XAML
- WPF学习系列013: 2.8 编译:将 XAML 与过程式代码混合使用
- 【转载】8天入门wpf—— 第二天 xaml详解
- 测试WPF中动态加载XAML中的控件