10

次のビュー モデルがあるとします。

public class AddressViewModel : ReactiveObject
{
    private string line;

    public string Line
    {
        get { return this.line; }
        set { this.RaiseAndSetIfChanged(x => x.Line, ref this.line, value); }
    }
}

public class EmployeeViewModel : ReactiveObject
{
    private AddressViewModel address;

    public AddressViewModel Address
    {
        get { return this.address; }
        set { this.RaiseAndSetIfChanged(x => x.Address, ref this.address, value); }
    }
}

ここEmployeeViewModelで、 の最新の値を持つプロパティを公開したいとしますAddress.Line

public EmployeeViewModel()
{
    this.changes = this.ObservableForProperty(x => x.Address)
        .Select(x => x.Value.Line)
        .ToProperty(this, x => x.Changes);
}

private readonly ObservableAsPropertyHelper<string> changes;
public string Changes
{
    get { return this.changes.Value; }
}

Addressこれは、プロパティに変更が加えられた場合にのみチェックされLineますが、内部への変更Addressが発生した場合にはチェックされません。代わりにこれを行うと:

public EmployeeViewModel()
{
    this.changes = this.Address.Changed
        .Where(x => x.PropertyName == "Line")
        .Select(x => this.Address.Line)        // x.Value is null here, for some reason, so I use this.Address.Line instead
        .ToProperty(this, x => x.Changes);
}

Lineこれは、現在の範囲内で変更が発生した場合にのみティックしAddressViewModelますが、新しい設定をまったく考慮していませんAddressViewModel(また、に対応していませんnull Address)。

私は、この問題を解決するための正しいアプローチに頭を悩ませようとしています。私は RxUI を初めて使用するので、明らかな何かが欠けている可能性があります。アドレスの変更を手動でフックして、2 番目のサブスクリプションをセットアップすることもできますが、これは見苦しく、エラーが発生しやすいようです

これを達成するために使用すべき標準パターンまたはヘルパーはありますか?

これを試すためにコピー/貼り付けできるコードを次に示します。

ViewModels.cs :

namespace RxUITest
{
    using System;
    using System.Reactive.Linq;
    using System.Threading;
    using System.Windows.Input;
    using ReactiveUI;
    using ReactiveUI.Xaml;

    public class AddressViewModel : ReactiveObject
    {
        private string line1;

        public string Line1
        {
            get { return this.line1; }
            set { this.RaiseAndSetIfChanged(x => x.Line1, ref this.line1, value); }
        }
    }

    public class EmployeeViewModel : ReactiveObject
    {
        private readonly ReactiveCommand changeAddressCommand;
        private readonly ReactiveCommand changeAddressLineCommand;
        private readonly ObservableAsPropertyHelper<string> changes;
        private AddressViewModel address;
        private int changeCount;

        public EmployeeViewModel()
        {
            this.changeAddressCommand = new ReactiveCommand();
            this.changeAddressLineCommand = new ReactiveCommand();

            this.changeAddressCommand.Subscribe(x => this.Address = new AddressViewModel() { Line1 = "Line " + Interlocked.Increment(ref this.changeCount) });
            this.changeAddressLineCommand.Subscribe(x => this.Address.Line1 = "Line " + Interlocked.Increment(ref this.changeCount));

            this.Address = new AddressViewModel() { Line1 = "Default" };

            // Address-only changes
            this.changes = this.ObservableForProperty(x => x.Address)
                .Select(x => x.Value.Line1 + " CHANGE")
                .ToProperty(this, x => x.Changes);

            // Address.Line1-only changes
            //this.changes = this.Address.Changed
            //    .Where(x => x.PropertyName == "Line1")
            //    .Select(x => this.Address.Line1 + " CHANGE")        // x.Value is null here, for some reason, so I use this.Address.Line1 instead
            //    .ToProperty(this, x => x.Changes);
        }

        public ICommand ChangeAddressCommand
        {
            get { return this.changeAddressCommand; }
        }

        public ICommand ChangeAddressLineCommand
        {
            get { return this.changeAddressLineCommand; }
        }

        public AddressViewModel Address
        {
            get { return this.address; }
            set { this.RaiseAndSetIfChanged(x => x.Address, ref this.address, value); }
        }

        public string Changes
        {
            get { return this.changes.Value; }
        }
    }
}

MainWindow.cs :

using System.Windows;

namespace RxUITest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new EmployeeViewModel();
        }
    }
}

MainWindow.xaml :

<Window x:Class="RxUITest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock Text="{Binding Changes}"/>
        <Button Command="{Binding ChangeAddressCommand}">Change Address</Button>
        <Button Command="{Binding ChangeAddressLineCommand}">Change Address.Line1</Button>
    </StackPanel>
</Window>
4

2 に答える 2

15

いいえ、これを行う簡単な方法があります。

this.WhenAny(x => x.Address.Line, x => x.Value)
    .Subscribe(x => Console.WriteLine("Either Address or Address.Line changed!"));
于 2013-01-24T23:35:53.753 に答える
0

あなたのアドレスが行の単純なコレクションである可能性があると考える場合、つまり

class AddressViewModel 
 : ObservableCollection<LineViewModel>, INotifyPropertyChanged

次に、おそらくイベント ハンドラーをアタッチして、変更を伝達します。

this.CollectionChanged += this.NotifyPropertyChanged(...);

したがって、ReactiveObject に対しても同じようなことを行いますが、Rx を使用します。

public class EmployeeViewModel : ReactiveObject
{
private AddressViewModel address;
private IDisposable addressSubscription;

public AddressViewModel Address
{
    get { return this.address; }
    set 
    { 
        if (addressSubscription != null)
        {
            addressSubscription.Dispose();
            addressSubscription =null;
        }
        if (value != null)
        {
            addressSubscription = value.Subscribe(...);
        }
        this.RaiseAndSetIfChanged(x => x.Address, ref this.address, value); 
     }
}

}

于 2013-01-24T15:23:07.437 に答える