Bug 37622 - Xaml Compile behaves differently when processing markup extensions
Summary: Xaml Compile behaves differently when processing markup extensions
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-12 17:33 UTC by Kevin B
Modified: 2016-01-14 21:25 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Example project showing Xaml Compile failure. (196.32 KB, application/x-zip-compressed)
2016-01-12 17:41 UTC, Kevin B
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED ANSWERED

Description Kevin B 2016-01-12 17:33:47 UTC
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).


Xamarin.Forms v2.0.0.6490
Comment 1 Kevin B 2016-01-12 17:41:03 UTC
Created attachment 14556 [details]
Example project showing Xaml Compile failure.
Comment 2 Stephane Delcroix 2016-01-14 12:24:09 UTC
Hi Kevin,

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)
        {
            return 42;
        }

        object IMarkupExtension.ProvideValue (IServiceProvider serviceProvider)
        {
            return (this as IMarkupExtension<double>).ProvideValue (serviceProvider);
        }
    }
Comment 3 Kevin B 2016-01-14 21:25:55 UTC
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.