定義應用程式並寫出第一個提示

一個有用的第一個提示詞會指出技術、應用程式行為、限制因素,以及能證明工作已完成的證據。 這降低了助理選擇熟悉但錯誤 Windows 框架的可能性。

從接受標準開始

在要求程式碼前,請先定義你預期會觀察到的狀況:

  1. 該專案能為 x64 編譯,且沒有錯誤或警告。
  2. 這個應用程式使用 WinUI 3 和 Microsoft.UI.Xaml,而不是 UWP 和 Windows.UI.Xaml
  3. 使用者可以新增、完成或刪除任務。
  4. 應用程式重新啟動後,任務依然存在。
  5. 每個輸入和指令都有一個可存取的名稱。
  6. 當視窗變窄時,指令仍可使用。
  7. 助理會解釋任何需要套件識別碼或視窗初始化的 API。

這些準則將籠統的要求轉為你和助理都能執行的檢查項目。

給予助理持久的約束

打開存放開發專案的資料夾,啟動你的 AI 程式助理,並使用這個提示:

Create a C# WinUI 3 desktop app named TaskTally.

Technology constraints:
- Use WinUI 3 and the current stable Windows App SDK.
- Use Microsoft.UI.Xaml namespaces. Don't use UWP, Windows.UI.Xaml,
  WPF, Windows Forms, .NET MAUI, or third-party UI controls.
- Create a packaged app and build for x64.
- Start from the dotnet new winui-mvvm template.
- Use CommunityToolkit.Mvvm for observable properties and commands.

App requirements:
- Show a single-page task list.
- Let the user add, complete, and delete tasks.
- Save tasks as JSON in the app's local storage.
- Use ListView for the collection and InfoBar for save status.
- Include an empty state, keyboard access, accessible names, and a
  layout that works at narrow window widths.

Working agreement:
- First show your implementation plan and the files you will change.
- Make one small, buildable change at a time.
- Build after each change and fix errors before continuing.
- Explain which Learn documentation supports any Windows API choice.
- Don't suppress warnings or replace packaged deployment with an
  unpackaged workaround.

最後一部分很重要。 它要求助理揭露假設,並提出更易檢查的檢查點。

檢查擬議的計畫

在核准編輯前,請檢查計畫是否包含:

  • 一個模型對應一項任務。
  • 一個具有可觀察集合與指令的視圖模型。
  • 一個倉儲服務。
  • 頁面用的是 XAML 的。
  • 如果需要,會有少量用於處理檢視專屬事件的後置程式碼。
  • 建立並啟動打包 WinUI 應用程式的步驟。

如果助理提出資料庫、依賴注入、多頁面或自訂控制,請助理修改計畫。 這些選擇在較大的應用程式中可能適用,但它們會加入初學者不需要的概念。

建立專案

助理應執行等同於以下指令:

dotnet new winui-mvvm -n TaskTally
cd TaskTally
dotnet build -p:Platform=x64

如果你使用 Visual Studio,可以改為建立一個打包好的 C# WinUI 專案,並加入 MVVM 工具包。 保留專案名稱 TaskTally ,讓教學中的命名空間相符。

Note

已封裝的 WinUI 應用程式在部署時必須具有其套件識別。 使用 Visual Studio、dotnet run搭配支援的 WinUI 範本,或使用 winapp CLI。 不要直接啟動產生的 .exe

檢查已產生的專案

提問:

Explain the purpose of App.xaml, MainWindow.xaml, MainPage.xaml,
Package.appxmanifest, and TaskTally.csproj. For each file, identify
the WinUI 3 or Windows App SDK setting that proves this isn't a UWP,
WPF, or MAUI project.

請自行核實說明:

  • 專案檔案包含 <UseWinUI>true</UseWinUI>
  • 此專案參考了 Microsoft.WindowsAppSDK
  • XAML 程式碼使用 C# 中的 Microsoft.UI.Xaml 型別。
  • Package.appxmanifest 提供套件識別。
  • 目標框架包含 Windows,例如 net10.0-windows10.0.26100.0

在新增功能前,先建立未更改的範本。 如果基線沒建立,先解決環境或專案建立的問題,再請助理產生更多程式碼。