重置 ReactiveUI ObservableAsPropertyHelper 值
Reset ReactiveUI ObservableAsPropertyHelper Value
我正在使用 ReactiveUI ReactiveObject 视图模型构建一个简单的 DateTime 计算器。
它有一个计算命令,可以计算一个值并正确更新由 ObservableAsPropertyHelper 字段支持的结果。
我还有一个响应式UI命令,它应该重置所有 UI 值。
我 运行 遇到的问题是如何重置只读“结果”值。
public class VM : ReactiveObject
{
private readonly ObservableAsPropertyHelper<DateTime?> _result;
[Reactive]
public DateTime Start { get; set; } = DateTime.Now;
[Reactive]
public int Days { get; set; } = 1;
public DateTime? Result => _result.Value;
public ReactiveCommand<Unit, DateTime?> CalculateCommand { get; }
public ReactiveCommand<Unit, Unit> ResetCommand { get; }
public VM()
{
CalculateCommand = ReactiveCommand
.CreateFromTask<Unit, DateTime?>((_, cancellationToken) => CalculateAsync(cancellationToken));
_result = CalculateCommand.ToProperty(this, nameof(Result));
// everything above this works.
ResetCommand = ReactiveCommand.Create(() =>
{
Start = DateTime.Now;
Days = 1;
// how do I reset "_result" or "Result" back to null???
});
}
private Task<DateTime?> CalculateAsync(CancellationToken _)
{
// to be replaced by an API call later.
return Task.FromResult<DateTime?>(Start.AddDays(Days));
}
}
如何重置由 ObservableAsPropertyHelper 实例支持的值?
我自己想出来了:
namespace WpfApp4;
using System.Reactive.Linq;
using System.Threading.Tasks;
using ReactiveUI;
using System;
using System.Reactive;
public class MainViewModel : ReactiveObject
{
public ReactiveCommand<Unit, DateTime?> StartCommand { get; }
public ReactiveCommand<Unit, DateTime?> ResetCommand { get; }
private DateTime _start = DateTime.Now;
private int _processingDays = 1;
private readonly ObservableAsPropertyHelper<DateTime?> _result;
public DateTime? Result => _result.Value;
public DateTime Start
{
get => _start;
set => this.RaiseAndSetIfChanged(ref _start, value);
}
public int ProcessingDays
{
get => _processingDays;
set => this.RaiseAndSetIfChanged(ref _processingDays, value);
}
public MainViewModel()
{
StartCommand = ReactiveCommand.CreateFromTask<DateTime?>(() => Task.FromResult<DateTime?>(Start.AddDays(ProcessingDays)));
ResetCommand = ReactiveCommand.Create<DateTime?>(execute: () => null);
_ = ResetCommand.Subscribe(onNext: (_) =>
{
Start = DateTime.Now;
ProcessingDays = 1;
});
_result = StartCommand.Merge(ResetCommand).ToProperty(this, nameof(Result));
}
}
我正在使用 ReactiveUI ReactiveObject 视图模型构建一个简单的 DateTime 计算器。
它有一个计算命令,可以计算一个值并正确更新由 ObservableAsPropertyHelper
我还有一个响应式UI命令,它应该重置所有 UI 值。
我 运行 遇到的问题是如何重置只读“结果”值。
public class VM : ReactiveObject
{
private readonly ObservableAsPropertyHelper<DateTime?> _result;
[Reactive]
public DateTime Start { get; set; } = DateTime.Now;
[Reactive]
public int Days { get; set; } = 1;
public DateTime? Result => _result.Value;
public ReactiveCommand<Unit, DateTime?> CalculateCommand { get; }
public ReactiveCommand<Unit, Unit> ResetCommand { get; }
public VM()
{
CalculateCommand = ReactiveCommand
.CreateFromTask<Unit, DateTime?>((_, cancellationToken) => CalculateAsync(cancellationToken));
_result = CalculateCommand.ToProperty(this, nameof(Result));
// everything above this works.
ResetCommand = ReactiveCommand.Create(() =>
{
Start = DateTime.Now;
Days = 1;
// how do I reset "_result" or "Result" back to null???
});
}
private Task<DateTime?> CalculateAsync(CancellationToken _)
{
// to be replaced by an API call later.
return Task.FromResult<DateTime?>(Start.AddDays(Days));
}
}
如何重置由 ObservableAsPropertyHelper 实例支持的值?
我自己想出来了:
namespace WpfApp4;
using System.Reactive.Linq;
using System.Threading.Tasks;
using ReactiveUI;
using System;
using System.Reactive;
public class MainViewModel : ReactiveObject
{
public ReactiveCommand<Unit, DateTime?> StartCommand { get; }
public ReactiveCommand<Unit, DateTime?> ResetCommand { get; }
private DateTime _start = DateTime.Now;
private int _processingDays = 1;
private readonly ObservableAsPropertyHelper<DateTime?> _result;
public DateTime? Result => _result.Value;
public DateTime Start
{
get => _start;
set => this.RaiseAndSetIfChanged(ref _start, value);
}
public int ProcessingDays
{
get => _processingDays;
set => this.RaiseAndSetIfChanged(ref _processingDays, value);
}
public MainViewModel()
{
StartCommand = ReactiveCommand.CreateFromTask<DateTime?>(() => Task.FromResult<DateTime?>(Start.AddDays(ProcessingDays)));
ResetCommand = ReactiveCommand.Create<DateTime?>(execute: () => null);
_ = ResetCommand.Subscribe(onNext: (_) =>
{
Start = DateTime.Now;
ProcessingDays = 1;
});
_result = StartCommand.Merge(ResetCommand).ToProperty(this, nameof(Result));
}
}