Add background image to button.

PrawalGuptaPrawalGupta USMember ✭✭

This is my code where I want add image background for button-

<ListView x:Name="listViewTest" BackgroundColor="#CDBA96" ItemSelected="OnItemSelected" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                            <Label Text="{Binding .}" VerticalTextAlignment="Center" FontSize="Medium" />
                            <Label Text="Label 2" VerticalTextAlignment="Center" FontSize="Small" />
                        </StackLayout>
                        <Button  HorizontalOptions="EndAndExpand" FontSize="30" **_Image=".....????......"_**  Clicked="OnCellClicked" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

In the above code I need to specify path for image in button control. But I am not getting image in output. I am using Xamarin Studio on Mac machine and this is Xamarin.Forms.PCL project. So where should I place image and how to set a path. Keep in mind If I want to change background image dynamically, it should be possible. Thanks.

Best Answers

Answers

  • StefanAStefanA NLMember
    edited April 2016

    Button.Image expects a FileImageSource so you can't embed it in the PCL.

    • In iOS you need to place it in Resources with build action set to BundleResource.
    • In Android place it in Drawables with build action set to AndroidResource.
    • In Windows place it in root of the project with build action set to Content.

    Then in PCL you can use "image.png" as path, programmatically or in XML.

    For more information you can follow this tutorial: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Local_Images

Sign In or Register to comment.