Xamarin Forms OnPlatform in Xaml

前端 未结 2 677
慢半拍i
慢半拍i 2021-01-18 14:41

I have the following C# code:

var footer = new StackLayout()
            { BackgroundColor = Device.OnPlatform(Color.FromRgb(225, 240, 251), Color.FromRgb(22         


        
相关标签:
2条回答
  • 2021-01-18 14:50

    You're almost there. The default converter takes care of converting the color from either a named color (e.g. White, Red, etc.) or a hex color (e.g.: #FF0000).

    <StackLayout.BackgroundColor>
        <OnPlatform x:TypeArguments="Color">
            <OnPlatform.iOS>#FF0000</OnPlatform.iOS>
            <OnPlatform.Android>#00FF00</OnPlatform.Android>
        </OnPlatform>
    </StackLayout.BackgroundColor>
    
    0 讨论(0)
  • 2021-01-18 15:07

    Newer version syntax of OnPlatform is slightly different

    In Xaml:

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

    In code:

     switch (Device.RuntimePlatform)
     {
         case "iOS":
         break;
         case "Android":
         break;
         case "UWP":
         break;
         default:
         break;
     }
    
    0 讨论(0)
提交回复
热议问题