WPF Canvas for VS Code
在同一个 VS Code 标签页编辑 XAML 并查看原生 WPF 预览。
Edit XAML and see a native WPF preview in the same VS Code tab.
Windows x64 · VS Code Desktop 1.100.0+
简体中文
打开 XAML
从 VS Code 资源管理器打开本地 .xaml 文件。左侧显示原生 WPF 预览,右侧编辑 XAML。使用 Ctrl+S 保存到当前文件。测试本地安装包时,在扩展视图的 … 菜单选择 从 VSIX 安装…,再选择 Windows x64 的 VSIX。
如果已有其他默认编辑器,在 XAML 标签页右键选择 使用其他编辑器重新打开…,然后选 WPF Canvas。可在同一菜单配置默认编辑器。
功能和模式
| 功能 |
用途 |
| 界面预览 |
源码更改后更新 WPF 预览。支持缩放、悬停和禁用状态。遇到 XAML 错误时保留上一次有效画面并提示位置。 |
| 编辑模式 |
从控件库拖入常用 WPF 控件,移动画布中的控件,或把草稿放到窗口外。用属性面板调整选中控件,改动会写回 XAML。 |
| XAML 文档格式化 |
点击工具栏的 格式化,或在源码内按 Shift+Alt+F,整理缩进和属性布局。格式化支持撤销,保存使用 Ctrl+S。 |
| 预览规则 |
XAML 中的设计时值(如 d:Text、d:DataContext、d:ItemsSource)优先。可用样例文字、列表数据或自定义 JSON 补齐没有设计时值的绑定。 |
| 资源库 |
选择任意本地文件夹,递归读取以 WPF ResourceDictionary 为根元素的 .xaml。扫描跳过示例窗口等其他文件,资源库文件以只读方式打开。 |
| 主题与语言 |
预览工具和编辑器跟随 VS Code 主题。默认跟随 VS Code 的中英文显示语言,其他语言使用简体中文。也可以在设置中为 wpfCanvas.language 指定语言,重新打开 XAML 标签页后生效。 |
预览 WPF 项目
在受信任的 VS Code 工作区打开包含 .csproj 的项目文件夹。多项目方案建议打开包含各项目的共同上级文件夹。插件会查找视图的相对资源字典、App.xaml 中的应用资源以及工作区内的引用项目资源,支持 StaticResource 和 DynamicResource。手动选择的资源库仍可提供共享样式。
预览自定义 UserControl、控件或转换器时,先生成项目,在预览提示中选择 启用项目代码。插件使用项目已有的 Windows x64 兼容 .NET WPF 编译输出及其依赖。启用后,控件的构造函数和其他项目代码可能执行。可在命令面板通过 WPF Canvas: 停用工作区项目代码 撤销授权。重新生成项目或修改资源后,可在工具栏单击 刷新预览。
启用项目代码后,预览器还会读取本地程序集的 XAML 命名空间定义,例如 GongSolutions 的拖放附加属性。引用路径来自现有编译输出、项目 HintPath、NuGet 已还原的运行时程序集和可找到的 Revit 安装。启用前或可选附加属性暂不可用时,画布可继续预览,并可展开 预览依赖详情。
转换器缺失或暂时无法加载时,画布仍会显示。受影响的绑定使用 XAML 中的 FallbackValue 或 WPF 属性默认值,源码与实际 Revit 程序的转换规则保持在原文件中。
插件使用独立的原生 WPF 进程渲染当前编辑器内的 XAML,支持样例绑定数据。依赖应用启动初始化、实际业务服务或旧版 .NET Framework 专用程序集的界面可能仍需要 Visual Studio 的设计器。
English
Open a XAML file
Open a local .xaml file from the VS Code Explorer. The native WPF preview is on the left and the XAML source editor is on the right. Press Ctrl+S to save to the current file. To try a local package, select Install from VSIX… in the Extensions view's … menu and choose the Windows x64 VSIX.
If a different editor is already associated with XAML, right-click the tab, select Reopen Editor With…, and choose WPF Canvas. The same menu lets you configure the default editor.
Features and modes
| Feature |
What it does |
| Live Preview |
Refresh the WPF preview as the source changes. Zoom in and inspect hover and disabled states. When XAML has an error, the editor keeps the last valid preview and reports the location when available. |
| Edit Mode |
Drag common WPF controls from the toolbox, move controls on the canvas, or stage a draft outside the window. Edit the selected control in the property inspector to apply changes to the XAML. |
| Format XAML |
Select Format in the toolbar or press Shift+Alt+F in the source editor to format indentation and attribute layout. Undo the edit or save it with Ctrl+S. |
| Preview Rules |
XAML design-time values such as d:Text, d:DataContext, and d:ItemsSource take precedence. Fill remaining preview bindings with sample text, list rows, or your own JSON. |
| Resource Library |
Choose a local folder and scan its subfolders for .xaml files whose root is a WPF ResourceDictionary. The scan skips demo windows and other files and opens library resources read-only. |
| Theme and Language |
The preview tools and editor follow the VS Code theme. The interface follows the VS Code display language for English and Simplified Chinese, with Simplified Chinese for other locales. To override it, set wpfCanvas.language and reopen the XAML tab. |
Preview a WPF project
Open the folder containing the .csproj in a trusted VS Code workspace. For solutions with multiple projects, open their common parent folder. WPF Canvas can resolve dictionaries relative to the view, application resources in App.xaml, and referenced project resources within the workspace. It supports StaticResource and DynamicResource. The optional shared resource folder remains available.
To preview custom user controls, controls, or converters, build the WPF project and select Enable Project Code when prompted. WPF Canvas uses the existing Windows x64–compatible .NET WPF build output and dependencies. Control constructors and other compiled project code can run once enabled. Use WPF Canvas: Disable Workspace Project Code from the Command Palette to revoke access. Select Refresh Preview after rebuilding or updating resources.
When project code is enabled, the preview also resolves XML namespaces registered by local libraries, such as GongSolutions drag-and-drop properties. Reference paths can come from build output, project HintPath entries, restored NuGet runtime assets, or a discovered Revit installation. Expand Preview dependency details when optional attached properties are skipped.
When a converter is missing or cannot be loaded, the preview still renders. Affected bindings use the configured FallbackValue or the WPF property default. Converter fallback applies only to the preview.
WPF Canvas renders the current editor text in a separate native WPF process and supports sample binding data. Views that depend on application startup, live application services, or legacy .NET Framework–specific assemblies may still require the Visual Studio designer.
开发与测试 / Development and testing
开发环境为 Windows x64、Node.js 22 LTS、Git 和 .NET 10 SDK。
Develop on Windows x64 with Node.js 22 LTS, Git, and the .NET 10 SDK.
npm.cmd ci
npm.cmd run build
code.cmd .
按 F5 启动扩展开发宿主,再打开 webview/samples/bindings-demo.xaml。
Press F5 to launch the Extension Development Host, then open webview/samples/bindings-demo.xaml.
| 命令 / Command |
用途 / Purpose |
npm run lint |
静态检查 / Lint source files |
npm run test:native |
测试原生预览、编辑和资源库 / Test native rendering, editing, and resource libraries |
npm test |
当前稳定版 VS Code 宿主测试 / Test the current stable VS Code host |
npm run test:minimum |
最低支持版本的宿主测试 / Test the minimum supported VS Code host |
npm run test:installed |
使用 WPF_CANVAS_VSCODE_EXECUTABLE 指定宿主 / Test the host selected by WPF_CANVAS_VSCODE_EXECUTABLE |
npm run package:vsix |
打包 Windows x64 VSIX / Package a Windows x64 VSIX |
源码迁移来自本地 WPF Canvas 的提交 4340e88。扩展在本仓库独立构建,发布流程见 docs/RELEASING.md。
The editor and native preview were ported from local WPF Canvas commit 4340e88. The extension builds independently from this repository. See docs/RELEASING.md for releases.