【问题标题】:Xamarin.Forms ListView, how to set RowHeight?Xamarin.Forms ListView,如何设置RowHeight?
【发布时间】:2014-08-21 16:38:32
【问题描述】:

Xamarin.Forms 中的ListView有一个RowHeightproperty,但似乎不管我将它设置到什么高度。

我使用 Visual Studio 2013,我拥有 Xamarin 的商业版许可证,我使用 Android 模拟器 MonoForAndroid_API_15,我相信我拥有所有相关内容的最新版本。到目前为止,我还不能运行 iOS 或 WinPhone 模拟器,所以我无法比较。这只是 Android 模拟器的问题还是 RowHeight 属性设置不正确?

这是我继承的ContentPage(注意我设置了RowHeight):

public class QuizzesPage : ContentPage
{
    private readonly ListView _listView;

    public QuizzesPage()
    {
        Title = "Test";
        NavigationPage.SetHasNavigationBar(this, true);
        _listView = new ListView {RowHeight = 20};
        Content = _listView;
    }

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        _listView.ItemsSource = new[] {"One", "Two", "Three", "Four", "Five"};
    }
}

在App:

public class App
{
    public static Page GetMainPage()
    {
        return new NavigationPage(new QuizzesPage());
    }
}

这是视觉效果:

请注意,我的代码是the TODO example used by Xamarin 的简化版本,您可以看到他们设置RowHeight 属性的方式与我相同。

无论我设置什么RowHeight,行的高度都相同。我能做什么?

编辑 1:我计划本周升级到 Windows 8.1,然后可以在 WinPhone 上测试它的外观。要在工作中将 Mac 添加到网络,我首先需要一些权限。这就是为什么我至今只使用 Android 模拟器的原因。

编辑 2: 我尝试将 HasUnevenRows 设置为 true,如 suggested here,但这只会使其接受更长的条目,而不是更改行的高度(或字体),即对我的口味来说太大了。

编辑 3: 我发布了一个解决方案/解决方法作为答案。至少我可以完全控制单元格的高度。但RowHeight 似乎并没有多大作用。

【问题讨论】:

    标签: android-emulator xamarin.forms


    【解决方案1】:

    我收到了this suggestion:

    不确定 RowHeight 但尝试将 ListView.HasUnevenRows 设置为 true。然后,您可以指定单元格的高度。可能是在使用 RowHeight 时也需要设置此布尔值。

    这让我意识到我必须创建一个继承自 ViewCell 的类并绑定到类的属性:

    public class QuizCell : ViewCell
    {
        public QuizCell()
        {
            var label = new Label {HeightRequest = 20};
            label.SetBinding (Label.TextProperty, "Title");
            View = label;
        }
    }
    

    为了使用它,我将QuizzesPage 更改为:

    public class QuizzesPage : ContentPage
    {
        private readonly ListView _listView;
    
        public QuizzesPage()
        {
            Title = "Test";
            NavigationPage.SetHasNavigationBar(this, true);
            _listView = new ListView
                        {
                            HasUnevenRows = true,
                            ItemTemplate = new DataTemplate(typeof (QuizCell)),
                            //RowHeight = 10
                        };
            Content = _listView;
        }
    
        protected async override void OnAppearing()
        {
            base.OnAppearing();
    
            var quizEndpoint = new QuizEndpoint("Test");
            var quizzes =  await quizEndpoint.LoadAllAsync();
            _listView.ItemsSource = quizzes;
        }
    }
    

    这里的重要部分是:

    1. ListViews ItemSource 设置为List<Quiz>,其中Quiz 对象具有Title 属性(我将Label 绑定到的属性)。
    2. ListView 将其 ItemTemplate 方法设置为 new DataTemplate(typeof (QuizCell)),这是我创建的新类。

    现在我可以使用继承自ViewCell 的类中使用的组件的HeightRequest 属性将行的高度设置为我想要的任何值。

    我的问题的原因是我简化了the TODO example 太多(而RowHeight 似乎没有多大作用)。此外,总而言之,这与模拟器无关。我希望这对某人有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多