Bug 47703 - IValueConverter not working if we set XamlCompilation attribute
Summary: IValueConverter not working if we set XamlCompilation attribute
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-23 10:50 UTC by Divakar
Modified: 2017-01-24 15:15 UTC (History)
3 users (show)

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


Attachments
IValueConverter not working if we set XamlCompilation attribute (65.00 KB, image/png)
2016-11-23 10:50 UTC, Divakar
Details
IValueConverter not working if we set XamlCompilation attribute (292.85 KB, application/x-zip-compressed)
2016-11-23 10:51 UTC, Divakar
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 Divakar 2016-11-23 10:50:58 UTC
Created attachment 18611 [details]
IValueConverter not working if we set XamlCompilation attribute

Attached sample will replicate the issue.

If we set XamlCompilation attribute to the main page like in the attached screenshot, IValueConverter bound to the custom view not hit.

If we remove that compilation from the mainpage.cs, then the converter working properly.
Comment 1 Divakar 2016-11-23 10:51:50 UTC
Created attachment 18612 [details]
IValueConverter not working if we set XamlCompilation attribute
Comment 2 Stephane Delcroix 2016-12-02 20:50:08 UTC
Your sample works just fine:
 - with the XF version linked from your project,
 - XF 2.3.3
 - our master

provided that you fix the signature of 

    public Binding DisplayBinding

into

    public BindingBase DisplayBinding

As its signature shows, the BindingExtension returns a BindingBase, and BindingBase is not assignable to Binding.
Comment 3 Divakar 2017-01-09 05:50:49 UTC
Hi Stephane Delcroix,

As you have mentioned earlier “ the BindingExtension returns a BindingBase, and BindingBase is not assignable to Binding”. I need some clarification on this. I have tried to do the same from code behind as shown below, which works well. As we all know that Binding class is derived from BindingBase(Abstract class).

// Code-behind

customView.DisplayBinding = new Binding("Name", BindingMode.OneWay, new Converter()); // where customView is the view loaded in the Xaml. Please refer the sample which we attached earlier.


When xaml binding also returns the same Binding class (which is derived from binding base), then why am I facing such conflict?  Is Xamarin internally using separate binding class for xaml alone. This issue is getting reproduced only if we set compilation property for xaml [XamlCompilation(XamlCompilationOptions.Compile)].    


Regards,
Divakar
Comment 4 Stephane Delcroix 2017-01-10 08:34:51 UTC
the `{Binding}` markup extension doesn't always generate a `Binding` object, that's why it's return value is `BindingBase`.

as you like c# equivalence, this is equivalent to this:

customView.DisplayBinding = ((BindingBase)new Binding("Name", BindingMode.OneWay, new Converter()));

and that won't compile if `DisplayBinding` is of type `Binding`.

That's for the explanation

Does the proposed fix _not_ works for you ? I don't get why this issue had been reopened. I'm reclining it.
Comment 5 Malcolm 2017-01-24 07:37:03 UTC
Hi Stephane,

I'd like to add that it's odd that there are two different outputs/behaviors depending on whether xaml compilation is turned on or not. 

Why is there a difference in logic between compile time xaml compilation and run time compilation? This would make sense if it never worked. But it does work when excluding the xaml compilation attribute.

What are the conditions or criteria for the {Binding} markup extension to return either a Binding object or a BindingBase object?
Comment 6 Stephane Delcroix 2017-01-24 08:39:37 UTC
I could make it fail with XamlC off as well
Comment 7 Malcolm 2017-01-24 15:15:24 UTC
Hi Stephane,

It makes good sense to type it as BindingBase (abstract class) rather than Binding (concrete implementation) as it conforms to dependency inversion. I fully agree with you on that.

What is confusing is that turning XamlC off works - it only breaks when you explicitly typecast it as you did above - essentially bypassing XamlC. Does the code that performs the xaml runtime evaluation also evaluate the code to as you've shown? From testing it does not appear to do so. That appears to be the real issue.