c# - 如何摆脱 MasterDetailPage 空白空间 Xamarin Forms

标签 c# xamarin.forms

我的项目有问题,我正在使用 MasterDetailPage 和一个简单的 ListView 并使用本地命名空间通过 调用他内部的另一个页面>ListView 也有 THIS结果,问题是工具栏和 ListView 之间的空白,但是如果我尝试在不使用 MasterDetailPage 的情况下导航到这个页面, ListView 在没有这个空白的情况下工作正常,并且您检查 HERE

App.cs 导航到 MenuPage.Xaml

MainPage = new NavigationPage(new 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>

最佳答案

暂时的解决办法是 在您的 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

关于c# - 如何摆脱 MasterDetailPage 空白空间 Xamarin Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515536/

相关文章:

c# - 允许用户上传 .MOV 视频文件并在网站上显示

c# - 更改 Xamarin.Forms 中背景图像的纵横比,同时保持内容居中

c# - 使用 Xamarin.Auth 进行 OAuth2 身份验证 - 用户名和密码?

android - [INSTALL_FAILED_INVALID_APK]Xamarin.Forms 缺少 Base.apk

Xamarin.Forms:我可以将一个 ContentPage 或 ContentView 嵌入到另一个 ContentPage 中吗

c# - 动态构建 Func<t,t> 列表 - 然后应用于 linq 查询

c# - Hashtable 的内存是如何分配的?

xaml - Xamarin.Forms:创建 SearchBar

c# - 序列化对象

c# - 我如何将 python 对象作为参数传递给 C# 程序