Bug 25189 - MarkupExtension not resolving bindings
Summary: MarkupExtension not resolving bindings
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-12-09 11:17 UTC by J.C.Ködel
Modified: 2017-03-20 07:14 UTC (History)
6 users (show)

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

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 NOT_ON_ROADMAP

Description J.C.Ködel 2014-12-09 11:17:56 UTC
I have an IMarkupExtension with two properties: Text (string), which is also [ContentProperty] and a int? IsPluralOn (which is optional, hence the ?).

This works:
Title="{languages:Translate VM_Stopped, IsPluralOn=5}"

But this don't:
Title="{languages:Translate VM_Stopped, IsPluralOn={Binding Source={x:Reference Self}, Path=Model.VMs.Running}}"

In the first line, the IsPluralOn has the value 5 (int?)

In the second line, an exception is thrown:

Object of type 'Xamarin.Forms.Binding' cannot be converted to type 'System.Nullable`1[System.Int32]'.


Fix: The binding should be resolved before passing the value to the IMarkupExtension.ProvideValue method.
Comment 1 Keith Rome 2015-02-28 19:06:19 UTC
Hi J.C., that is normal behavior for XAML in WPF/Silverlight so I assume it would be normal behavior for Xamarin Forms as well.

To make IsPluralOn bindable in that way, then your markup extension needs to inherit from BindableObject and the property needs to be implemented as a bindable property. It would look something like this:

public class LanguagesExtension: BindableObject, IMarkupExtension
{
    //... implement the markup extension stuff too

    public static BindableProperty IsPluralOnProperty = BindableProperty.Create("IsPluralOn", typeof(int?), typeof(LanguagesExtension), null);

    public int? IsPluralOn
    {
        get { return (int?)GetValue(IsPluralProperty); }
        set { SetValue(IsPluralProperty, value); }
    }
}

Now, if *that* doesn't work then there is indeed a problem :)
Comment 2 Brent Anderson 2017-03-20 07:14:42 UTC
It appears the suggested BindableProperty approach does not actually work. i.e. the setter never gets called.