我在我的 VSTO 项目中遇到一个奇怪的错误,其中 XAML 文件导致无关代码中的构建错误。
这是我做的
创建一个新的 Excel 2010 加载项项目(此处命名为 TestAddIn)
修改
ThisAddIn_Startup
为阅读private void ThisAddIn_Startup(object sender, System.EventArgs e) { Worksheet w = Globals.ThisAddIn.Application.Workbooks[1].Sheets[1]; w.Rows[1].Font.Bold = true; }
构建项目。构建应该会成功。
将 WPF 用户控件作为新项添加到项目中。您还需要添加
System.Xaml
作为项目引用。再次构建项目。构建应该会成功。
修改UserControl1.xaml为read
<UserControl x:Class="TestAddIn.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Test="clr-namespace:TestAddIn" <!-- Add this line --> > <Grid> </Grid> </UserControl>
构建项目。构建失败并显示错误消息
'object' does not contain a definition for 'Font' and no extension method 'Font' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
在 UserControl1.xaml 中删除或添加命名空间行将导致构建成功或失败。
我真的很困惑,因为 WPF 文件与加载项文件没有直接关系。我想一定是链接有问题?
我意识到我可以通过显式转换 (((Range)w.Rows[1]).Font.Bold = true
) 来解决这个问题。但我真的不想在所有地方都这样做。
我该如何解决这个问题,以便我的 VSTO 项目中可以有一个 WPF 文件?
最佳答案
我找到了一种方法让 VS2012 的用户可以使用它。
- 转到“解决方案资源管理器”->“引用”文件夹并选择对“Microsoft.Office.Interop.Excel”的引用。
- 在其属性中,将“嵌入互操作类型”设置为 False 并编译。错误应该仍然出现。 现在再次将其设置回 True 并编译。瞧!
此错误已在 VS2013(更新 3)中修复。
关于c# - 添加 WPF 命名空间导致 VSTO 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583384/