2017-10-12 58 views
0

我正在处理此示例Click here如何更改Xamarin.form中自定义地图的大小?

我发现地图用App.ScreenWidth和App.ScreenHeight显示。这里是我的xaml代码如下:

<ContentPage.Content> 
    <local:CustomMap x:Name="customMap" MapType="Street" 
     WidthRequest="{x:Static local:App.ScreenWidth}" 
     HeightRequest="{x:Static local:App.ScreenHeight}" /> 
</ContentPage.Content> 

如何将App.screenheight设置为其高度的1/2?

回答

0

最方便的可能是使用AbsoluteLayoutRelativeLayout。我将展示如何使用与AbsoluteLayout做到这一点,因为这是我用更多的时候

<ContentPage.Content> 
    <AbsoluteLayout VerticalOptions="Fill" HorizontalOptions="Fill"> 
    <local:CustomMap [...] 
     AbsoluteLayout.LayoutFlags="All" 
     AbsoluteLayout.LayoutBounds="0,0,1,.5" /> 
    </AbsoluteLayout> 
</ContentPage.Content> 

随着VerticalOptions="Fill"HorizontalOptions=Fill你告诉AbsoluteLayout采取全可用空间在您的ContentPageAbsoluteLayout.LayoutFlags="All"是一个附加属性,它告诉您的CustomMapAbsoluteLayout中的定位应该是相对的。最后,你告诉你的CustomMap它应该被定位在(0,0)处,采用AbsoluteLayout.LayoutBounds="0,0,1,.5"的宽度和半高度。

0

我们无法在XAML中进行计算,因此您可以使用代码隐藏方法。

下面是示例代码一些简单的计算:

public class MapPageCS : ContentPage 
    { 
     public MapPageCS() 
     { 
     var customMap = new CustomMap { 
      MapType = MapType.Street, 
      WidthRequest = App.ScreenWidth /2, 
      HeightRequest = App.ScreenHeight/2 
     }; 
     ... 

     Content = customMap; 
     } 
    } 
+0

谢谢,但这是行不通的。哈哈 –

2

如果你想在地图覆盖一半的应用程式画面,只需使用一个Grid系统。

的代码应该是这样的:

<ContentPage.Content> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions>    
     <local:CustomMap x:Name="customMap" MapType="Street" Grid.Row="0" /> 
    </Grid> 
</ContentPage.Content> 

因此,通过增加2列到你的网格,每个相等的高度,将保证该屏幕的一半,并通过将地图中的第一行中的划分将继续它的高度也是你屏幕的一半。

相关问题