文档
安装
使用 ReactiveUI 需要安装平台所对应的包。
比如开发 WPF 应用程序需要下载 ReactiveUI 和 ReactiveUI.WPF。
ViewModel
自定义的 ViewModel 类应该继承 ReactiveObject 类。
public class ExampleViewModel : ReactiveObject { }
可读可写的属性
private string name;
public string Name
{
get => name;
set => this.RaiseAndSetIfChanged(ref name, value);
}
只读属性
public ReactiveCommand<Object> PostTweet { get; }
PostTweet = ReactiveCommand.Create(/*...*/);
只写属性
private readonly ObservableAsPropertyHelper<string> firstName;
public string FirstName => firstName.Value;
this.WhenAnyValue(x => x.Name)
.Where(x => !string.IsNullOrEmpty(x))
.Select(x => x.Split(' ')[0])
.ToProperty(this, x => x.FirstName, out firstName);
下载并使用 ReactiveUI.Fody 后代码可以简化
可读可写的属性
[Reactive]
public string Name;
只写属性
public string FullName { [ObservableAsProperty] get; }
this.WhenAnyValue(x => x.Name)
.Where(x => !string.IsNullOrEmpty(x))
.Select(x => x.Split(' ')[0])
.ToPropertyEx(this, x => x.FullName);
Command
通过调用 ReactiveCommand 类的静态方法创建命令
- CreateFromObservable()
- CreateFromTask()
- Create()
- CreateCombined()