Xamarin在Xaml中形成OnPlatform [英] Xamarin Forms OnPlatform in Xaml
问题描述
我有以下C#代码:
var footer = new StackLayout()
{ BackgroundColor = Device.OnPlatform(Color.FromRgb(225, 240, 251), Color.FromRgb(225, 240, 251), Color.Black),
};
如何将其转换为Xamarin Xaml,更重要的是,将其转换为FromRgb的设备平台规范?
How can I translate that into Xamarin Xaml, more importantly the Device Platform specifics for the FromRgb?
到目前为止,我有以下XAML ...再次,是FromRgb困扰了我.
I have the following XAML so far... again, it's the FromRgb that's stumping me.
<StackLayout.BackgroundColor>
<Color>
<OnPlatform x:TypeArguments="Color"></OnPlatform>
</Color>
</StackLayout.BackgroundColor>
更新14/02/2015
UPDATE 14/02/2015
我刚刚尝试了以下答案,但有以下内容,但它没有更新iOS或Windows Phone的背景色.如果我将背景色添加到根StackLayout元素中,则可以使用...:
I've just tried the answer below and I have the following but it's not updating the background colour for either iOS or Windows Phone. If I add the Background Color to the root StackLayout element it works...:
<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)进行转换.
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>
这篇关于Xamarin在Xaml中形成OnPlatform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!