0

I created a View(BlankScreen) which has Image and in the code behind of the view I created a BindableProperty Icon.

View's Xaml Code:

<ContentView
    x:Class="Demo.CustomViews.BlankScreen"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentView.Content>
        <StackLayout>
            <Label Text="Hello Xamarin.Forms!" />
            <Image x:Name="image"/>
        </StackLayout>
    </ContentView.Content>
</ContentView>

View's Code Behind:

public partial class BlankScreen : ContentView
    {
        public BlankScreen ()
        {
            InitializeComponent ();
        }

        public static readonly BindableProperty IconProperty =
            BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(BlankScreen), null);

        public ImageSource Icon
        {
            get => (ImageSource)GetValue(IconProperty);
            set => SetValue(IconProperty, value);
        }
    }

Then I used this view to Page so I included this view with BindableProperty Icon.

<customviews:BlankScreen Icon="chat" />

But Problem is chat icon not able to set on my view which was passed from the parent page's view.

So how can i solve this issue, please help?

2 Answers2

0

The reason I think it is not working is that you don't seem to be setting it up

Your bindable property should look something like below:

 public static readonly BindableProperty IconProperty =
        BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(BlankScreen), null, propertyChanged: IconChanged);

    public ImageSource Icon
    {
        get => (ImageSource)GetValue(IconProperty);
        set => SetValue(IconProperty, value);
    }

Once you do that add the below method and update the image:

  private static void IconChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BlankScreen screen= bindable as BlankScreen ;
        screen.image.Source= newValue.ToString();
    }

Let me know in case of queries

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
0

I find there is image in BlankScreen, but you don't set source for it. I bind Icon for this image.

 <ContentView.Content>
    <StackLayout>
        <Label Text="Hello Xamarin.Forms!" />
        <Image x:Name="image" Source="{Binding Icon}" />
    </StackLayout>
</ContentView.Content>

Then create propertyChanged event:

public partial class BlankScreen : ContentView
{
    public static readonly BindableProperty IconProperty =
        BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(BlankScreen), defaultBindingMode: BindingMode.TwoWay,
                                                                   propertyChanged: (bindable, oldVal, newVal) =>
                                                                   {
                                                                       var page = (BlankScreen)bindable;
                                                                       page.image.Source = (ImageSource)newVal;
                                                                   });

    public ImageSource Icon
    {
        get => (ImageSource)GetValue(IconProperty);
        set => SetValue(IconProperty, value);
    }
    public BlankScreen ()
    {
        InitializeComponent ();

    }
}

Finally, I use this custom view in Contentpage.

 <ContentPage.Content>
    <StackLayout>
        <customview:BlankScreen Icon="a1.jpg" />
    </StackLayout>
</ContentPage.Content>

it works fine.

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16