1. 第一种区分平台,在cs文件中区分, Device.OnPlatform 推荐
public UiRightViewModel()
{
Device.OnPlatform(
iOS: () =>
{
this.ThrottleViewWidth = 190 * AppSetting.Instance.HeightScale;
this.ThrottleViewHeight = 200 * AppSetting.Instance.WidthScale;
},
Android: () => {
this.ThrottleViewWidth = 190 * 3 * AppSetting.Instance.HeightScale;
this.ThrottleViewHeight = 69 * 3 * AppSetting.Instance.WidthScale;
}
);
}
2. 第二种区分平台 ,xaml文件中, ContentPage.Resources
<? xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="Page"
xmlns:local="clr-namespace:XamlSamples;assembly=XamlSamples"
x:Class="XamlSamples.ListViewDemoPage"
Title="ListView Demo Page">
<ContentPage.Resources>
<ResourceDictionary>
<OnPlatform x:Key="boxSize"
x:TypeArguments="x:Double"
iOS="50"
Android="50"
WinPhone="75" />ContentView
<!-- This is only an issue on the iPhone; Android and
WinPhone auto size the row height to the contents. -->
<OnPlatform x:Key="rowHeight"
x:TypeArguments="x:Int32"
iOS ="100"
Android="{Binding Path=Width,Source={x:Reference Page}}"
WinPhone="85" />
<local:DoubleToIntConverter x:Key="intConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ListView ItemsSource = "{x:Static local:NamedColor.All}"
RowHeight="{StaticResource rowHeight}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding = "5, 5, 0, 5"
Orientation="Horizontal"
Spacing="15">
<BoxView WidthRequest = "{StaticResource boxSize}"
HeightRequest="{StaticResource boxSize}"
Color="{Binding Color}" />
<StackLayout Padding = "5, 0, 0, 0"
VerticalOptions="Center" BackgroundColor = "Green" >
<Label Text = "{Binding FriendlyName}"
Font="Bold, Medium" BackgroundColor = "Red" />
<StackLayout Orientation = "Horizontal"
Spacing="0" BackgroundColor = "Purple">
<Label Text = "{Binding Color.R,
Converter={StaticResource intConverter},
ConverterParameter=255,
StringFormat='R={0:X2}'}" BackgroundColor = "Yellow" />
<Label Text = "{Binding Color.G,
Converter={StaticResource intConverter},
ConverterParameter=255,
StringFormat=', G={0:X2}'}" BackgroundColor = "Red" />
<Label Text = "{Binding Color.B,
Converter={StaticResource intConverter},
ConverterParameter=255,
StringFormat=', B={0:X2}'}" BackgroundColor = "Gray" />
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
3. 第三种区分平台,xaml文件中
<Slider HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" ValueChanged="Slider_OnValueChanged"
Minimum="0" Maximum="100" Rotation="0" Value="50" BackgroundColor = "Transparent" Opacity = "1">
<Slider.Effects>
<effects:SliderEffect ThumbImage="button_flightpage_unlock.png"
TrackImage="transparent_image.png"
ReturnPosition="Start"
SlideToEnd="SliderEffect_OnSlideToEnd"
TrackInset = "30" >
<effects:SliderEffect.ThumbImageHeight>
<OnPlatform x:TypeArguments="x:Double">
<OnPlatform.Android>120</OnPlatform.Android>
<OnPlatform.iOS>60</OnPlatform.iOS>
</OnPlatform>
</effects:SliderEffect.ThumbImageHeight>
<effects:SliderEffect.ThumbImageWidth>
<OnPlatform x:TypeArguments="x:Double">
<OnPlatform.Android>120</OnPlatform.Android>
<OnPlatform.iOS>60</OnPlatform.iOS>
</OnPlatform>
</effects:SliderEffect.ThumbImageWidth>
</effects:SliderEffect>
</Slider.Effects>
</Slider>
核心代码
<effects:SliderEffect.ThumbImageWidth>
<OnPlatform x:TypeArguments="x:Double">
<OnPlatform.Android>120</OnPlatform.Android>
<OnPlatform.iOS>60</OnPlatform.iOS>
</OnPlatform>
</effects:SliderEffect.ThumbImageWidth>
4. 第四种使用方式,简便写法
this.Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 10, 5);
网友评论