Search code examples
buttonwindows-phone-8xamarin.formsxamlparseexception

No Property of name Image found for Button on Windows Phone


I'm getting an issue I don't understand. I have a Button with an image defined with the following code :

<Button Image="SearchFilterIcon.png"
              Grid.Row="0"
              Grid.Column="1"
              Clicked="OnButtonFilterClicked" />

This works well on Android. The image is displayed on my button but when I launch the Windows Phone application, I get a XamlParseException which says that : No Property of name Image found.

How is it possible? The Button widget isn't the same on Android and Windows Phone?


Solution

  • If you do the following:-

    Button objButton1 = new Button();
    objButton1.Image = (FileImageSource)ImageSource.FromFile("testImage1.png");
    objStackLayout.Children.Add(objButton1);
    this.Content = objStackLayout;
    

    Then it will work (via code-behind).

    The Button control always had the Image property, even in Xamarin.Forms v1.2.2x, so this is not a new property introduced and nothing to do with having the latest packages installed.

    As a workaround perhaps you should consider giving the XAML Button a x:Name as in:-

    <Button x:Name="myButton1"/>
    

    And then assign the image from code-behind:-

    myButton1.Image = (FileImageSource)ImageSource.FromFile("testImage1.png");
    

    Update 1

    This was a case of very old libraries being used (v1.0.6186). Once the project is reupdated to the latest binaries for v1.2.3x, then this works fine.