WPF 應用程式進入點、核心組件引用、Application/Window 模型、XAML/Code-behind 分離、VS 自動生成代碼機制 — 奠定所有 WPF 開發的基石。
// MyApp.cs
using System;
using System.Windows; // 核心命名空間
namespace MyFirstAvalonApp {
class MyApp {
[STAThread] // WPF 必須單執行緒單元模型
static void Main() {
MessageBox.Show("Hello, Avalon"); // WPF 訊息盒
}
}
}
| 組件 | 命名空間 | 核心功能 |
|---|---|---|
WindowsBase.dll | `System.Windows` (基礎) | DependencyObject, Dispatcher, 資源系統, 命令 |
PresentationCore.dll | `System.Windows` (核心) | Visual, UIElement, 佈局, 輸入, 繪圖 |
PresentationFramework.dll | `System.Windows.Controls` 等 | Window, Control, Panel, 應用級框架 |
**注意**:引用的是 `\Framework\v3.0\` 下的 DLL,非 `\Framework\v2.0.50727\System.Windows.Forms.dll`。
<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputType>winexe</OutputType> <!-- GUI 應用 -->
<OutputPath>.\</OutputPath>
<Assembly>1st.exe</Assembly>
</PropertyGroup>
<ItemGroup>
<Compile Include="MyApp.cs" />
<Reference Include="System" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!-- 關鍵導入 -->
</Project>
| 類型 | 根標籤 | 適用場景 | 入口 |
|---|---|---|---|
| **Window Application (C/S)** | ` | 桌面獨立程式 | `App.xaml` + `Application` |
| **Browser Application (B/S, XBAP)** | ` | 瀏覽器託管 (需信任) | `App.xaml` + `Application` |
| 代碼 | XAML | 原理 |
|---|---|---|
| `this.Content = btn;` | 子元素放在 ` | Window 繼承 ContentControl |
| `btn.Click += ...` | `x:Name="btn"` + 後台事件綁定 | 分部類合併 |
| `new Window1()` | `StartupUri="Window1.xaml"` | Application 載入 |
理解 `STAThread`、`Dispatcher`、`Application`、`Window`、三大組件、MSBuild、分部類、XAML/Code-behind 雙重性 — 即擁有 WPF 開發的「地圖與指南針」。