我正在玩反应式UI,我想在进程完成后显示一条消息,并在一段时间(4秒(后隐藏此消息。如果消息的发送速度快于隐藏时间,则应重置超时,因此在显示/更新最后一条消息后 4 秒后,消息始终处于隐藏状态。如果最后一条消息与上一条消息相同,则隐藏时间也应延长。
目前我有这段代码,它可以做我想要的,但对我来说它看起来太麻烦了。我只是在试验RxUI,所以大多数时候我不知道我在做什么。有没有更好的方法来实现这一目标?
public class MainViewModel: ReactiveObject
{
// Message to be shown.
private string message;
public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value);
// Flag for the UI, if the message panel should be visible.
private ObservableAsPropertyHelper<bool> isMessageVisible;
public bool IsMessageVisible => isMessageVisible.Value;
// Command that runs async process, the result is the message to be shown.
public ReactiveCommand<Unit, string> Run { get; private set; }
public MainViewModel()
{
var msg = this.WhenAnyValue(x => x.Message, x => !string.IsNullOrEmpty(x));
// If message changes, after 4 seconds return false, causing hiding the message panel.
var hide = msg.Select(x => false).Throttle(TimeSpan.FromSeconds(4), RxApp.MainThreadScheduler);
// Merge both sequences into one output property.
Observable.Merge(msg, hide).ToProperty(this, x => x.IsMessageVisible, out isMessageVisible);
Run = ReactiveCommand.CreateFromObservable(() => Observable.StartAsync(Process));
// Merge various message sources and set message property. Set Message = null to force property change.
Observable.Merge(Run, Run.ThrownExceptions.Select(x => x.Message)).Subscribe(x => { Message = null; Message = x; });
}
...
}
我个人会这样声明isMessageVisible
:
isMessageVisible = this
.WhenAnyValue(x => x.Message, x => !string.IsNullOrEmpty(x))
.Select(showMessage => Observable.Return(showMessage).Concat(Observable.Return(false).Delay(4, RxApp.MainThreadScheduler)))
.Switch()
.ToProperty(this, x => x.IsMessageVisible);
它将所有逻辑放在一个管道中,我认为这更具可读性。
除了重写isMessageVisible
,我首先会更改消息的显示方式。
我会放弃isMessageVisible
,只有Message
属性。何时string.IsNullOrEmpty(Message) == true
在 UI 中隐藏消息,以及string.IsNullOrEmpty(Message) == false
何时显示 UI,以及何时显示 UI。对于 RxUI 绑定,这将是这样的:
this.OneWayBind(ViewModel, vm => vm.Message, v => v.Message.Text, message => !string.IsNullOrWhitespace(message));
然后我会在视图模型中执行此操作:
public class MainViewModel: ReactiveObject
{
// Message to be shown.
private ObservableAsPropertyHelper<string> message;
public string Message => message.Value;
// Command that runs async process, the result is the message to be shown.
public ReactiveCommand<Unit, string> Run { get; private set; }
public MainViewModel()
{
Run = ReactiveCommand.CreateFromObservable(() => Observable.StartAsync(Process));
// Merge various message sources and set message property.
message = Observable.Merge(Run, Run.ThrownExceptions.Select(x => x.Message))
.Select(msg => Observable.Return(msg).Concat(Observable.Return("").Delay(4, RxApp.MainThreadScheduler))) // 1
.Switch() // 2
.ToProperty(this, x => x.Message);
}
}
- 这将立即返回新消息,然后在 4 秒后返回一个空字符串
- 这将仅订阅
Select
返回的最新可观察量。如果发送新消息,则先前的可观察量将不会发送空字符串
如果您有多个命令返回消息,则可以添加一个方便的函数来减少代码量:
private IObservable<string> CreateMessageStream(params ReactiveCommand<Unit, string> commands)
=> Observable.Merge(commands.SelectMany(command => new IObservable<string>[] { command, command.ThrownExceptions.Select(x => x.Message) }))
.Select(msg => Observable.Return(msg).Concat(Observable.Return("").Delay(4, RxApp.MainThreadScheduler)))
.Switch()
然后你可以像这样声明message
:
message = CreateMessageStream(Run, Walk, Crawl)
.ToProperty(this, x => x.Message);
其中Run
、Walk
和Crawl
都是ReactiveCommand
。