如何根据XAML中的平台移动boxview的位置

时间:2018-01-26 19:20:50

标签: xaml xamarin xamarin.forms

这是我在boxview中的代码XAML,它位于absolutelayout

<BoxView  BackgroundColor="#04388C" AbsoluteLayout.LayoutBounds="0,1,1,.1" AbsoluteLayout.LayoutFlags="All"/>

那里显示的是我的ANDROID设备的absolutelayout.layoutbounds,但对于IOS我需要它是&#34; 0,0,1,.1&#34;在顶部

让我放置截图↓↓↓↓↓点击图片链接

IOS屏幕底部的蓝色框我需要它在顶部

the blue box on the bottom on the screen on IOS i need it to be at the top

2 个答案:

答案 0 :(得分:1)

在XAML中:

<BoxView  x:Name="MyBoxView"/>

在Code-Behind构造函数中:

AbsoluteLayout.SetLayoutFlags(MyBoxView, AbsoluteLayoutFlags.All);
AbsoluteLayout.SetLayoutBounds(MyBoxView, new Rectangle(0, Device.OnPlatform(iOS:0, Android:1, WinPhone:0), 1, 0.1));

注意:Device.OnPlatform(iOsValue,androidValue,WinValue)根据运行平台设置不同的值。

<强>更新 上面的代码会给我们以下警告信息:

[System.Obsolete("OnPlatform is obsolete as of version 2.3.4. Please use switch(RuntimePlatform) instead.")]

要解决此问题,我们可以使用此代码:

AbsoluteLayout.SetLayoutFlags(MyBoxView, AbsoluteLayoutFlags.All);
switch (Device.RuntimePlatform)
{
     case Device.Android:
     {
         AbsoluteLayout.SetLayoutBounds(MyBoxView, new Rectangle(0, 1, 1, 0.1));
         break;
     }
     case Device.iOS:
     {
          AbsoluteLayout.SetLayoutBounds(MyBoxView, new Rectangle(0, 0, 1, 0.1));
          break;
     }
}

答案 1 :(得分:1)

仅限XAML:

<ResourceDictionary>
    <OnPlatform x:Key="layoutBounds" x:TypeArguments="Rectangle">
    <On Platform="iOS">0,0,1,.1</On>
    <On Platform="Android, UWP">0,1,1,.1</On>
  </OnPlatform>
</ResourceDictionary>
...
<AbsoluteLayout>
  <BoxView BackgroundColor="Blue" 
           AbsoluteLayout.LayoutBounds="{StaticResource layoutBounds}" 
           AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>