如何进行油门事件RX?
我试图在更改该值时触发滑块触发的事件,我想将其投入到1秒钟,每秒打印,但是打印的数据是不正确的,该集合中的所有值都在同一时间打印。但是我想每秒打印该唯一数据类型的最后一个值,下面发布的结果。
[完全可重现的代码] XAML:
<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收到的时间18/04/2022 00:16:05
唯一1 POS 0.06468305收到的时间18/04/2022 00:16:05 OBS
唯一1 POS 0.06468305收到的时间18/04/2022 00:16:05 OBS
唯一1 POS 0.06468305收到的时间18/04/2022 00:16:05 OBS
唯一1 POS 0.06468305收到的时间18/04/2022 00:16:05 OBS
预期:如果用户在0.0起启动,并且在1秒内从0.0拖动到300.98,则到200.45到2秒:
unique1 pos 0.0收到的时间18/04/2022 00:16:05
unique1 POS 300.98收到的时间18/04/2022 00:16:06
unique1 pos 200.45收到的时间18/04/2022 00:16:07
问题:
studentList
计数不断增长,而不是可观察到的重置(每次使用时,每次将鼠标从控件中释放出来,都应重置列表。学生列表
计数不断增长,而不是通过可观察到的重置(每次使用时,每次使用鼠标都应 'debug.print仅在我从拖动运动中释放鼠标
时才能打印/执行
@enigmativity答案将我的第四个问题分类后才被打印/执行。
我是RX技术的新手,因此我不确定是否以正确的方式实施它。
基本上,如果更改的值来自相同的控件,则我想对其进行防油门,例如,当用户单击旋钮时,在滑块控件上,一旦用户释放鼠标释放鼠标,请点击top stop stop stop stop stop totlottling
I am trying to throttle an event being triggered by a slider when the value has been changed I would like to throttle to 1 seconds, its Print every second but the data being printed is incorrect all value in the collection is being printed at the same time but I would like to print the last value of that Unique data type every second, result posted below.
[Fully reproducible code]
XAML:
<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>
Code behind:
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()));
}
}
Model:
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);
}
}
}
Result:
Unique1 Pos 0.06468305 Time Received 18/04/2022 00:16:05
Unique1 Pos 0.06468305 Time Received 18/04/2022 00:16:05 OBS
Unique1 Pos 0.06468305 Time Received 18/04/2022 00:16:05 OBS
Unique1 Pos 0.06468305 Time Received 18/04/2022 00:16:05 OBS
Unique1 Pos 0.06468305 Time Received 18/04/2022 00:16:05 OBS
Expected: If the user starts at 0.0 and drag from 0.0 to 300.98 in 1 seconds, then to 200.45 by 2 seconds:
Unique1 Pos 0.0 Time Received 18/04/2022 00:16:05
Unique1 Pos 300.98 Time Received 18/04/2022 00:16:06
Unique1 Pos 200.45 Time Received 18/04/2022 00:16:07
Issues:
studentsList
counts keeps growing and not reset by the observable(Every time the use release the mouse from the control it should reset the list.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
The Debug.Print only gets printed/execute once I release the mouse from the drag motion
@Enigmativity answer sorted my forth known issue.
I am new to the Rx technology so I am not sure if I am implementing it in the correct manner.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有行:
应该是:
RX中的操作员流利。您可以从
油门
操作员中获得一个新的观察。没有分配可观察
保持不变。You have the line:
It should be:
The operators in Rx are fluent. You get a new observable from the
Throttle
operator. Without the assignmentobservable
stays the same.为了向您展示您可能需要的可观察到的工作,应该看起来像这样:
订阅,这样的订阅:
我添加了此代码来模拟长期运行的HTTP调用:
现在我不知道正是您要使用此查询的方法,但这应该有助于使您朝着正确的方向发展。
RX的关键是尝试将查询降低到您订阅一次的一个单个查询。它使生活更加轻松。
这是完整的代码:
In an attempt to show you what you probably need to get your observable to work, it should look something like this:
And the subscription, like this:
I added this code to simulate a long-running HTTP call:
Now I don't know exactly what you're trying to do with this query, but this should help to move you in the right direction.
Key to Rx is to try to get your query down to one single query that you subscribe to once. It makes life much easier.
Here's the complete code: