如何摆脱MasterDetailPage空白窗体Xamarin

问题描述:

我有我的项目出了问题,我使用MasterDetailPage用一个简单的ListView和使用本地命名空间来调用另一个页面的他体内有ListView过了,有THIS结果,问题是,工具栏和列表视图之间的空白,但如果我尝试浏览这个很页面不使用MasterDetailPage,列表视图工作正常,没有这个空白,并且您检查HERE如何摆脱MasterDetailPage空白窗体Xamarin

应用。 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> 

出现这种情况,如果您使用MasterDetailPage与NavigationPage。

不要声明MasterDetailPage新NavigationPage(新MasterDetailPage())。

相反声明MasterDetailPage为VAR masterDetailPage =新MasterDetailPage()。 之后,使用masterDetailPage.Detail = new NavigationPage(new ContentPage());

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

一个临时的解决方案 创建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); 
     } 
    } 
} 

也许需要设置Padding 0您view

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