Xamarin.Forms ListView Multilining和选定的项目颜色

时间:2017-12-14 12:23:50

标签: listview xamarin.forms multiline listviewitem selecteditem

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

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

enter image description here

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

像这样:[更新]

enter image description here

这是我的代码:

的MainPage

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>

StoreCell

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;
    }

[更新]

Coordinate类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;
    }
}

你需要问任何事情,非常感谢你。

2 个答案:

答案 0 :(得分:4)

1。对于&#34;多线&#34;列表视图中的viewcells必须在列表视图中设置属性:

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

HasUnevenRows不是必需的,但取决于您的需求。我想这个属性的名字告诉你它是什么:D

2. 对于橙色删除(仅适用于Android): 在Android项目的Resources / values / styles.xml中添加/替换此行:

  

&LT; item name =&#34; android:colorActivatedHighlight&#34; &GT;#000000&LT; / item&gt;。

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

对于iOS高亮颜色:

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

答案 1 :(得分:1)

尝试:

1.将RowHeight上的ListView设置为合适的尺寸 2.使用StackPanel作为列表项的模板可能更容易,而不是使用嵌套的GridView
3.您的Store课程应该对您的媒体资源采用适当的ToString()方法。目前您有Coordinate类型的属性ToString()返回什么?

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