Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 控制每比特绑定到大比特集_Silverlight_Binding_Mvvm_Bitset - Fatal编程技术网

Silverlight 控制每比特绑定到大比特集

Silverlight 控制每比特绑定到大比特集,silverlight,binding,mvvm,bitset,Silverlight,Binding,Mvvm,Bitset,假设我得到了一个大的位集(类不重要,可以是bool[100]),我得到了10x10个黑色和白色的矩形,我想绑定到位集中的每个位 我也不想在单个位发生变化时强制进行完全重画(有一些解决方案可能导致这种行为),而只是对特定矩形进行一次重画 关于我的实际实现的任何其他细节都无关紧要,我可以使用最适合存储这些位的任何类(ObservableCollection,smth-else,你可以这么说) 我更喜欢最优雅的解决方案,我也不希望用100个属性放大viewmodel。。这是一个测试解决方案的有趣问题。

假设我得到了一个大的位集(类不重要,可以是bool[100]),我得到了10x10个黑色和白色的矩形,我想绑定到位集中的每个位

我也不想在单个位发生变化时强制进行完全重画(有一些解决方案可能导致这种行为),而只是对特定矩形进行一次重画

关于我的实际实现的任何其他细节都无关紧要,我可以使用最适合存储这些位的任何类(ObservableCollection,smth-else,你可以这么说)


我更喜欢最优雅的解决方案,我也不希望用100个属性放大viewmodel。。这是一个测试解决方案的有趣问题。

。)

这是我想到的。单击“播放”按钮观看颜色随机化

视图:


以及ViewModel:

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Media;
使用System.Windows.Threading;
namespace Samples.ViewModels
{
公共类BitsetViewModel
{
私有列表_bitset=新列表();
私有随机数_rand=new Random();
专用调度程序_timer=新调度程序();
公共BitsetViewModel()
{
_timer.Interval=TimeSpan.fromMillimes(1);
_timer.Tick+=新事件处理程序(_timer_Tick);
对于(int i=0;i<2000;i++)
{
var color=_rand.Next(0,5);
_添加(新的BitsetItem(){Color=Color==1?Colors.Black:Colors.White});
}
}
void\u timer\u Tick(对象发送方,事件参数e)
{
变量位=_rand.Next(0,1999);
_位集[bit]。颜色=\位集[bit]。颜色==颜色。白色?颜色。黑色:颜色。白色;
}
公共IEnumerable位集
{
获取{return\u bitset;}
}
公共字符串操作
{
获取{return\u timer.IsEnabled?“停止”:“播放”}
}
公共游戏
{
如果(_timer.IsEnabled)
_timer.Stop();
其他的
_timer.Start();
}
}
公共类BitsetItem:INotifyPropertyChanged
{
私有颜色_Color=颜色。白色;
私有SolidColorBrush_brush=新SolidColorBrush();
公共颜色
{
获取{return\u color;}
设置
{
_颜色=值;
_笔刷=新的SolidColorBrush(颜色);
RaisePropertyChanged(“颜色”);
RaisePropertyChanged(“刷子”);
}
}
公共色刷
{
获取{return\u brush;}
设置
{
_刷=值;
RaisePropertyChanged(“刷子”);
}
}
私有void RaisePropertyChanged(字符串名称)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(名称));
}
}
公共事件属性更改事件处理程序属性更改;
}
}

您是否可以添加一些关于这种方法性能的想法。因为没有人对这个问题感兴趣,所以我会在事后把它记下来作为答案。当我在我的机器上运行它时,它的性能非常好。你有什么顾虑?我最关心的是你的循环。在循环中更改单个位。我想知道如果你在2000位上循环,它的性能会如何。无论如何,我可以自己做,但更一般地说,有没有办法让它执行得更快?这种方法有什么问题。由于没有替代方案,我对这一解决方案的特性感兴趣。我的循环只是一个如何更改位的示例。我假设你会有一些真正的逻辑,实际上会改变位。顺便说一句,我在我的例子中使用了2000。嗯,可能有一些误解。稍后我会发布更新的信息,这样我就更清楚了。我有可能误解了什么,所以我会做更多的测试。
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;
    }
}