2017-10-12 61 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列到你的網格,每個相等的高度,將保證該屏幕的一半,並通過將地圖中的第一行中的劃分將繼續它的高度也是你屏幕的一半。

相關問題