本文整理汇总了C#中Subject.ToReadOnlyReactiveProperty方法的典型用法代码示例。如果您正苦于以下问题:C# Subject.ToReadOnlyReactiveProperty方法的具体用法?C# Subject.ToReadOnlyReactiveProperty怎么用?C# Subject.ToReadOnlyReactiveProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subject
的用法示例。
在下文中一共展示了Subject.ToReadOnlyReactiveProperty方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NormalPattern
public void NormalPattern()
{
var s = new Subject<string>();
var rp = s.ToReadOnlyReactiveProperty(eventScheduler: Scheduler.CurrentThread);
var buffer = new List<string>();
rp.Subscribe(buffer.Add);
rp.Value.IsNull();
buffer.Count.Is(1);
buffer[0].IsNull();
s.OnNext("Hello");
rp.Value.Is("Hello");
buffer.Count.Is(2);
buffer.Is(default(string), "Hello");
s.OnNext("Hello");
rp.Value.Is("Hello");
buffer.Count.Is(2); // distinct until changed.
}
示例2: NormalPatternNoDistinctUntilChanged
public void NormalPatternNoDistinctUntilChanged()
{
var s = new Subject<string>();
var rp = s.ToReadOnlyReactiveProperty(
mode: ReactivePropertyMode.RaiseLatestValueOnSubscribe,
eventScheduler: Scheduler.CurrentThread);
var buffer = new List<string>();
rp.Subscribe(buffer.Add);
rp.Value.IsNull();
buffer.Count.Is(1);
buffer[0].IsNull();
s.OnNext("Hello");
rp.Value.Is("Hello");
buffer.Count.Is(2);
buffer.Is(default(string), "Hello");
s.OnNext("Hello");
rp.Value.Is("Hello");
buffer.Count.Is(3); // not distinct until changed.
}
示例3: MultiSubscribeTest
public void MultiSubscribeTest()
{
var s = new Subject<string>();
var rp = s.ToReadOnlyReactiveProperty();
var buffer1 = new List<string>();
rp.Subscribe(buffer1.Add);
buffer1.Count.Is(1);
s.OnNext("Hello world");
buffer1.Count.Is(2);
buffer1.Is(default(string), "Hello world");
var buffer2 = new List<string>();
rp.Subscribe(buffer2.Add);
buffer1.Is(default(string), "Hello world");
buffer2.Is("Hello world");
s.OnNext("ReactiveProperty");
buffer1.Is(default(string), "Hello world", "ReactiveProperty");
buffer2.Is("Hello world", "ReactiveProperty");
}
示例4: ToReadOnlyReactivePropertyValueType
public void ToReadOnlyReactivePropertyValueType()
{
SetScehdulerForImport();
{
var source = new Subject<int>();
var rp = source.ToReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
}
{
var source = new Subject<int>();
var rp = source.ToSequentialReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100, 100);
}
{
var source = new Subject<int>();
var rp = source.ToReadOnlyReactiveProperty(0);
var result = rp.Record();
result.Values.IsCollection(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
}
{
var source = new Subject<int>();
var rp = source.ToSequentialReadOnlyReactiveProperty(0);
var result = rp.Record();
result.Values.IsCollection(0);
source.OnNext(0);
result.Values.IsCollection(0, 0);
source.OnNext(10);
result.Values.IsCollection(0, 0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 0, 10, 100, 100);
}
UniRx.Scheduler.SetDefaultForUnity();
}
示例5: ToReadOnlyReactivePropertyClassType
public void ToReadOnlyReactivePropertyClassType()
{
SetScehdulerForImport();
{
var source = new Subject<string>();
var rp = source.ToReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(null);
result.Values.IsCollection((string)null);
source.OnNext("a");
result.Values.IsCollection((string)null, "a");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
}
{
var source = new Subject<string>();
var rp = source.ToSequentialReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(null);
result.Values.IsCollection((string)null);
source.OnNext("a");
result.Values.IsCollection((string)null, "a");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b", "b");
}
{
var source = new Subject<string>();
var rp = source.ToReadOnlyReactiveProperty("z");
var result = rp.Record();
result.Values.IsCollection("z");
source.OnNext("z");
result.Values.IsCollection("z");
source.OnNext("a");
result.Values.IsCollection("z", "a");
source.OnNext("b");
result.Values.IsCollection("z", "a", "b");
source.OnNext("b");
result.Values.IsCollection("z", "a", "b");
source.OnNext(null);
result.Values.IsCollection("z", "a", "b", null);
source.OnNext(null);
result.Values.IsCollection("z", "a", "b", null);
}
{
var source = new Subject<string>();
var rp = source.ToSequentialReadOnlyReactiveProperty("z");
var result = rp.Record();
result.Values.IsCollection("z");
source.OnNext("z");
result.Values.IsCollection("z", "z");
source.OnNext("a");
result.Values.IsCollection("z", "z", "a");
source.OnNext("b");
result.Values.IsCollection("z", "z", "a", "b");
source.OnNext("b");
result.Values.IsCollection("z", "z", "a", "b", "b");
source.OnNext(null);
result.Values.IsCollection("z", "z", "a", "b", "b", null);
source.OnNext(null);
result.Values.IsCollection("z", "z", "a", "b", "b", null, null);
}
UniRx.Scheduler.SetDefaultForUnity();
}
示例6: FinishedSourceToReadOnlyReactiveProperty
public void FinishedSourceToReadOnlyReactiveProperty()
{
SetScehdulerForImport();
// pattern of OnCompleted
{
var source = Observable.Return(9);
var rxProp = source.ToReadOnlyReactiveProperty();
var notifications = rxProp.Record().Notifications;
notifications.IsCollection(Notification.CreateOnNext(9), Notification.CreateOnCompleted<int>());
rxProp.Record().Notifications.IsCollection(
Notification.CreateOnNext(9),
Notification.CreateOnCompleted<int>());
}
// pattern of OnError
{
// after
{
var ex = new Exception();
var source = Observable.Throw<int>(ex);
var rxProp = source.ToReadOnlyReactiveProperty();
var notifications = rxProp.Record().Notifications;
notifications.IsCollection(Notification.CreateOnError<int>(ex));
rxProp.Record().Notifications.IsCollection(Notification.CreateOnError<int>(ex));
}
// immediate
{
var ex = new Exception();
var source = new Subject<int>();
var rxProp = source.ToReadOnlyReactiveProperty();
var record = rxProp.Record();
source.OnError(new Exception());
var notifications = record.Notifications;
notifications.Count.Is(1);
notifications[0].Kind.Is(NotificationKind.OnError);
rxProp.Record().Notifications[0].Kind.Is(NotificationKind.OnError);
}
}
UniRx.Scheduler.SetDefaultForUnity();
}
示例7: PropertyChangedTest
public void PropertyChangedTest()
{
var s = new Subject<string>();
var rp = s.ToReadOnlyReactiveProperty(eventScheduler: Scheduler.CurrentThread);
var buffer = new List<string>();
rp.PropertyChanged += (_, args) =>
{
buffer.Add(args.PropertyName);
};
buffer.Count.Is(0);
s.OnNext("Hello");
buffer.Count.Is(1);
s.OnNext("Hello");
buffer.Count.Is(1);
s.OnNext("World");
buffer.Count.Is(2);
}
示例8: PropertyChangedNoDistinctUntilChangedTest
public void PropertyChangedNoDistinctUntilChangedTest()
{
var s = new Subject<string>();
var rp = s.ToReadOnlyReactiveProperty(
mode: ReactivePropertyMode.RaiseLatestValueOnSubscribe,
eventScheduler: Scheduler.CurrentThread);
var buffer = new List<string>();
rp.PropertyChanged += (_, args) =>
{
buffer.Add(args.PropertyName);
};
buffer.Count.Is(0);
s.OnNext("Hello");
buffer.Count.Is(1);
s.OnNext("Hello");
buffer.Count.Is(2);
s.OnNext("World");
buffer.Count.Is(3);
}
示例9: MainViewModel
public MainViewModel()
{
var powerDistribution = ((App)App.Current).PowerDistribution;
var powerDistributionLogger = ((App) Application.Current).PowerDistributionLogger;
var viewSettings = ((App) Application.Current).ViewSettings;
// View Settings
this.PlotForeground = viewSettings
.ObserveProperty(self => self.PlotStyle).Select(style => style.ObserveProperty(self => self.PlotForeground))
.Switch()
.Select(color => new SolidColorBrush(color))
.ToReadOnlyReactiveProperty()
.AddTo(this.disposables);
this.PlotBackground = viewSettings
.ObserveProperty(self => self.PlotStyle).Select(style => style.ObserveProperty(self => self.PlotBackground))
.Switch()
.Select(color => new SolidColorBrush(color))
.ToReadOnlyReactiveProperty()
.AddTo(this.disposables);
this.CurrentSensors = powerDistribution.Sensors.ToReadOnlyReactiveCollection(sensor => new CurrentSensorViewModel(sensor)).AddTo(this.disposables);
this.TotalCurrent = powerDistribution.ObserveProperty(self => self.TotalCurrent).ToReadOnlyReactiveProperty(mode:ReactivePropertyMode.RaiseLatestValueOnSubscribe)
.AddTo(this.disposables);
this.Capacity = powerDistribution.ObserveProperty(self => self.Capacity).ToReadOnlyReactiveProperty().AddTo(this.disposables);
this.Usage = Observable.CombineLatest(
this.TotalCurrent,
this.Capacity,
(totalCurrent, capacity) => capacity > 0 ? totalCurrent / capacity : 0)
.ToReadOnlyReactiveProperty()
.AddTo(this.disposables);
this.AlertState = Observable.CombineLatest(
powerDistribution.ObserveProperty(self => self.IsWarningCondition),
powerDistribution.ObserveProperty(self => self.IsCriticalCondition),
(isWarning, isCritical) => isCritical ? ErrorAlertState : (isWarning ? WarningAlertState : NormalAlertState))
.ToReadOnlyReactiveProperty()
.AddTo(this.disposables);
this.CurrentPlotType = new ReactiveProperty<PlotType>().AddTo(this.disposables);
this.IsLive = this.CurrentPlotType.Select(type => type == PlotType.Live).ToReadOnlyReactiveProperty().AddTo(this.disposables);
this.IsCustom = this.CurrentPlotType.Select(type => type == PlotType.Custom).ToReadOnlyReactiveProperty().AddTo(this.disposables);
this.IsHistroy = this.IsLive.Select(value => !value).ToReadOnlyReactiveProperty().AddTo(this.disposables);
this.CurrentPlotModel = new OxyPlot.PlotModel()
{
Axes =
{
new OxyPlot.Axes.LinearAxis() { Unit = "A", Position = OxyPlot.Axes.AxisPosition.Left, Minimum = 0 },
new OxyPlot.Axes.DateTimeAxis() { Unit = "Time", Position = OxyPlot.Axes.AxisPosition.Bottom },
},
};
// Change plot model colors.
viewSettings
.ObserveProperty(self => self.PlotStyle).Select(style => style.ObserveProperty(self => self.PlotForeground))
.Switch()
.Subscribe(color_ =>
{
var color = color_.ToOxyColor();
this.CurrentPlotModel.TextColor = color;
this.CurrentPlotModel.PlotAreaBorderColor = color;
foreach (var axis in this.CurrentPlotModel.Axes)
{
axis.MajorGridlineColor = color;
axis.MinorGridlineColor = color;
axis.TextColor = color;
axis.AxislineColor = color;
axis.TicklineColor = color;
}
})
.AddTo(this.disposables);
var totalCurrentSeries = new OxyPlot.Series.LineSeries()
{
Title = "Total Current",
};
// Change plot series color.
viewSettings
.ObserveProperty(self => self.PlotStyle).Select(style => style.ObserveProperty(self => self.SeriesColor))
.Switch()
.Subscribe(color => totalCurrentSeries.Color = color.ToOxyColor())
.AddTo(this.disposables);
this.CurrentPlotModel.Series.Add(totalCurrentSeries);
var currentHistory = this.TotalCurrent
.Select(Value => new { Value, TimeStamp = DateTime.Now })
.ToObservationHistory(Observable.Empty<Unit>(), pair => (DateTime.Now - pair.TimeStamp) >= TimeSpan.FromSeconds(60))
.AddTo(this.disposables);
var livePlotSource = currentHistory.HistoryChanged
.Select(_ =>
{
var history = currentHistory.GetHistory();
return history.Select(pair => new OxyPlot.DataPoint() {X = OxyPlot.Axes.DateTimeAxis.ToDouble(pair.TimeStamp), Y = pair.Value});
});
//.........这里部分代码省略.........
示例10: ToReadOnlyReactivePropertyValueType
public void ToReadOnlyReactivePropertyValueType()
{
{
var source = new Subject<int>();
var rp = source.ToReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
}
{
var source = new Subject<int>();
var rp = source.ToSequentialReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100, 100);
}
{
var source = new Subject<int>();
var rp = source.ToReadOnlyReactiveProperty(0);
var result = rp.Record();
result.Values.IsCollection(0);
source.OnNext(0);
result.Values.IsCollection(0);
source.OnNext(10);
result.Values.IsCollection(0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 10, 100);
}
{
var source = new Subject<int>();
var rp = source.ToSequentialReadOnlyReactiveProperty(0);
var result = rp.Record();
result.Values.IsCollection(0);
source.OnNext(0);
result.Values.IsCollection(0, 0);
source.OnNext(10);
result.Values.IsCollection(0, 0, 10);
source.OnNext(100);
result.Values.IsCollection(0, 0, 10, 100);
source.OnNext(100);
result.Values.IsCollection(0, 0, 10, 100, 100);
}
}
示例11: ToReadOnlyReactivePropertyClassType
public void ToReadOnlyReactivePropertyClassType()
{
{
var source = new Subject<string>();
var rp = source.ToReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(null);
result.Values.IsCollection((string)null);
source.OnNext("a");
result.Values.IsCollection((string)null, "a");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
}
{
var source = new Subject<string>();
var rp = source.ToSequentialReadOnlyReactiveProperty();
var result = rp.Record();
result.Values.Count.Is(0);
source.OnNext(null);
result.Values.IsCollection((string)null);
source.OnNext("a");
result.Values.IsCollection((string)null, "a");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b");
source.OnNext("b");
result.Values.IsCollection((string)null, "a", "b", "b");
}
{
var source = new Subject<string>();
var rp = source.ToReadOnlyReactiveProperty("z");
var result = rp.Record();
result.Values.IsCollection("z");
source.OnNext("z");
result.Values.IsCollection("z");
source.OnNext("a");
result.Values.IsCollection("z", "a");
source.OnNext("b");
result.Values.IsCollection("z", "a", "b");
source.OnNext("b");
result.Values.IsCollection("z", "a", "b");
source.OnNext(null);
result.Values.IsCollection("z", "a", "b", null);
source.OnNext(null);
result.Values.IsCollection("z", "a", "b", null);
}
{
var source = new Subject<string>();
var rp = source.ToSequentialReadOnlyReactiveProperty("z");
var result = rp.Record();
result.Values.IsCollection("z");
source.OnNext("z");
result.Values.IsCollection("z", "z");
source.OnNext("a");
result.Values.IsCollection("z", "z", "a");
source.OnNext("b");
result.Values.IsCollection("z", "z", "a", "b");
source.OnNext("b");
result.Values.IsCollection("z", "z", "a", "b", "b");
source.OnNext(null);
result.Values.IsCollection("z", "z", "a", "b", "b", null);
source.OnNext(null);
result.Values.IsCollection("z", "z", "a", "b", "b", null, null);
}
}