首页 文章

Xamarin在Xaml中形成OnPlatform

提问于
浏览
4

我有以下C#代码:

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

如何将其转换为Xamarin Xaml,更重要的是将其转换为FromRgb的设备平台细节?

到目前为止,我有以下XAML ......再次,这是让我感到困惑的FromRgb .

<StackLayout.BackgroundColor>
    <Color>
        <OnPlatform x:TypeArguments="Color"></OnPlatform>
    </Color>
</StackLayout.BackgroundColor>

更新2015年2月14日

我刚刚尝试了下面的答案,我有以下内容,但它没有更新iOS或Windows Phone的背景颜色 . 如果我将背景颜色添加到根StackLayout元素,它可以工作......:

<StackLayout HorizontalOptions="FillAndExpand">
    <StackLayout.BackgroundColor>
      <Color>
        <OnPlatform x:TypeArguments="Color">
          <OnPlatform.WinPhone>#51C0D4</OnPlatform.WinPhone>
          <OnPlatform.iOS>#51C0D4</OnPlatform.iOS>
        </OnPlatform>
      </Color>
    </StackLayout.BackgroundColor>
    <Label Text=""></Label>
    <StackLayout Orientation="Horizontal"  VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand">
      <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
      <Button Text="Login"></Button>
      <Button Text="Sign Up"></Button>
      </StackLayout>
    </StackLayout>
  </StackLayout>

2 回答

  • 1

    你快到了 . 默认转换器负责转换颜色来自命名颜色(例如白色,红色等)或十六进制颜色(例如:#FF0000) .

    <StackLayout.BackgroundColor>
        <OnPlatform x:TypeArguments="Color">
            <OnPlatform.iOS>#FF0000</OnPlatform.iOS>
            <OnPlatform.Android>#00FF00</OnPlatform.Android>
        </OnPlatform>
    </StackLayout.BackgroundColor>
    
  • 3

    OnPlatform的较新版本语法略有不同

    在Xaml中:

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

    在代码中:

    switch (Device.RuntimePlatform)
     {
         case "iOS":
         break;
         case "Android":
         break;
         case "UWP":
         break;
         default:
         break;
     }
    

相关问题