我试图限制由滑块触发的事件,当值已更改时,我希望限制为1秒,每秒打印一次,但打印的数据不正确.同时打印集合中的所有值,但我希望每秒打印该唯一数据类型的最后一个值,结果发布在下面.
[完全可复制代码]
<Grid>
<TextBox x:Name="txtbilly" Text="{Binding Position, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" Margin="0,0,0,382" ></TextBox>
<TextBox x:Name="txtbob" Text="{Binding Position, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" Margin="0,162,0,233" ></TextBox>
<Slider x:Name="slbilly" Value="{Binding Position, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" Margin="0,57,0,306" GotMouseCapture="slbilly_GotMouseCapture"/>
<Slider x:Name="slbob" Value="{Binding Position, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,206,0,171" GotMouseCapture="slbob_GotMouseCapture" />
</Grid>
代码隐藏:
public partial class MainWindow : Window
{
Student billy = new Student("Unique1", 0.0f);
Student Bob = new Student("Unique2", 0.0f);
public MainWindow()
{
InitializeComponent();
txtbilly.DataContext = billy;
slbilly.DataContext = billy;
billy.PropertyChanged += Students_PropertyChanged;
txtbob.DataContext = Bob;
slbob.DataContext = Bob;
Bob.PropertyChanged += Students_PropertyChanged;
}
static IList<Student> studentsList = new List<Student>();
IObservable<Student> observable;
private void Students_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var student = (Student)sender;
studentsList.Add(student);
Debug.Print("List COunt ==>>>" + studentsList.Count.ToString() + "\n");
observable = studentsList.ToObservable(Scheduler.Default);
observable.Throttle(TimeSpan.FromSeconds(1), Scheduler.Default);
}
private void slbilly_GotMouseCapture(object sender, MouseEventArgs e)
{
observable?.Subscribe(i => Debug.Print("{0}\nPos {1}\n Time Received {2}\nOBS Count{3}", i?.ID?.ToString(), i.Position.ToString(), DateTime.Now.ToString(), observable.Count<Student>().ToString()));
}
private void slbob_GotMouseCapture(object sender, MouseEventArgs e)
{
observable?.Subscribe(i => Debug.Print("{0}\nPos {1}\n Time Received {2}\nOBS Count{3}", i?.ID?.ToString(), i.Position.ToString(), DateTime.Now.ToString(), observable.Count<Student>().ToString()));
}
}
型号:
public class Student : INotifyPropertyChanged
{
private string _ID;
public string ID
{
get
{
return _ID;
}
set
{
_ID = value;
OnPropertyChanged(new PropertyChangedEventArgs("ID"));
}
}
private float _Position;
public float Position
{
get
{
return _Position;
}
set
{
_Position = value;
OnPropertyChanged(new PropertyChangedEventArgs("Position"));
}
}
public Student(string id, float position)
{
this.Position = position;
this.ID = id;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
结果:
Unique1 Pos 0.06468305收到时间2022年4月18日00:16:05
Unique1 Pos 0.06468305收到时间2022年4月18日00:16:05 OBS
Unique1 Pos 0.06468305收到时间2022年4月18日00:16:05 OBS
Unique1 Pos 0.06468305收到时间2022年4月18日00:16:05 OBS
Unique1 Pos 0.06468305收到时间2022年4月18日00:16:05 OBS
预期:如果用户从0.0开始,并在1秒内从0.0拖动到300.98,然后在2秒内拖动到200.45:
收到Unique1 Pos 0.0时间2022年4月18日00:16:05
Unique1 Pos 300.98收到时间2022年4月18日00:16:06
Unique1 Pos 200.45收到时间2022年4月18日00:16:07
问题:
-
studentsList
个计数持续增长,且不被可观察对象重置(每次使用鼠标从控件中释放时,它都应重置列表). -
InvalidOperationException:'集合已修改;枚举操作可能无法执行.'
-
调试.仅当我将鼠标从拖动动作中释放后,打印才会被打印/执行
@谜团性的回答整理了我的第四个已知问题.
我是Rx技术的新手,所以我不确定我是否以正确的方式实现了它.
Basically if the Value changed is coming from the same control I would like to throttle it, e.g. on the slider control when the user click the knob to drag start throttling once the user releases the mouse click stop throttling