当 属性 在 class 的 setter 之外更改时,如何让 ReactiveUI WhenAnyValue 发出
How to get ReactiveUI WhenAnyValue to emit when property is changed outside the setter of class
我想知道我如何仍然可以使用 属性 触发 ReactiveUI 的 WhenAnyValue,它可以在 class 中的 属性 的 setter 之外更改。请原谅这个例子只是为了快速说明问题。假设我有两个 classes,一个是人,另一个是地址。
public class Address : ReactiveObject
{
[Reactive]
public string Street1 { get; set; }
[Reactive]
public string Street2 { get; set; }
[Reactive]
public string State { get; set; }
[Reactive]
public string City { get; set; }
[Reactive]
public string ZipCode { get; set; }
}
public class Person : ReactiveObject
{
[Reactive]
public string Name { get; set; }
[Reactive]
public Address Address { get; set; }
public Person(Address address)
{
Address = address;
address.Changed .Subscribe(_ =>
{
//Tried this way
this.RaisePropertyChanged(_.PropertyName);
//Also tried this way
this.RaisePropertyChanged(nameof(Address));
});
this.Changed.Subscribe(_ =>
{
Console.WriteLine($"Person Property: {_.PropertyName} changed");
});
this.WhenAnyValue(_ => _.Name, _ => _.Address, (a, b) => "Emit").Subscribe(Console.WriteLine);
}
}
private static void Main(string[] args)
{
var address = new Address
{
Street1 = "Street1",
Street2 = "Street2",
City = "City",
State = "LA",
ZipCode = "11111"
};
var person = new Person(address);
person.Name = "Name";
Task.Delay(3000).Wait();
address.City = "CityChanged";
Console.ReadLine();
}
我认为我只需要订阅 Address Changed Observable,然后从 Person Raise属性Changed 中获取地址 属性。它确实会向 Person 的 Changed Observable 发出信号,并且其行为符合我的预期,但是 Person 中的 WhenAnyValue Observable 在那种情况下永远不会发出。
我想通了这个问题。问题是我必须将所有嵌套属性添加到 WhenAnyValue。在我的示例中,如果我更改此行
this.WhenAnyValue(_ => _.Name, _ => _.Address, (a, b) => "Emit").Subscribe(Console.WriteLine);
到
this.WhenAnyValue(_ => _.Name, _ => _.Address.City, (a, b) => "Emit").Subscribe(Console.WriteLine);
它发射。那里的关键是将它从 _.Address 更改为 _.Address.City。此外,它仍然无需订阅 Address.Changed Observable 就可以发射,这更好。
我想知道我如何仍然可以使用 属性 触发 ReactiveUI 的 WhenAnyValue,它可以在 class 中的 属性 的 setter 之外更改。请原谅这个例子只是为了快速说明问题。假设我有两个 classes,一个是人,另一个是地址。
public class Address : ReactiveObject
{
[Reactive]
public string Street1 { get; set; }
[Reactive]
public string Street2 { get; set; }
[Reactive]
public string State { get; set; }
[Reactive]
public string City { get; set; }
[Reactive]
public string ZipCode { get; set; }
}
public class Person : ReactiveObject
{
[Reactive]
public string Name { get; set; }
[Reactive]
public Address Address { get; set; }
public Person(Address address)
{
Address = address;
address.Changed .Subscribe(_ =>
{
//Tried this way
this.RaisePropertyChanged(_.PropertyName);
//Also tried this way
this.RaisePropertyChanged(nameof(Address));
});
this.Changed.Subscribe(_ =>
{
Console.WriteLine($"Person Property: {_.PropertyName} changed");
});
this.WhenAnyValue(_ => _.Name, _ => _.Address, (a, b) => "Emit").Subscribe(Console.WriteLine);
}
}
private static void Main(string[] args)
{
var address = new Address
{
Street1 = "Street1",
Street2 = "Street2",
City = "City",
State = "LA",
ZipCode = "11111"
};
var person = new Person(address);
person.Name = "Name";
Task.Delay(3000).Wait();
address.City = "CityChanged";
Console.ReadLine();
}
我认为我只需要订阅 Address Changed Observable,然后从 Person Raise属性Changed 中获取地址 属性。它确实会向 Person 的 Changed Observable 发出信号,并且其行为符合我的预期,但是 Person 中的 WhenAnyValue Observable 在那种情况下永远不会发出。
我想通了这个问题。问题是我必须将所有嵌套属性添加到 WhenAnyValue。在我的示例中,如果我更改此行
this.WhenAnyValue(_ => _.Name, _ => _.Address, (a, b) => "Emit").Subscribe(Console.WriteLine);
到
this.WhenAnyValue(_ => _.Name, _ => _.Address.City, (a, b) => "Emit").Subscribe(Console.WriteLine);
它发射。那里的关键是将它从 _.Address 更改为 _.Address.City。此外,它仍然无需订阅 Address.Changed Observable 就可以发射,这更好。