環境: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檔案的内容,然後重新運作,示例效果: