【问题标题】:Xamarin Forms OnPlatform in XamlXamarin 在 Xaml 中形成 OnPlatform
【发布时间】:2015-04-14 20:01:38
【问题描述】:

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

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

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

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

    【讨论】:

    • 糟糕!我的错。我复制了你的代码作为我的答案,它有一个语法错误。不要包含那个 元素;只需将 OnPlatform 元素作为 BackgroundColor 的子元素(也已编辑答案)。
    • 哦,我正在挖掘婴儿日记应用程序的想法。希望我有一个给我的孩子...
    • 谢谢。啊,我应该尝试一下,我没有意识到!我实际上开始用代码编写我的页面。我是否正确地说您没有在 VS 中获得用于 Xamarin Xaml 页面的可视化设计器?如果没有,我看不出用 xaml 编写页面比代码有任何优势。糟糕,我不是有意将应用程序的名称留在那里……现在取出来。 Xamarin 是一个巨大的学习曲线......我的导航昨晚(阅读后)使用消息传递......
    • 我个人更喜欢 XAML 而不是代码;层次结构的可视化更容易,它还可以提醒开发人员“这是 UI;不允许任何逻辑。”
    • 这很好,但是,MVVM 也可以帮助您做到这一点。我目前正在使用 Mvvm Light!到目前为止真的很喜欢。正如我所说,这对我来说是全新的!
    【解决方案2】:

    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;
     }
    

    【讨论】:

    【解决方案3】:

    你也可以这样做:

    <ContentView>
            <OnPlatform x:TypeArguments="View">
                <On Platform="iOS">
                    <Label Text="iOS" />
                </On>
                <On Platform="Android">
                    <Label Text="Android" />
                </On>
            </OnPlatform>
        </ContentView>
    

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 2017-02-09
      • 2017-12-14
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多