CodeGrue
CodeGrue

Reputation: 5933

OnPlatform in NavigationPage using XAML

Given this original code, I'm trying to hide the icon on Android:

<NavigationPage Title="Page" Icon="page_icon.png">
    <x:Arguments>
        <views:MyPage />
    </x:Arguments>
</NavigationPage>  

I have attempted to use OnPlatform many different ways and cannot get it to work. Here are some things I have tried:

<NavigationPage Title="Page">          
    <NavigationPage.Icon>     
        <OnPlatform x:TypeArguments="x:String">     
            <OnPlatform.iOS>page_icon.png</OnPlatform.iOS> 
            <OnPlatform.Android>{x:Null}</OnPlatform.Android>    
        </OnPlatform>    
    </NavigationPage.Icon>     
    <x:Arguments>
        <views:MyPage />
    </x:Arguments>
</NavigationPage> 

This produced the error:

The given key was not present in the dictionary.

I also tried this:

<NavigationPage Title="Rater">
    <OnPlatform 
        x:Key="Icon"
        x:TypeArguments="x:String" 
        iOS="tab_feed.png"
        Android="{x:Null}"
    />
    <x:Arguments>
        <views:MyPage />
    </x:Arguments>
</NavigationPage>

Which runs, but no icons show up on iOS.

Upvotes: 0

Views: 890

Answers (2)

Yuri S
Yuri S

Reputation: 5370

Not sure how you deal with icons, it may be a separate question but OnPlatform was redesigned. Here is how you suppose to use it now:

<OnPlatform x:Key="SwitchOnColor"  x:TypeArguments="Color" >
    <On Platform="iOS,Android" >#0000FF</On>
    <On Platform="UWP">#FF0000</On>
</OnPlatform>

Upvotes: 1

halfer
halfer

Reputation: 20467

(Posted on behalf of the OP).

With the help of Yuri S's example, I was able for create markup that worked as intended on iOS and Android. This is it.

    <NavigationPage.Icon>
        <OnPlatform x:Key="SwitchOnIcon" x:TypeArguments="FileImageSource">
            <On Platform="iOS">tab_feed.png</On>
        </OnPlatform>
    </NavigationPage.Icon>  

Upvotes: 1

Related Questions