這是一個有趣的測試解決方案的問題。 :)
這是我想出來的。點擊播放按鈕隨機觀看顏色。
查看:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Samples.ViewModels"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="Samples.BitsetView"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="500">
<UserControl.Resources>
<vm:BitsetViewModel x:Key="vm" />
<ItemsPanelTemplate x:Key="ItemsPanelTemplate">
<toolkit:WrapPanel />
</ItemsPanelTemplate>
<DataTemplate x:Key="ItemTemplate">
<Rectangle Width="10" Height="10" Fill="{Binding Brush}"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}" Width="500" Height="400">
<ItemsControl Margin="0" ItemsSource="{Binding Bitset}" ItemsPanel="{StaticResource ItemsPanelTemplate}" ItemTemplate="{StaticResource ItemTemplate}"/>
<Button Content="{Binding Action}" Height="23" Width="40" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Play"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</UserControl>
和視圖模型:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Threading;
namespace Samples.ViewModels
{
public class BitsetViewModel
{
private List<BitsetItem> _bitset = new List<BitsetItem>();
private Random _rand = new Random();
private DispatcherTimer _timer = new DispatcherTimer();
public BitsetViewModel()
{
_timer.Interval = TimeSpan.FromMilliseconds(1);
_timer.Tick += new EventHandler(_timer_Tick);
for (int i = 0; i < 2000; i++)
{
var color = _rand.Next(0, 5);
_bitset.Add(new BitsetItem() { Color = color == 1 ? Colors.Black : Colors.White });
}
}
void _timer_Tick(object sender, EventArgs e)
{
var bit = _rand.Next(0, 1999);
_bitset[bit].Color = _bitset[bit].Color == Colors.White ? Colors.Black : Colors.White;
}
public IEnumerable<BitsetItem> Bitset
{
get {return _bitset;}
}
public string Action
{
get { return _timer.IsEnabled ? "Stop" : "Play"; }
}
public void Play()
{
if (_timer.IsEnabled)
_timer.Stop();
else
_timer.Start();
}
}
public class BitsetItem : INotifyPropertyChanged
{
private Color _color = Colors.White;
private SolidColorBrush _brush = new SolidColorBrush();
public Color Color
{
get { return _color; }
set
{
_color = value;
_brush = new SolidColorBrush(Color);
RaisePropertyChanged("Color");
RaisePropertyChanged("Brush");
}
}
public SolidColorBrush Brush
{
get { return _brush; }
set
{
_brush = value;
RaisePropertyChanged("Brush");
}
}
private void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
你可以添加對這種做法表現某種思想。如果沒有其他人對此感興趣,那麼我會把它標記爲答案。 –
當我在機器上運行它時,它非常高效。你有什麼擔憂? – Bryant
我主要關心你的循環。循環中改變單個位。我想知道,如果您循環使用2000個bitsets,它將會是多麼的高性能。無論如何,我可以自己做到這一點,但更普遍的是有沒有辦法讓它的表現更快?這種方法有什麼問題。由於沒有其他選擇,我對這一解決方案的特點感興趣。 –