【问题标题】:how to get rid of MasterDetailPage empty space Xamarin Forms如何摆脱 MasterDetailPage 空白空间 Xamarin Forms
【发布时间】:2017-01-13 03:20:59
【问题描述】:

我的项目有问题,我正在使用 MasterDetailPage 和一个简单的 ListView 并使用本地命名空间来调用他内部的另一个页面以及 ListView,并得到 THIS 结果,问题是,工具栏和列表视图之间的空白区域,但是如果我尝试在不使用 MasterDetailPage 的情况下导航该页面,则列表视图在没有该空白区域的情况下工作正常,您检查HERE

App.cs 导航到 MenuPage.Xaml

MainPage = new NavigationPage(new MenuPage());

MenuPage.XAML

`<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Gone.MenuPage"
             xmlns:local="clr-namespace:Gone;assembly=Gone">
  <MasterDetailPage.Master>
    <ContentPage Title="Menu">
      <ListView BackgroundColor="Transparent"
                SeparatorVisibility="Default"
                HasUnevenRows="True"
                x:Name="listView">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout Padding="2" Orientation="Horizontal">
                <Image Aspect="Fill" WidthRequest="60" HeightRequest="60" Source="{Binding image}"/>
                <StackLayout Padding="5,18,0,0" Orientation="Vertical">
                  <Label TextColor="Black" Text="{Binding title}"/>
                </StackLayout>
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </ContentPage>
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <local:MainPage/>
  </MasterDetailPage.Detail>
</MasterDetailPage>`

MainPage.Xaml

    <ListView BackgroundColor="Transparent"
              SeparatorVisibility="Default"
              HasUnevenRows="True"
              x:Name="listView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Orientation="Horizontal">
            <Image Aspect="AspectFill" WidthRequest="130" HeightRequest="130" Source="{Binding image}"/>
            <StackLayout Padding="20" Orientation="Vertical">
              <Label LineBreakMode="WordWrap" FontSize="17" TextColor="#4CAF50" Text="{Binding title}"/>
              <Label FontAttributes="Bold" TextColor="#2962FF" Text="{Binding price}"/>
              <Label TextColor="#455A64" Text="{Binding date}"/>
            </StackLayout>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    临时解决方案是 在您的 Android 项目中创建 CustomMasterDetailRenderer

    [assembly: ExportRenderer(typeof(MasterDetailPage), typeof(CustomMasterDetailRenderer))]
    namespace Your.Name.Space
    {
        public class CustomMasterDetailRenderer : MasterDetailPageRenderer
        {
            public override void AddView(Android.Views.View child)
            {
                child.GetType().GetRuntimeProperty("TopPadding").SetValue(child, 0);
                var padding = child.GetType().GetRuntimeProperty("TopPadding").GetValue(child);
    
                base.AddView(child);
            }
        }
    }
    

    可能需要在您的view 中设置Padding 0。

    感谢西尔维娅·马丁内斯: https://forums.xamarin.com/discussion/comment/244966/#Comment_244966

    【讨论】:

      【解决方案2】:

      如果您将 MasterDetailPage 与 NavigationPage 一起使用,则会发生这种情况。

      不要将 MasterDetailPage 声明为 new NavigationPage(new MasterDetailPage())。

      而是将 MasterDetailPage 声明为 var masterDetailPage = new MasterDetailPage()。 之后使用 masterDetailPage.Detail = new NavigationPage(new ContentPage());

      所以如果你通过masterDetail和navigation都有导航,你会想出几个NavigationPages。而且您将无法仅使用 Navigation.PushAsync(somePage)。取而代之的是,您将不得不使用当前的 NavigationPage 实例,例如: ((NavigationPage)(this.Detail)).PushAsync(new ContentPage());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多