0

I am trying to bind data in xamarin.forms using xaml. But the problem is that it doesn't work. My xaml code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:BindingSample"
         x:Class="BindingSample.Test">
  <ContentPage.Resources>
    <ResourceDictionary>
      <OnPlatform x:Key="statusToImgConverter" 
            x:TypeArguments="local:StatusToImgSourceConverter"
            iOS="null"
            Android="local:StatusToImgSourceConverterDroid"
            WinPhone="local:StatusToImgSourceConverterUWP"/>
    </ResourceDictionary>
  </ContentPage.Resources>

  <ContentPage.BindingContext>
    <local:TestViewModel/>
  </ContentPage.BindingContext>

  <StackLayout>
    <Grid RowSpacing="1" ColumnSpacing="1">
      <Image Source="{Binding Enabled, Converter={StaticResource statusToImgConverter}}" />
      <Label Text="{Binding Enabled}" Grid.Column="1" Grid.ColumnSpan="4" />
    </Grid>
    <Button x:Name="enableBtn" Text="enable"/>
  </StackLayout>
</ContentPage>

View model class looks like this (I want to use Fody for INotifyPropertyChanged code generation)

[ImplementPropertyChanged]
class TestViewModel
{
    public TestViewModel()
    {
        Enabled = true;
    }

    bool Enabled { get; set; }
}

But the problem is that image and label are not shown. Commenting out ResourceDictionary and Image do not do any effect (so the problem is not in converters).

What am I doing wrong?

Edit - added code for content page. Button is shown, but image and label are not.

public partial class Test : ContentPage
{
    public Protection()
    {
        InitializeComponent();

        enableBtn.Clicked += OnEnableClicked;
    }

    private void OnEnableClicked(object sender, EventArgs e)
    {
        //do nothing here at this time
    }

}
Alexey Vukolov
  • 223
  • 1
  • 3
  • 13

1 Answers1

0

Finally I got the source of error. The right code should be:

<ResourceDictionary>
    <OnPlatform x:TypeArguments="local:StatusToImgSourceConverter" x:Key="statusToImgConverter">
      <OnPlatform.Android>
        <local:StatusToImgSourceConverterDroid/>
      </OnPlatform.Android>
      <OnPlatform.WinPhone>
        <local:StatusToImgSourceConverterUWP/>
      </OnPlatform.WinPhone>
    </OnPlatform>          
</ResourceDictionary>

In Xaml. And Enabled property must be explicitly declared public.

Alexey Vukolov
  • 223
  • 1
  • 3
  • 13