WPF入門之WPF載入和編譯xaml
阿新 • • 發佈:2018-12-22
環境:vs2013
雖然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
MainWindow.xaml.cs<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>
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檔案的內容,然後重新執行,示例效果: