【发布时间】:2017-10-09 01:10:06
【问题描述】:
我正在尝试实现一个 UI 控件,用户可以在该控件中单击按钮以使事物稍微移动,或者按住按钮并在按住按钮的同时使事物移动。
假设我有Task<Unit> StartMove()、Task<Unit> StopMove() 和Task<Unit> MoveStep()。按钮单击应该执行MoveStep()
并且按钮保持应该开始移动,然后在释放按钮时立即停止移动。移动发生时应忽略快速单击(双击),每秒发送的 MoveStep 命令不应超过 2 次。还需要一些故障保护,以在错误或长时间超时(例如 5 分钟)后停止移动。
按钮按下由 Button 对象上的一个属性表示,当用户按下按钮时会触发一个 true 值,当它被释放时会触发一个 false,这个值在常规 WPF 按钮上称为 IsPressed。真值后跟假值不到一秒代表点击,真值后跟假值超过一秒代表保持(这个秒值也可以调整到半秒)。
问题归结为获取以随机间隔到达的此类真/假值的流(想想:猴子正在随机按下按钮),并从该流中确定按钮是否被单击或按住。基于此,应触发操作:MoveStep 用于单击,StartMove 然后StopMove 用于按住按钮。
工作代码sn-p
我终于得到了一些可行的东西。
到目前为止,我有 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
【问题讨论】:
-
我阅读了更多的 Reactive UI 文档(其中一些非常令人困惑,为什么会有聊天日志?) - 似乎我可以在视图本身中使用 BindCommand 方法,我'当我有机会时,我必须调查一下reactiveui.net/docs/handbook/commands/binding-commands
标签: c# system.reactive reactiveui