如何使用 Reactive UI 在单击按钮与按下按钮(按住)后触发不同的操作
How to use Reactive UI to trigger a different action following a button click vs button press (hold)
我正在尝试实现一个 UI 控件,用户可以在其中单击按钮让物体移动一点,或者按住按钮并在按住按钮的同时让物体移动.
假设我有 Task<Unit> StartMove()
、Task<Unit> StopMove()
和 Task<Unit> MoveStep()
。单击按钮应执行 MoveStep()
并且按住按钮应该开始移动,然后在释放按钮时立即停止移动。移动发生时应忽略快速点击(双击),并且每秒发送的 MoveStep 命令不应超过 2 倍。还需要一些故障安全装置,它会在出现错误或长时间超时(比如 5 分钟)后停止移动。
按钮按下由 Button 对象上的 属性 表示,当用户按下按钮时触发 true
值,释放时触发 false
值,这值在常规 WPF 按钮上称为 IsPressed。真值后跟假值不到一秒表示点击,真值后跟假值超过一秒表示保持(此秒值也可以调整为半秒)。
问题归结为获取以随机间隔到达的真/假值流(想想:猴子随机按下按钮)并从该流中确定按钮是被单击还是被按住。基于此,应触发操作:MoveStep
表示单击,StartMove
然后 StopMove
表示按钮按住。
工作代码片段
我终于找到了一些有用的东西。
到目前为止我有 MainWindow
public partial class MainWindow : Window, IViewFor<AppViewModel>
{
public AppViewModel ViewModel { get; set; }
object IViewFor.ViewModel { get => ViewModel; set => ViewModel = value as AppViewModel; }
public MainWindow()
{
ViewModel = new AppViewModel();
DataContext = ViewModel;
InitializeComponent();
this.WhenAnyValue(x => x.MoveLeftButton.IsPressed).InvokeCommand(this, x => x.ViewModel.MoveLeftCommand);
}
protected override void OnClosing(CancelEventArgs e)
{
ViewModel.Dispose();
base.OnClosing(e);
}
}
一个 AppViewModel
public class AppViewModel : ReactiveObject, IDisposable
{
public ReactiveCommand<bool, bool> MoveLeftCommand { get; protected set; }
public AppViewModel()
{
MoveLeftCommand = ReactiveCommand.CreateFromTask<bool, bool>(isPressed => _MoveLeft(isPressed));
MoveLeftCommand.Buffer(TimeSpan.FromMilliseconds(500))
.Do(x => _InterpretCommand(x))
.Subscribe(x => Console.WriteLine($"{TimeStamp} {string.Join(",", x)}"))
}
private Task<bool> _MoveLeft(bool isPressed)
{
return Task.Run(() => isPressed); // Just to set a breakpoint here really
}
private static void _InterpretCommand(IList<bool> listOfBools)
{
if (listOfBools == null || listOfBools.Count == 0)
{
return;
}
if (listOfBools.First() == false)
{
Console.WriteLine("Stop move");
return;
}
if (listOfBools.Count == 1 && listOfBools.First() == true)
{
Console.WriteLine("Start move");
return;
}
if (listOfBools.Count >= 2)
{
Console.WriteLine("Click move");
return;
}
}
}
而我的 MainWindow.xaml 真的只是
<Button x:Name="MoveLeftButton" Content="Left"/>
随机序列示例
var rands = new Random();
rands.Next();
var better = Observable.Generate(
true,
_ => true,
x => !x,
x => x,
_ => TimeSpan.FromMilliseconds(rands.Next(1000)))
.Take(20);
better.Buffer(TimeSpan.FromMilliseconds(500))
.Do(x => _InterpretCommand(x))
.Subscribe(x => Console.WriteLine($"{TimeStamp} {string.Join(",", x)}"));
static string TimeStamp => DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
这会产生输出
2017-10-06 19:11:54.231
Start move
2017-10-06 19:11:54.720 True
2017-10-06 19:11:55.220
Stop move
2017-10-06 19:11:55.719 False,True
Stop move
2017-10-06 19:11:56.221 False
Start move
2017-10-06 19:11:56.719 True
Stop move
2017-10-06 19:11:57.222 False
2017-10-06 19:11:57.719
Start move
2017-10-06 19:11:58.220 True
Stop move
2017-10-06 19:11:58.720 False
2017-10-06 19:11:59.219
Click move
2017-10-06 19:11:59.719 True,False
2017-10-06 19:12:00.217
Start move
2017-10-06 19:12:00.719 True
Stop move
2017-10-06 19:12:01.221 False
Click move
2017-10-06 19:12:01.722 True,False
Start move
2017-10-06 19:12:02.217 True
2017-10-06 19:12:02.722
Stop move
2017-10-06 19:12:03.220 False
2017-10-06 19:12:03.720
Start move
2017-10-06 19:12:04.217 True
Stop move
2017-10-06 19:12:04.722 False
Start move
2017-10-06 19:12:05.220 True
Stop move
2017-10-06 19:12:05.516 False
根据我有限的经验,我相信您应该能够在 WhenAnyValue
语句之后但在调用命令之前添加 Rx 扩展,例如 Throttle
或 Buffer
。
this.WhenAnyValue(x => x.MoveLeftButton.IsPressed)
.Buffer(TimeSpan.FromSeconds(1))
.InvokeCommand(this, x => x.ViewModel.MoveLeftCommand);
从这个答案中获得洞察力:
我把一些很棒的东西串起来了!
BufferWithClosingValue
来自链接的答案:
public static IObservable<IList<TSource>> BufferWithClosingValue<TSource>(
this IObservable<TSource> source,
TimeSpan maxTime,
TSource closingValue)
{
return source.GroupByUntil(_ => true,
g => g.Where(i => i.Equals(closingValue)).Select(_ => Unit.Default)
.Merge(Observable.Timer(maxTime).Select(_ => Unit.Default)))
.SelectMany(i => i.ToList());
}
随机序列示例:
var alternatingTrueFalse = Observable.Generate(
true,
_ => true,
x => !x,
x => x,
_ => TimeSpan.FromMilliseconds(new Random().Next(1000)))
.Take(40).Publish().RefCount();
var bufferedWithTime = alternatingTrueFalse.BufferWithClosingValue(TimeSpan.FromMilliseconds(500), false);
var clicks = bufferedWithTime.Where(x => x.Count() == 2).ThrottleFirst(TimeSpan.FromMilliseconds(500));
var holdStarts = bufferedWithTime.Where(x => x.Count() == 1 && x.First() == true);
var holdStops = bufferedWithTime.Where(x => x.Count() == 1 && x.First() == false);
clicks.Select(_ => "Click").DumpTimes("Clicks");
holdStarts.Select(_ => "Hold Start").DumpTimes("Hold Start");
holdStops.Select(_ => "Hold Stop").DumpTimes("Hold stop");
使用此答案中的 ThrottleFirst
/ SampleFirst
实现:
示例输出
2017-10-08 16:58:14.549 - Hold Start-->Hold Start :: 6
2017-10-08 16:58:15.032 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:15.796 - Clicks-->Click :: 7
2017-10-08 16:58:16.548 - Clicks-->Click :: 6
2017-10-08 16:58:17.785 - Hold Start-->Hold Start :: 5
2017-10-08 16:58:18.254 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:19.294 - Hold Start-->Hold Start :: 8
2017-10-08 16:58:19.728 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:20.186 - Clicks-->Click :: 6
这似乎没有任何其他尝试解决此问题时遇到的竞争条件问题,所以我喜欢它!
我正在尝试实现一个 UI 控件,用户可以在其中单击按钮让物体移动一点,或者按住按钮并在按住按钮的同时让物体移动.
假设我有 Task<Unit> StartMove()
、Task<Unit> StopMove()
和 Task<Unit> MoveStep()
。单击按钮应执行 MoveStep()
并且按住按钮应该开始移动,然后在释放按钮时立即停止移动。移动发生时应忽略快速点击(双击),并且每秒发送的 MoveStep 命令不应超过 2 倍。还需要一些故障安全装置,它会在出现错误或长时间超时(比如 5 分钟)后停止移动。
按钮按下由 Button 对象上的 属性 表示,当用户按下按钮时触发 true
值,释放时触发 false
值,这值在常规 WPF 按钮上称为 IsPressed。真值后跟假值不到一秒表示点击,真值后跟假值超过一秒表示保持(此秒值也可以调整为半秒)。
问题归结为获取以随机间隔到达的真/假值流(想想:猴子随机按下按钮)并从该流中确定按钮是被单击还是被按住。基于此,应触发操作:MoveStep
表示单击,StartMove
然后 StopMove
表示按钮按住。
工作代码片段
我终于找到了一些有用的东西。
到目前为止我有 MainWindow
public partial class MainWindow : Window, IViewFor<AppViewModel>
{
public AppViewModel ViewModel { get; set; }
object IViewFor.ViewModel { get => ViewModel; set => ViewModel = value as AppViewModel; }
public MainWindow()
{
ViewModel = new AppViewModel();
DataContext = ViewModel;
InitializeComponent();
this.WhenAnyValue(x => x.MoveLeftButton.IsPressed).InvokeCommand(this, x => x.ViewModel.MoveLeftCommand);
}
protected override void OnClosing(CancelEventArgs e)
{
ViewModel.Dispose();
base.OnClosing(e);
}
}
一个 AppViewModel
public class AppViewModel : ReactiveObject, IDisposable
{
public ReactiveCommand<bool, bool> MoveLeftCommand { get; protected set; }
public AppViewModel()
{
MoveLeftCommand = ReactiveCommand.CreateFromTask<bool, bool>(isPressed => _MoveLeft(isPressed));
MoveLeftCommand.Buffer(TimeSpan.FromMilliseconds(500))
.Do(x => _InterpretCommand(x))
.Subscribe(x => Console.WriteLine($"{TimeStamp} {string.Join(",", x)}"))
}
private Task<bool> _MoveLeft(bool isPressed)
{
return Task.Run(() => isPressed); // Just to set a breakpoint here really
}
private static void _InterpretCommand(IList<bool> listOfBools)
{
if (listOfBools == null || listOfBools.Count == 0)
{
return;
}
if (listOfBools.First() == false)
{
Console.WriteLine("Stop move");
return;
}
if (listOfBools.Count == 1 && listOfBools.First() == true)
{
Console.WriteLine("Start move");
return;
}
if (listOfBools.Count >= 2)
{
Console.WriteLine("Click move");
return;
}
}
}
而我的 MainWindow.xaml 真的只是
<Button x:Name="MoveLeftButton" Content="Left"/>
随机序列示例
var rands = new Random();
rands.Next();
var better = Observable.Generate(
true,
_ => true,
x => !x,
x => x,
_ => TimeSpan.FromMilliseconds(rands.Next(1000)))
.Take(20);
better.Buffer(TimeSpan.FromMilliseconds(500))
.Do(x => _InterpretCommand(x))
.Subscribe(x => Console.WriteLine($"{TimeStamp} {string.Join(",", x)}"));
static string TimeStamp => DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
这会产生输出
2017-10-06 19:11:54.231
Start move
2017-10-06 19:11:54.720 True
2017-10-06 19:11:55.220
Stop move
2017-10-06 19:11:55.719 False,True
Stop move
2017-10-06 19:11:56.221 False
Start move
2017-10-06 19:11:56.719 True
Stop move
2017-10-06 19:11:57.222 False
2017-10-06 19:11:57.719
Start move
2017-10-06 19:11:58.220 True
Stop move
2017-10-06 19:11:58.720 False
2017-10-06 19:11:59.219
Click move
2017-10-06 19:11:59.719 True,False
2017-10-06 19:12:00.217
Start move
2017-10-06 19:12:00.719 True
Stop move
2017-10-06 19:12:01.221 False
Click move
2017-10-06 19:12:01.722 True,False
Start move
2017-10-06 19:12:02.217 True
2017-10-06 19:12:02.722
Stop move
2017-10-06 19:12:03.220 False
2017-10-06 19:12:03.720
Start move
2017-10-06 19:12:04.217 True
Stop move
2017-10-06 19:12:04.722 False
Start move
2017-10-06 19:12:05.220 True
Stop move
2017-10-06 19:12:05.516 False
根据我有限的经验,我相信您应该能够在 WhenAnyValue
语句之后但在调用命令之前添加 Rx 扩展,例如 Throttle
或 Buffer
。
this.WhenAnyValue(x => x.MoveLeftButton.IsPressed)
.Buffer(TimeSpan.FromSeconds(1))
.InvokeCommand(this, x => x.ViewModel.MoveLeftCommand);
从这个答案中获得洞察力: 我把一些很棒的东西串起来了!
BufferWithClosingValue
来自链接的答案:
public static IObservable<IList<TSource>> BufferWithClosingValue<TSource>(
this IObservable<TSource> source,
TimeSpan maxTime,
TSource closingValue)
{
return source.GroupByUntil(_ => true,
g => g.Where(i => i.Equals(closingValue)).Select(_ => Unit.Default)
.Merge(Observable.Timer(maxTime).Select(_ => Unit.Default)))
.SelectMany(i => i.ToList());
}
随机序列示例:
var alternatingTrueFalse = Observable.Generate(
true,
_ => true,
x => !x,
x => x,
_ => TimeSpan.FromMilliseconds(new Random().Next(1000)))
.Take(40).Publish().RefCount();
var bufferedWithTime = alternatingTrueFalse.BufferWithClosingValue(TimeSpan.FromMilliseconds(500), false);
var clicks = bufferedWithTime.Where(x => x.Count() == 2).ThrottleFirst(TimeSpan.FromMilliseconds(500));
var holdStarts = bufferedWithTime.Where(x => x.Count() == 1 && x.First() == true);
var holdStops = bufferedWithTime.Where(x => x.Count() == 1 && x.First() == false);
clicks.Select(_ => "Click").DumpTimes("Clicks");
holdStarts.Select(_ => "Hold Start").DumpTimes("Hold Start");
holdStops.Select(_ => "Hold Stop").DumpTimes("Hold stop");
使用此答案中的 ThrottleFirst
/ SampleFirst
实现:
示例输出
2017-10-08 16:58:14.549 - Hold Start-->Hold Start :: 6
2017-10-08 16:58:15.032 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:15.796 - Clicks-->Click :: 7
2017-10-08 16:58:16.548 - Clicks-->Click :: 6
2017-10-08 16:58:17.785 - Hold Start-->Hold Start :: 5
2017-10-08 16:58:18.254 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:19.294 - Hold Start-->Hold Start :: 8
2017-10-08 16:58:19.728 - Hold stop-->Hold Stop :: 7
2017-10-08 16:58:20.186 - Clicks-->Click :: 6
这似乎没有任何其他尝试解决此问题时遇到的竞争条件问题,所以我喜欢它!