Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Enabling Xaml compile on a Xaml file that contains a markup extension behave differently than when the Xaml is processed at run-time.
To reproduce the behavior:
In the attached project, in App.cs if you remove the XamlCompilation attribute the app runs just fine.
Leaving the Xaml Compile causes an InvalidCastException to be thrown.
After some testing it appears that the Xaml Compile attempts to directly cast the the returned value from the markup extension while the run-time Xaml will attempt to convert the returned value to the correct type.
This problem is amplified, if the mark up extension returns a Binding. It also attempts to cast the binding rather than calling SetBinding (which is what the run-time Xaml does).
Created attachment 14556 [details]
Example project showing Xaml Compile failure.
We're aware of that casting limitation in XamlC right now, and are working on a fix. This is quite hard as it can't be reliably determined at compile time.
That's why all our markup extensions implements IMarkupExtension<T> instead of IMarkupExtension, so the return type is known at compile time. You can do that as well:
public class TowelExtension : IMarkupExtension<double>
public double ProvideValue(IServiceProvider serviceProvider)
object IMarkupExtension.ProvideValue (IServiceProvider serviceProvider)
return (this as IMarkupExtension<double>).ProvideValue (serviceProvider);
Thank you for the feedback. This workaround is good enough to get our app compiling its XAML.
I am hopeful that a full solution is in the works that does not require us to exactly type our return value will be out soon.