xaml - Xamarin 在 Xaml 中形成 OnPlatform

标签 xaml xamarin.forms

我有以下 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>

更新 14/02/2015

我刚刚尝试了下面的答案,我有以下答案,但它没有更新 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>

最佳答案

您快到了。默认转换器负责从命名颜色(例如白色、红色等)或十六进制颜色(例如:#FF0000)转换颜色。

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

关于xaml - Xamarin 在 Xaml 中形成 OnPlatform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505351/

相关文章:

c# - 使用带有 XAML 绑定(bind)的 StringFormat 将文件夹注入(inject)图像路径

c# - Xamarin.Forms 使用 MVVM 在屏幕之间传输数据

xamarin - 收到通过 xamarin.forms 中的 FCM 发送的通知,但图像未显示在系统托盘中

xamarin - 内容页面的顶部是否始终有一个标题区域?

xamarin - 汉堡包菜单 Xamarin Forms (MasterDetailPage)

c# - 使用 .NET 标准库的 Xamarin.Android 上的 JsonSerializationException 'Unable to find a constructor'

c# - ListView 上的水平滚动

c# - 为什么我的每一个设计 View 中都会出现 XAML 设计器错误?

c# - HttpClient PostAsync 不返回

wpf - 表格列宽和水平对齐方式