3

Xamarin.FormsListView中的 有一个RowHeight属性,但似乎无论我将其设置为什么高度。

我使用 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());
    }
}

这是视觉结果:

Android模拟器RowHeight问题

请注意,我的代码是Xamarin 使用的 TODO 示例的简化版本,您可以看到它们RowHeight以与我相同的方式设置属性。

无论我设置什么,因为RowHeight行具有相同的巨大高度。我能做些什么?

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

编辑2:我尝试设置HasUnevenRows为true,如此处所建议的,但这只会让它接受更长的条目,而不是改变行的高度(或字体),这对我来说太大了。

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

4

1 回答 1

6

我得到了这个建议

不确定 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. ListViewsItemSource设置为对象具有属性的List<Quiz>地方(我绑定到的那个)。QuizTitleLabel
  2. ListView它的ItemTemplate方法设置为new DataTemplate(typeof (QuizCell)),这是我创建的新类。

现在我可以HeightRequest使用继承自ViewCell.

我出现问题的原因是我过于简化了 TODO 示例RowHeight(这似乎并没有起到多大作用)。此外,总而言之,这与模拟器无关。我希望这可以帮助别人!

于 2014-07-01T17:17:26.610 回答