代码片段分享

Xamarin forms 在代码中判断当前系统平台

2019-08-09  本文已影响0人  花艺荣

OnPlatform 允许您根据平台更改XAML中的属性:

<ContentPage>
 <ContentPage.Padding>
     <OnPlatform x:TypeArguments="Thickness">
         <On Platform="Android, UWP">0</On>
         <On Platform="iOS">0,20,0,0</On>
     </OnPlatform>
 </ContentPage.Padding>
</ContentPage>

代码中这么做:

switch(Device.RuntimePlatform)
{
  case Device.iOS:
      this.Padding = new Thickness(0,20,0,0);
      break;
  case Device.Android:
  case Device.UWP:   
  case Device.macOS:
  default:
      // This is just an example. You wouldn't actually need to do this, since Padding is already 0 by default.
      this.Padding = new Thickness(0);
      break;
}

参考地址:
https://xamarinhelp.com/xamarin-forms-onplatform-runtimeplatform/

上一篇下一篇

猜你喜欢

热点阅读