简体   繁体   English

如何摆脱MasterDetailPage空格Xamarin Forms

[英]how to get rid of MasterDetailPage empty space Xamarin Forms

I have a problem with my project, I'm using MasterDetailPage with a simple ListView and using a local namespace to call another page inside of him with a ListView too, and have THIS result, the problem is, that white space between the toolbar and the listview, but if I try to navigate for this very page without using the MasterDetailPage, the listview works fine without this white space, and you check HERE 我有我的项目出了问题,我使用MasterDetailPage用一个简单ListView和使用本地命名空间与调用另一页在他体内ListView过了,有结果,问题是,该工具栏之间的空白和列表视图,但是如果我尝试在不使用MasterDetailPage的情况下导航这个页面,那么listview在没有这个空格的情况下工作正常,你可以在这里查看

App.cs navigates to MenuPage.Xaml App.cs导航到MenuPage.Xaml

MainPage = new NavigationPage(new MenuPage());

MenuPage.XAML 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 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>

A temporary solution is Creating a CustomMasterDetailRenderer in your Android Project: 临时解决方案是在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);
        }
    }
}

Maybe is needed set Padding 0 in your view . 也许需要在你的view设置Padding 0。

Thanks to Sylvia Martinez: https://forums.xamarin.com/discussion/comment/244966/#Comment_244966 感谢Sylvia Martinez: https//forums.xamarin.com/discussion/comment/244966/#Comment_244966

This happens if you use MasterDetailPage with NavigationPage. 如果您将MasterDetailPage与NavigationPage一起使用,则会发生这种情况。

Don't declare MasterDetailPage as new NavigationPage(new MasterDetailPage()). 不要将MasterDetailPage声明为新的NavigationPage(新的MasterDetailPage())。

Instead declare MasterDetailPage as var masterDetailPage = new MasterDetailPage(). 而是将MasterDetailPage声明为var masterDetailPage = new MasterDetailPage()。 After that use masterDetailPage.Detail = new NavigationPage(new ContentPage()); 之后使用masterDetailPage.Detail = new NavigationPage(new ContentPage());

So if you have navigation both through masterDetail and navigation, you will come up with several NavigationPages. 因此,如果您通过masterDetail和导航进行导航,您将会想出几个NavigationPages。 And you won't be able to use just Navigation.PushAsync(somePage). 而你将无法使用Navigation.PushAsync(somePage)。 Instead of it you will have to use the current NavigationPage instance like: ((NavigationPage)(this.Detail)).PushAsync(new ContentPage()); 而不是它你将不得不使用当前的NavigationPage实例,如:((NavigationPage)(this.Detail))。PushAsync(new ContentPage());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM