VS2010 Custom MarkupExtension

2019-03-22 16:33发布

问题:

We have just switched to vs2010 from vs2008, and our projects compile and run well without any problem. However, the xaml designer has some errors. Here is one of the errors hope someone can provide a solution.

We have a custom MarkupExtension, and we use it in xaml to get certain resources. Something like this:

<Button Style="{l:GetResource Key=MyButtonStyle}" />

I know we can use StaticResource or DynamicResource. However we have our reasons to use the custom markupextension.

The error message from the designer is:

'GetResourceExtension' is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types.

My question is that the code works fine in vs2008, and also runs fine in 2010, how to make the 2010 designer work?

回答1:

I've written a few Markup Extensions and although the code runs I get Xaml Error's like the one you get all the time. Assuming you have valid code I found the only way to get rid of the errors was via the rebuild option and then running the app.

Of course the problem is if you change it again or move it the errors seem to come back. My assumption is the static compile cannot fully resolve whatever your markup is doing and is throwing the error based on this.

In my case I was writing a method binder and because the object was null until runtime I ended up having to use new Object(); and swapping it out at runtime. It's hacky but the compile error were driving me nuts.

The xaml parser seems to be better in 2012 but even then there are the odd errors.