Xamarin.Forms ListView 多行和选中项颜色
Xamarin.Forms ListView Multilining and selected item color
嗨,我 运行 link google,但我似乎找不到解决我的 2 个问题的方法。
我正在 Xamarin Forms 中为 android 和 IOS 构建 n 个应用程序,我想要一个包含文本和图像的列表,就像这个例子一样。
问题是:
[更新]
1.无法让文字显示坐标。
2. 我无法替换所选项目
上丑陋的 o运行ge
像这样:[更新]
这是我的代码:
主页
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;
}
[更新]
坐标class toString
public override string ToString()
{
return X + " , " + Y;
}
商店Class
可能做错了...
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;
}
}
有什么需要的尽管问,非常感谢。
尝试:
1. 将 ListView
上的 RowHeight
设置为合适的大小。
2. 不使用嵌套的 StackPanel
,使用 GridView
作为列表项的模板会更容易。
3. 你的 Store
class 应该在你的属性上有一个合适的 ToString()
方法。目前你有一个 属性 类型 Coordinate
是什么 ToString()
returns?
关于第二个问题ListItem
的高亮颜色。网络上有很多例子,尤其是 Whosebug。这取决于您想要实现的目标。如果你想完全禁用 Android 和 iOS 上的突出显示,那么你将不得不使用自定义渲染器为每个平台单独处理它。否则,如果您只想在 Android 平台上更改或删除橙色,那么您应该检查@Csharpest 的解决方案。
1. 对于列表视图中的 "multiline" 个视单元,您必须在列表视图中设置属性:
<ListView x:Name="storesList"
HasUnevenRows="True"
RowHeight="50">
<!-- your row height -->
<!-- ..... -->
</ListView>
HasUnevenRows 不是必需的,但取决于您的需要。我猜 属性 的名字告诉你它是干什么用的 :D
2. 对于橙色删除(仅 Android):
Add/Replace Resources/values/styles.xml 在 Android 项目中的这一行:
< item name="android:colorActivatedHighlight" >#000000< /item >.
我不确定是否会使用该颜色。在我的例子中,橙色将被替换为默认的灰色。这对我来说没问题。
对于iOS高亮颜色:
https://forums.xamarin.com/discussion/comment/162154/#Comment_162154
嗨,我 运行 link google,但我似乎找不到解决我的 2 个问题的方法。
我正在 Xamarin Forms 中为 android 和 IOS 构建 n 个应用程序,我想要一个包含文本和图像的列表,就像这个例子一样。
问题是: [更新] 1.无法让文字显示坐标。 2. 我无法替换所选项目
上丑陋的 o运行ge像这样:[更新]
这是我的代码:
主页
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;
}
[更新]
坐标class toString
public override string ToString()
{
return X + " , " + Y;
}
商店Class
可能做错了...
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;
}
}
有什么需要的尽管问,非常感谢。
尝试:
1. 将 ListView
上的 RowHeight
设置为合适的大小。
2. 不使用嵌套的 StackPanel
,使用 GridView
作为列表项的模板会更容易。
3. 你的 Store
class 应该在你的属性上有一个合适的 ToString()
方法。目前你有一个 属性 类型 Coordinate
是什么 ToString()
returns?
关于第二个问题ListItem
的高亮颜色。网络上有很多例子,尤其是 Whosebug。这取决于您想要实现的目标。如果你想完全禁用 Android 和 iOS 上的突出显示,那么你将不得不使用自定义渲染器为每个平台单独处理它。否则,如果您只想在 Android 平台上更改或删除橙色,那么您应该检查@Csharpest 的解决方案。
1. 对于列表视图中的 "multiline" 个视单元,您必须在列表视图中设置属性:
<ListView x:Name="storesList"
HasUnevenRows="True"
RowHeight="50">
<!-- your row height -->
<!-- ..... -->
</ListView>
HasUnevenRows 不是必需的,但取决于您的需要。我猜 属性 的名字告诉你它是干什么用的 :D
2. 对于橙色删除(仅 Android): Add/Replace Resources/values/styles.xml 在 Android 项目中的这一行:
< item name="android:colorActivatedHighlight" >#000000< /item >.
我不确定是否会使用该颜色。在我的例子中,橙色将被替换为默认的灰色。这对我来说没问题。
对于iOS高亮颜色:
https://forums.xamarin.com/discussion/comment/162154/#Comment_162154