VS2022新建VSIX项目出现XAML绑定失败问题求助
VS2022 VSIX项目调试时的XAML绑定错误问题及解决办法
最近第一次在VS2022里搭建VSIX扩展项目,严格跟着官方的Hello World步骤操作:
- 右键项目(注意不是解决方案),选择「添加→新建项」
- 在模板里选「扩展性」分类,添加Command项
- 按F5启动调试时,输出窗口弹出了一堆XAML绑定错误,我的Command代码如下:
using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using System; using System.ComponentModel.Design; using System.Globalization; using System.Threading; using System.Threading.Tasks; using Task = System.Threading.Tasks.Task; namespace VSIXProject2 { internal sealed class Command1 { public const int CommandId = 0x0100; public static readonly Guid CommandSet = new Guid("f83fb9b7-23b6-4605-bc93-74df2c22ec53"); private readonly AsyncPackage package; private Command1(AsyncPackage package, OleMenuCommandService commandService) { this.package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); var menuCommandID = new CommandID(CommandSet, CommandId); var menuItem = new MenuCommand(this.Execute, menuCommandID); commandService.AddCommand(menuItem); } public static Command1 Instance { get; private set; } private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider { get { return this.package; } } public static async Task InitializeAsync(AsyncPackage package) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); OleMenuCommandService commandService = await package.GetServiceAsync(typeof (IMenuCommandService)) as OleMenuCommandService; Instance = new Command1(package, commandService); } private void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName); string title = "Command1"; VsShellUtilities.ShowMessageBox( this.package, message, title, OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } } }
调试时的错误输出如下:
System.Windows.Data Error: 17 : Cannot get 'CurrentState' value (type 'Int32') from '' (type 'PolicyBasedDataSource'). BindingExpression:Path=CurrentState; DataItem='PolicyBasedDataSource' (HashCode=8713795); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' BindingExpression:Path=ToolTip; DataItem='GlyphButton' (Name='MaximizeButton'); target element is 'GlyphButton' (Name='MaximizeButton'); target property is 'Name' (type 'String') System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' BindingExpression:Path=ToolTip; DataItem='GlyphButton' (Name='CloseButton'); target element is 'GlyphButton' (Name='CloseButton'); target property is 'Name' (type 'String') System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' BindingExpression:Path=Content; DataItem='Button' (Name=''); target element is 'Button' (Name=''); target property is 'Name' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Text' property not found on 'object' ''DataSource' (HashCode=60105532)'. BindingExpression:Path=Text; DataItem='DataSource' (HashCode=60105532); target element is 'VsToolBar' (Name=''); target property is 'Name' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''DataSource' (HashCode=60105532)'. BindingExpression:Path=Items; DataItem='DataSource' (HashCode=60105532); target element is 'VsToolBar' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Error: 40 : BindingExpression path error: 'PlacementSlot' property not found on 'object' ''DataSource' (HashCode=60105532)'. BindingExpression:Path=PlacementSlot; DataItem='DataSource' (HashCode=60105532); target element is 'VsToolBar' (Name=''); target property is 'Band' (type 'Int32') System.Windows.Data Error: 40 : BindingExpression path error: 'BandIndex' property not found on 'object' ''DataSource' (HashCode=60105532)'. BindingExpression:Path=BandIndex; DataItem='DataSource' (HashCode=60105532); target element is 'VsToolBar' (Name=''); target property is 'BandIndex' (type 'Int32') System.Windows.Data Error: 40 : BindingExpression path error: 'IsVisible' property not found on 'object' ''DataSource' (HashCode=60105532)'. BindingExpression:Path=IsVisible; DataItem='DataSource' (HashCode=60105532); target element is 'VsToolBar' (Name=''); target property is 'Visibility' (type 'Visibility')
我在社区找到了类似问题的讨论,现在想请教这个问题的原因和解决办法。
问题原因
这是Visual Studio 2022早期版本(17.0.x至17.2.x左右)存在的已知UI框架绑定bug,属于VS调试宿主环境的问题,和你的代码完全无关——你写的Command代码完全符合官方规范,没有任何错误。
解决办法
- 优先更新VS到最新稳定版本:微软在后续的版本更新中已经修复了这个绑定错误,你可以通过VS顶部菜单的「帮助→检查更新」升级到最新版,更新后这些错误就会消失。
- 临时规避(无法更新时):这些错误仅会出现在调试输出中,不会影响你的VSIX扩展功能正常运行,你可以直接忽略这些错误,继续开发和调试项目。
内容的提问来源于stack exchange,提问作者mikus
相关产品推荐
相关产品推荐

