Adding a WPF namespace causes VSTO compile error
-
Friday, July 20, 2012 5:07 PM
I'm getting a weird error in my VSTO project wherein a XAML file is causing a build error in unrelated code.
Here is what I did
1. Create a new Excel 2010 Add-in project (here named TestAddIn)
2. Modify `ThisAddIn_Startup` to read
private void ThisAddIn_Startup(object sender, System.EventArgs e) { Worksheet w = Globals.ThisAddIn.Application.Workbooks[1].Sheets[1]; w.Rows[1].Font.Bold = true; }3. Build project. The build should succeed.
4. Add a WPF User Control as a New Item to the project. You'll also need to add `System.Xaml` as a project reference.
5. Build project again. The build should succeed.
6. Modify UserControl1.xaml to 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>7. Build project. The build fails with error message
'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?)
Removing or adding the namespace line in UserControl1.xaml will cause the build to succeed or fail.
I'm really confused by this because the WPF file has no direct relation to the add-in file. I guess there must be a linking issue?
I realize I can fix this by just doing explicit casts (((Range)w.Rows[1]).Font.Bold = true). But I don't really want to do this all over the place.
How can I fix this so that I can have a WPF file in my VSTO project?
All Replies
-
Monday, July 23, 2012 7:25 AMModerator
Hi root45,
Thanks for posting in the MSDN Forum.
Did you add the reference "System.Xaml" to your VSTO project?
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
-
Monday, July 23, 2012 1:10 PM
Hi Tom,
Yes I did add that reference. It's in step 4 of my above post.
-
Wednesday, July 25, 2012 7:09 AMModerator
Hi root45,
I noticed "Font" in the exception message. Do you know where this key word exist?
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, July 25, 2012 12:09 PM
Hi Tom,
Yes. 'Font' is a property of the 'Range; object used in step 2 of the above post.
The point is that the code in step 2 is being affected by the XAML in step 6, even though they have no relation.

