【发布时间】:2020-06-27 17:39:08
【问题描述】:
我需要通过 Xamarin 上的效果删除列表视图的选定背景颜色。我通过这个link发现listview包含了资源,但是我一直无法通过效果中的控件访问这些资源。
这是我尝试过的:
Windows.UI.Xaml.Controls.ListView listView = (Windows.UI.Xaml.Controls.ListView)Control;
ListView elementListView = (ListView)Element;
var backgroundColor = elementListView.BackgroundColor.ToWindowsColor();
listView.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemBackgroundSelected"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemBackgroundSelectedPointerOver"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemRevealBackgroundSelectedPressed"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemSelectedBackgroundThemeBrush"] = new SolidColorBrush(backgroundColor);
我不确定这些资源中是否真的包含在 listView 中,因为关于 listview 的 UWP 文档没有像 Button 或其他控件那样包含有关资源的部分。
有人在这方面取得了成功吗?
编辑
我已接受 Cherry Bu - MSFT 下面的回答,因为它让我发现我可以使用:
Windows.UI.Xaml.Controls.ListView listView = (Windows.UI.Xaml.Controls.ListView)Control;
ListView elementListView = (ListView)Element;
var backgroundColor = elementListView.BackgroundColor.ToWindowsColor();
listView.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(backgroundColor);
listView.Resources["SystemControlHighlightListAccentMediumBrush"] = new SolidColorBrush(backgroundColor);
在效果中。这只会更改放置效果的列表视图的资源,而不是应用程序中的所有列表视图。
感谢您的帮助!
【问题讨论】:
-
您提供的链接更改了所述 ListViewItem 使用的主题资源值(这可能是矫枉过正,但事实并非如此)。在这种情况下,您应该使用 App.Current.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);而是 listView.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);
标签: c# xamarin xamarin.forms uwp