【初入MAUI】如何添加特定于平台的自定义,2种方式,多种语法
如何添加特定于平台的自定义,2种方式,多种语法
第一种方式:使用 OnPlatform 标记扩展
第一种写法:冗长写法
-
打开xaml文件
-
使用OnPlatform标记扩展
举例:
{
...
<VerticalStackLayout.BackgroundColor>
</VerticalStackLayout.BackgroundColor>
...
}
如此一来,将 iOS、Android 和 Windows 中页面上的堆积布局的背景颜色分别更改为 Silver、Green 和 Yellow。
第二种写法:简洁写法
-
打开xaml文件
-
使用OnPlatform标记扩展
举例:
{
...
}
第二种方式:使用 Device 类
只有一种写法
-
打开XAML下的隐藏文件
-
在InitializeComponent()后,设置属性。
举例:
{
MyStackLayout.Padding =
DeviceInfo.Platform == DevicePlatform.iOS
? new Thickness(30, 60, 30, 30) // Shift down by 60 points on iOS only
: new Thickness(30); // Set the default margin to be 30 points}
如此一来,只会在IOS中将内容下移60点,在其他平台则不会。
总结
微软推荐使用第一种方式的简洁写法,理由是“执行此操作更适合且更方便”。
浙公网安备 33010602011771号