【问题标题】:Xamarin Xaml StackLayout over Map地图上的 Xamarin Xaml StackLayout
【发布时间】:2017-01-01 00:49:20
【问题描述】:

我创建了一个页面,该页面接受位置的一些详细信息(包括纬度/经度),然后显示地图和详细信息。 使用代码它可以在 Android 模拟器中很好地呈现 UI。 尝试在 XAML 中执行此操作时,我无法正确显示它 - 尽管尝试尽可能地在 XAML 中复制代码。

代码为:

public class TestDetailPage : ContentPage
{

    public TestDetailPage(TripLogEntry entry) {

        Title = "Entry Details";
        var mainLayout = new Grid
        {
            RowDefinitions = {
                new RowDefinition { Height = new GridLength (4, GridUnitType.Star) },
                new RowDefinition { Height = GridLength.Auto },
                new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }}
        };

        var map = new Map();
        // Center the map around the log entry's location
        map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(entry.Latitude, entry.Longitude), Distance.FromMiles(.5)));
        // Place a pin on the map for the log entry's location
        map.Pins.Add(new Pin
        {
            Type = PinType.Place,
            Label = entry.Title,
            Position = new Position(entry.Latitude, entry.Longitude)
        });
        var title = new Label
        {
            HorizontalOptions = LayoutOptions.Center
        };
        title.Text = entry.Title;
        var date = new Label
        {
            HorizontalOptions = LayoutOptions.Center
        };
        date.Text = entry.Date.ToString("M");
        var rating = new Label
        {
            HorizontalOptions = LayoutOptions.Center
        };
        rating.Text = $"{entry.Rating} star rating";
        var notes = new Label
        {
            HorizontalOptions = LayoutOptions.Center
        };
        notes.Text = entry.Notes;
        var details = new StackLayout
        {
            Padding = 10,
            Children = { title, date, rating, notes }
        };
        var detailsBg = new BoxView
        {
            BackgroundColor = Color.White,
            Opacity = .8
        };
        mainLayout.Children.Add(map);
        mainLayout.Children.Add(detailsBg, 0, 1);
        mainLayout.Children.Add(details, 0, 1);
        Grid.SetRowSpan(map, 3);
        Content = mainLayout;
    }
}

这呈现为: Map when using code

下面的 XAML 呈现如下:

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="TripLog.Pages.DetailPage" Title="Entry Details"
         xmlns:map="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps">
  <ContentPage.Content>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="4*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="1*" />
      </Grid.RowDefinitions>      
      <map:Map x:Name="DetailMap" Grid.RowSpan="3" />
      <BoxView x:Name="DetailsBG" BackgroundColor="Color.White" Grid.Row="0" Grid.Column="1" />
      <StackLayout Padding="10" Grid.Row="0" Grid.Column="1">
        <Label x:Name="EntryTitle" />
        <Label x:Name="EntryDate" />
        <Label x:Name="EntryRating" />
        <Label x:Name="EntryNotes" />
      </StackLayout>        
    </Grid>    
  </ContentPage.Content>
 </ContentPage>

Map when using XAML

如您所见,我尝试将代码复制到 XAML,但无济于事。 在 XAML 代码中完成后,您能否协助获得相同的 UI 呈现?

提前致谢。

【问题讨论】:

    标签: xaml xamarin


    【解决方案1】:

    Grid.Children.Add(Element, Col, Row) 的参数,这可能违反直觉。在您的 XAML 中,您正在转置 Column 和 Row 值。

    尝试切换到:

      <BoxView x:Name="DetailsBG" BackgroundColor="Color.White" Grid.Row="1" Grid.Column="0" />
      <StackLayout Padding="10" Grid.Row="1" Grid.Column="0">
    

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 2020-03-21
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多