- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下问题:我有一个 Listview
,其中每个项目都是一个自定义控件(“DisruptionIcon”),它显示一个图标,具体取决于为自定义控件设置的属性。图标可以是多种形状中的一种(此处示例中只有“无”和“方形”)。现在的问题是,如果我在 ListView
中向下滚动并在备份之后,图标是错误的。结果是:
在向下滚动之前:
上下滚动后:
资源是:
主页.xaml:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="MainPg"
>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView
Width="200"
ItemsSource="{Binding LineList, ElementName=MainPg}"
>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<local:DisruptionIcon
DisplayName="{Binding DisplayName}"
IconType="{Binding DisplayStyle}"
Color1="{Binding Color1}"
Color2="{Binding Color2}"
/>
<TextBlock Text="{Binding DisplayName}" Foreground="Red"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
LineList = new ObservableCollection<ServerLineDefinition>();
for (byte i = 0; i <= 250; i++)
{
var ds = new ServerLineDefinition("ID " + i, "FFFFFF", "000000", 1, i);
LineList.Add(ds);
}
InitializeComponent();
}
public ObservableCollection<ServerLineDefinition> LineList { get; set; }
}
我的自定义控件是这样的:DisruptionIcon.xaml:
<UserControl
x:Class="App1.DisruptionIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Name="mainUc" mc:Ignorable="d"
d:DesignHeight="34" d:DesignWidth="80"
Width="80" Height="34"
>
<Grid>
<local:DisplayIconTemplateSelector Content="{Binding IconType, ElementName=mainUc}">
<local:DisplayIconTemplateSelector.StyleNone>
<DataTemplate>
<Grid/>
</DataTemplate>
</local:DisplayIconTemplateSelector.StyleNone>
<local:DisplayIconTemplateSelector.StyleSquare>
<DataTemplate>
<Rectangle
Width="80"
Height="32"
Fill="{Binding Color2, ElementName=mainUc}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
StrokeThickness="2"
Stroke="{Binding Color1, ElementName=mainUc}"
/>
</DataTemplate>
</local:DisplayIconTemplateSelector.StyleSquare>
</local:DisplayIconTemplateSelector>
<Viewbox
Stretch="Uniform"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="8,0"
>
<TextBlock
Text="{Binding DisplayName, ElementName=mainUc}"
Foreground="{Binding Color1, ElementName=mainUc}"
Height="auto"
VerticalAlignment="Top"
TextWrapping="NoWrap"
FontWeight="Bold"
TextAlignment="Center"
Margin="0,-2,0,0"
/>
</Viewbox>
</Grid>
这是 disruptionicon.xaml.cs
中 DisruptionIcon
的代码,其中基本上属性仅由 DependencyProperties 绑定(bind),颜色将转换为 SolidColorBrush
如果它们以字符串形式提供。由于应用程序设计,这是我需要的东西:
public sealed partial class DisruptionIcon
{
public DisruptionIcon()
{
Color1 = new SolidColorBrush(Colors.White);
Color2 = new SolidColorBrush(Colors.Black);
IconType = DisplayStyle.None;
DisplayName = "";
InitializeComponent();
}
#region Color1
/// <summary>
/// Used for the View of multiple Icons. Contains an IconId and the text to show for every Entry
/// </summary>
public object Color1
{
get { return (object)GetValue(DisruptionColor1Property); }
set { SetValue(DisruptionColor1Property, value); }
}
// Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisruptionColor1Property =
DependencyProperty.Register("Color1", typeof(object), typeof(DisruptionIcon), new PropertyMetadata(null, DisruptionColor1PropertyCallback));
public static void DisruptionColor1PropertyCallback(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var iconColor1 = ((DisruptionIcon)dp).Color1;
if (iconColor1 == null || iconColor1 is SolidColorBrush)
return;
//Convert Colors
if (iconColor1 is string)
iconColor1 = ConvertToSolidColorBrush((string)iconColor1);
((DisruptionIcon)dp).Color1 = iconColor1;
}
#endregion
#region Color2
/// <summary>
/// Used for the View of multiple Icons. Contains an IconId and the text to show for every Entry
/// </summary>
public object Color2
{
get { return (object)GetValue(DisruptionColor2Property); }
set { SetValue(DisruptionColor2Property, value); }
}
// Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisruptionColor2Property =
DependencyProperty.Register("Color2", typeof(object), typeof(DisruptionIcon), new PropertyMetadata(null, DisruptionColor2PropertyCallback));
public static void DisruptionColor2PropertyCallback(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var iconColor2 = ((DisruptionIcon)dp).Color2;
if (iconColor2 == null || iconColor2 is SolidColorBrush)
return;
//Convert Colors
if (iconColor2 is string)
iconColor2 = ConvertToSolidColorBrush((string)iconColor2);
((DisruptionIcon)dp).Color2 = iconColor2;
}
#endregion
#region IconType
/// <summary>
/// Used for the View of multiple Icons. Contains an IconId and the text to show for every Entry
/// </summary>
public object IconType
{
get { return (DisplayStyle)GetValue(DisruptionDisplayStyleProperty); }
set { SetValue(DisruptionDisplayStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisruptionDisplayStyleProperty =
DependencyProperty.Register("IconType", typeof(object), typeof(DisruptionIcon), new PropertyMetadata(DisplayStyle.None, DisruptionDisplayStylePropertyCallback));
public static void DisruptionDisplayStylePropertyCallback(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var iconDisplayStyle = ((DisruptionIcon)dp).IconType;
if (args.NewValue is Int32)
{
((DisruptionIcon)dp).IconType = (DisplayStyle)args.NewValue;
}
}
#endregion
#region DisplayName
/// <summary>
/// Used for the View of multiple Icons. Contains an IconId and the text to show for every Entry
/// </summary>
public string DisplayName
{
get { return (string)GetValue(DisruptionDisplayNameProperty); }
set { SetValue(DisruptionDisplayNameProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisruptionDisplayNameProperty =
DependencyProperty.Register("DisplayName", typeof(string), typeof(DisruptionIcon), new PropertyMetadata(null, DisruptionDisplayNamePropertyCallback));
public static void DisruptionDisplayNamePropertyCallback(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var iconDisplayName = ((DisruptionIcon)dp).DisplayName;
if (iconDisplayName == null)
return;
((DisruptionIcon)dp).DisplayName = iconDisplayName;
}
#endregion
/// <summary>
/// Converts a ColorCode (i.e. FF8899) to SolidColorBrush
/// </summary>
/// <param name="colorCode">Six-Digit Hex-Code of the Color</param>
/// <returns></returns>
private static SolidColorBrush ConvertToSolidColorBrush(string colorCode)
{
if (colorCode != null && colorCode.Length == 6)
{
return new SolidColorBrush(Color.FromArgb(255,
Convert.ToByte(colorCode.Substring(0, 2), 16),
Convert.ToByte(colorCode.Substring(2, 2), 16),
Convert.ToByte(colorCode.Substring(4), 16)));
}
return new SolidColorBrush(Colors.Black);
}
}
我的模板选择器 DisplayIconTemplateSelector.cs:
public class DisplayIconTemplateSelector : ContentControl
{
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
ContentTemplate = SelectTemplate(newContent, this);
}
public DataTemplate StyleNone { get; set; }
public DataTemplate StyleSquare { get; set; }
public DataTemplate SelectTemplate(object item, DependencyObject container)
{
var quoteItem = (DisplayStyle)item;
switch (quoteItem)
{
default:
return StyleNone;
case DisplayStyle.Square:
return StyleSquare;
}
}
}
最后是我的 ServerLineDefinition 类:
public class ServerLineDefinition : INotifyPropertyChanged
{
public ServerLineDefinition() { }
public ServerLineDefinition(
string displayName,
string backgroundColor,
string foregroundColor,
int displayStyle,
int id)
{
DisplayName = displayName;
Color2 = backgroundColor;
Color1 = foregroundColor;
DisplayStyle = displayStyle;
Id = id;
}
public int Id { get; set; }
public string DisplayName { get; set; }
public int DisplayStyle { get; set; }
/// <summary>
/// RGB-Value for BackgroundColor
/// </summary>
public string Color2 { get; set; }
/// <summary>
/// RGB-Value for ForegroundColor
/// </summary>
public string Color1 { get; set; }
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged; //To Update Content on the Form
/// <summary>
/// Helper for Triggering PropertyChanged
/// </summary>
/// <param name="triggerControl">The Name of the Property to update</param>
private void RaisePropertyChanged(string triggerControl)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(triggerControl));
}
}
#endregion
}
在我看来,ListView
的缓存存在问题。如果我将 ListView
替换为包裹在 ScrollViewer
中的 ItemsControl
,则问题不存在,但会占用更多内存并需要更多时间载入。此外,如果您取消注释作为注释添加到 MainPage.xaml
中的 TextBlock
,您会在每一行中看到正确的 ID,但会看到错误的图像,如截图。
编辑:如果我将任何控件放在 ScrollViewer
中,那么它会很慢,但可以工作。如果我将 DisruptionIcon.xaml 的整个代码直接放到 MainPage 中引用 DirutpionIcon 的位置,它也可以工作。
最佳答案
尝试为 ItemsControl 使用此样式 setter ,关键字“虚拟化”:
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ItemsControl">
<Border x:Name="LayoutRoot">
<ScrollViewer>
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
并在您的DisplayName 的DependencyProperty 上,更新为:
public static void DisruptionDisplayNamePropertyCallback(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var iconDisplayName = (string)e.NewValue;
((DisruptionIcon)dp).DisplayName = iconDisplayName;
}
关于c# - Windows UI 中的 ListView 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31817050/
我阅读了有关 JSR 107 缓存 (JCache) 的内容。 我很困惑:据我所知,每个 CPU 都管理其缓存内存(无需操作系统的任何帮助)。 那么,为什么我们需要 Java 缓存处理程序? (如果C
好吧,我是 jQuery 的新手。我一直在这里和那里搞乱一点点并习惯它。我终于明白了(它并不像某些人想象的那么难)。因此,鉴于此链接:http://jqueryui.com/sortable/#dis
我正在使用 Struts 2 和 Hibernate。我有一个简单的表,其中包含一个日期字段,用于存储有关何时发生特定操作的信息。这个日期值显示在我的 jsp 中。 我遇到的问题是hibernate更
我有点不确定这里发生了什么,但是我试图解释正在发生的事情,也许一旦我弄清楚我到底在问什么,就可能写一个更好的问题。 我刚刚安装了Varnish,对于我的请求时间来说似乎很棒。这是一个Magneto 2
解决 Project Euler 的问题后,我在论坛中发现了以下 Haskell 代码: fillRow115 minLength = cache where cache = ((map fill
我正试图找到一种方法来为我网络上的每台计算机缓存或存储某些 python 包。我看过以下解决方案: pypicache但它不再被积极开发,作者推荐 devpi,请参见此处:https://bitbuc
我想到的一个问题是可以从一开始就缓存网络套接字吗?在我的拓扑中,我在通过双 ISP 连接连接到互联网的 HAProxy 服务器后面有 2 个 Apache 服务器(带有 Google PageSpee
我很难说出不同缓存区域 (OS) 之间的区别。我想简要解释一下磁盘\缓冲区\交换\页面缓存。他们住在哪里?它们之间的主要区别是什么? 据我了解,页面缓存是主内存的一部分,用于存储从 I/O 设备获取的
1.题目 请你为最不经常使用(LFU)缓存算法设计并实现数据结构。 实现 LFUCache 类: LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象 in
1.题目 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: ① LRUCache(int capacity) 以正整数作为容量 capacity
我想在访问该 View 时关闭某些页面的缓存。它适用于简单查询模型对象的页面。 好像什么时候 'django.middleware.cache.FetchFromCacheMiddleware', 启
documents为 ExePackage element state Cache属性的目的是 Whether to cache the package. The default is "yes".
我知道 docker 用图层存储每个图像。如果我在一台开发服务器上有多个用户,并且每个人都在运行相同的 Dockerfile,但将镜像存储为 user1_myapp . user2 将其存储为 use
在 Codeigniter 中没有出现缓存问题几年后,我发现了一个问题。我在其他地方看到过该问题,但没有适合我的解决方案。 例如,如果我在 View 中更改一些纯 html 文本并上传新文件并按 F5
我在 Janusgraph 文档中阅读了有关 Janusgraph Cache 的内容。关于事务缓存,我几乎没有怀疑。我在我的应用程序中使用嵌入式 janusgrah 服务器。 如果我只对例如进行读取
我想知道是否有来自终端的任何命令可以用来匹配 Android Studio 中执行文件>使缓存无效/重新启动的使用。 谢谢! 最佳答案 According to a JetBrains employe
我想制作一个 python 装饰器来内存函数。例如,如果 @memoization_decorator def add(a, b, negative=False): print "Com
我经常在 jQuery 事件处理程序中使用 $(this) 并且从不缓存它。如果我愿意的话 var $this = $(this); 并且将使用变量而不是构造函数,我的代码会获得任何显着的额外性能吗?
是的,我要说实话,我不知道varnish vcl,我可以解决一些基本问题,但是我不太清楚,这就是为什么我遇到问题了。 我正在尝试通过http请求设置缓存禁止,但是该请求不能通过DNS而是通过 Varn
在 WP 站点上加载约 4000 个并发用户时遇到此问题。 这是我的配置: F5 负载均衡器 ---> Varnish 4,8 核,32 Gb RAM ---> 9 个后端,4 个核,每个 16 RA
我是一名优秀的程序员,十分优秀!