Search code examples
xamarin.formsandroid-toolbar

How to bind image icon on ToolbarItem on Platform tag


How to bind an image icon on the ToolbarItem OnPlatform tag in Xamarin.Forms

This is my code:

<ToolbarItem 
    Icon="{OnPlatform iOS='iconscalendarplus64.png', Android='iconscalendarplus64.png'}" 
    Priority="0"
    Order="Primary" 
    Command="{Binding PrikaziCommand}"

I am trying to bind images for the Android OnPlatform, but it is not working.


Solution

  • Since the Android and iOS properties of the OnPlatform are not BindableProperty

    They don't support Data Binding. Hence you cannot bind those Property.

    As an alternative you can set the platform specific image source to a property in the ViewModel and you it instead.

    XAML

    <ContentPage.ToolbarItems>
        <ToolbarItem IconImageSource="{Binding PlatformSpecificImage}"/>
    </ContentPage.ToolbarItems>
    

    View Model

    public ImageSource PlatformSpecificImage { get; set; }
    
    public ViewModel()
    {
        if(Device.RuntimePlatform == Device.Android)
        {
            PlatformSpecificImage = "android_image.png";
        }
        else
        {
            PlatformSpecificImage = "iOS_image.png";
        }
    }
    

    Hope this could help!