您的位置:首页 > 其它

WPF入门之WPF加载和编译xaml

2017-08-20 21:53 302 查看
环境: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应用程序的方式,不再赘述。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xaml