【问题标题】:Xamarin.Forms ListView Multilining and selected item colorXamarin.Forms ListView Multilining 和选定项颜色
【发布时间】:2018-05-28 12:57:43
【问题描述】:

您好,我已经在 google 上运行了每个链接,但我似乎无法找到解决我的 2 个问题的方法。

我正在为 Xamarin 表单中的 android 和 IOS 构建 n 应用程序,我想要一个包含文本和图像的列表,就像这个例子一样。

问题是: [更新] 1. 无法让文字显示坐标。 2. 我无法替换所选项目上丑陋的橙色

像这样:[更新]

这是我的代码:

主页

private ObservableCollection<Store> stores { get; set; }
    public MainPage()
    {
        InitializeComponent();
        storesList.BackgroundColor = Color.CornflowerBlue;
        storesList.SeparatorColor=Color.White;
        storesList.ItemTemplate = new DataTemplate(typeof(StoreCell));
        stores = new ObservableCollection<Store>();

        Store store1 = new Store
        {
            Name = "Pombal",
            Location = new Coordinate(39.9143958, -8.6297282).ToString()
        };
        Store store2 = new Store
        {
            Name = "Unknown",
            Location = new Coordinate(8.9143958, -39.6297282).ToString(),
            Schedule = "09:00-12:30 / 13:30-18:00"
        };

        stores.Add(store1);
        stores.Add(store2);

        storesList.ItemsSource = stores;

    }

<ListView x:Name="storesList" RowHeight="70" HasUnevenRows="True">

</ListView>

存储单元

public StoreCell()
    {
        InitializeComponent();
        var image = new Image();
        var nameLabel = new Label { TextColor = Color.White };
        var locationLabel = new Label { TextColor = Color.White };
        var scheduleLabel = new Label { TextColor = Color.White };
        var verticaLayout = new StackLayout();
        var horizontalLayout = new StackLayout();

        //set bindings
        nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
        locationLabel.SetBinding(Label.TextProperty, new Binding("Location"));
        scheduleLabel.SetBinding(Label.TextProperty, new Binding("Schedule"));
        image.SetBinding(Image.SourceProperty, new Binding("Image"));

        //Set properties for desired design
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        horizontalLayout.HorizontalOptions = LayoutOptions.Fill;
        image.HorizontalOptions = LayoutOptions.End;
        nameLabel.FontSize = 24;

        //add views to the view hierarchy
        verticaLayout.Children.Add(nameLabel);
        verticaLayout.Children.Add(locationLabel);
        verticaLayout.Children.Add(scheduleLabel);
        horizontalLayout.Children.Add(verticaLayout);
        horizontalLayout.Children.Add(image);

        // add to parent view
        View = horizontalLayout;
    }

[更新]

坐标类 toString

public override string ToString()
{
    return X + " , " + Y;
}

商店类

可能做错了......

class Store
{

    public string Name { get; set; }
    public string Location { get; set; }
    public string Schedule { get; set; }

    public Store() : this(null, null, null)
    {

    }

    public Store(String name, string location) : this(name, location, "")
    {
    }

    public Store(String name, string location, String schedule)
    {
        Name = name;
        Location = location;
        Schedule = schedule;
    }
}

有什么需要的直接问吧,非常感谢。

【问题讨论】:

    标签: listview xamarin.forms multiline listviewitem selecteditem


    【解决方案1】:

    1.对于列表视图中的“多行”视图单元,您必须在列表视图上设置属性:

    <ListView x:Name="storesList"
                HasUnevenRows="True"
                RowHeight="50"> 
    <!-- your row height -->
    <!-- ..... -->
    </ListView>
    

    HasUnevenRows 不是必需的,但取决于您的需要。我猜该属性的名称告诉你它的用途:D

    2. 对于橙色移除(仅限 Android): 在 Android 项目的 Resources/values/styles.xml 中添加/替换这一行:

    #000000.

    我不确定是否会使用该颜色。在我的情况下,橙色将替换为默认灰色。这对我来说没问题。

    对于 iOS 高亮颜色:

    https://forums.xamarin.com/discussion/comment/162154/#Comment_162154

    【讨论】:

    • 您提供的解决方案仅涵盖问题的一部分,并且仅涵盖单个操作系统 - Android,但问题涉及 Android 和 iOS。
    • 橙色高亮仅出现在 android 上。我涵盖了这两个问题。
    • 可能是我误解了你的回答。我很抱歉。
    • 没问题,如果我真的误会了,我们会在问题创建者回复后立即发现
    • 是的,Android 和 IOS 都有
    【解决方案2】:

    尝试:

    1. 将RowHeight 上的ListView 设置为适当的大小。
    2. 与其使用嵌套的StackPanel,不如使用GridView 作为列表项的模板。
    3. 你的Store 类应该在你的属性上有一个正确的ToString() 方法。目前你有一个Coordinate 类型的属性ToString() 返回什么?

    关于ListItem 的高亮颜色的第二个问题。网络上有很多例子,尤其是在stackoverflow。这取决于您想要实现的具体目标。如果您想在 Android 和 iOS 上完全禁用突出显示,那么您必须使用自定义渲染器为每个平台单独处理它。否则,如果您只想在 Android 平台上更改或删除橙色,则应查看@Csharpest 的解决方案。

    【讨论】:

    • 默认我会尝试改变它'
    • 我试图避免使用自定义渲染器方法
    • 不幸的是,所有其他解决方案都只能在 Android 或 iOS 上运行,但不能同时在两者上运行。在这种特定情况下,自定义渲染器方法不需要那么多自定义代码,因此应该不是问题。
    • RowHeightHasUnevenRows 提供了帮助,我现在可以看到时间表而不必覆盖 toString 方法,但是坐标虽然它们在字符串方法中有一个覆盖,但根本没有显示,它只是一片空白。
    • 如果 Coordinate 是你的类,只需重写 ToString() 方法,否则你可以创建一个转换器:developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/…
    猜你喜欢
    • 2017-12-17
    • 2016-11-26
    • 2013-11-06
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2014-03-07
    相关资源
    最近更新 更多