Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
1.71k stars 70 forks source link

ReactivePropertyの双方向バインド機能はありませんか? #227

Closed zerodev1200 closed 1 week ago

zerodev1200 commented 1 week ago

runceel/ReactivePropertyにあった、ToReactivePropertyAsSynchronized()と同じような双方向バインド機能を探しています。 下記はWPFで実行しています。

runceel/ReactiveProperty

public partial class MainWindow : Window
{
    public ReactiveProperty<int> A { get; } = new();
    public ReactiveProperty<int> B { get; } = new();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        var item = new Items();

        A = item.ToReactivePropertyAsSynchronized(x => x.Id);
        B = item.ToReactivePropertyAsSynchronized(x => x.Id);

        item.Id = 1;
        Debug.WriteLine($"item.Id:{item.Id}"); //1
        Debug.WriteLine($"a:{A.Value}"); //1
        Debug.WriteLine($"b:{B.Value}"); //1

        A.Value = 2;
        Debug.WriteLine($"item.Id:{item.Id}"); //2
        Debug.WriteLine($"a:{A.Value}"); //2
        Debug.WriteLine($"b:{B.Value}"); //2

        B.Value = 3;
        Debug.WriteLine($"item.Id:{item.Id}"); //3
        Debug.WriteLine($"a:{A.Value}"); //3
        Debug.WriteLine($"b:{B.Value}"); //3

        item.Id = 4;
        Debug.WriteLine($"item.Id:{item.Id}"); //4
        Debug.WriteLine($"a:{A.Value}"); //4
        Debug.WriteLine($"b:{B.Value}"); //4
    }

R3

public partial class MainWindow : Window
{
    public BindableReactiveProperty<int> A { get; } = new();
    public BindableReactiveProperty<int> B { get; } = new();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        var item = new Items();

        A = item.ObservePropertyChanged(x => x.Id).ToBindableReactiveProperty();
        B = item.ObservePropertyChanged(x => x.Id).ToBindableReactiveProperty();

        item.Id = 1;
        Debug.WriteLine($"item.Id:{item.Id}"); //1
        Debug.WriteLine($"a:{A.Value}"); //1
        Debug.WriteLine($"b:{B.Value}"); //1

        A.Value = 2;
        Debug.WriteLine($"item.Id:{item.Id}"); //1
        Debug.WriteLine($"a:{A.Value}"); //2
        Debug.WriteLine($"b:{B.Value}"); //1

        B.Value = 3;
        Debug.WriteLine($"item.Id:{item.Id}"); //1
        Debug.WriteLine($"a:{A.Value}"); //2
        Debug.WriteLine($"b:{B.Value}"); //3

        item.Id = 4;
        Debug.WriteLine($"item.Id:{item.Id}"); //4
        Debug.WriteLine($"a:{A.Value}"); //4
        Debug.WriteLine($"b:{B.Value}"); //4
    }
}

使い方が誤っているかもしれません。 お手数をおかけしますが、ご確認いただければ幸いです。

neuecc commented 1 week ago

R3のReactivePropertyはミニマムなコア要素だけを提供したいと考えているため フレームワーク的な高機能なものは入れない方針です。 なので、あるかどうかでいうと、ないですし、入れる予定もありませんになります。 どこまでをコア要素ととらえるかは難しいところですが、

ものに関しては中に入れることを検討し、そうでないものは原則としては入れない、といったところでしょうか。

他力本願なことを言えば ReactiveProperty.R3 みたいなのが爆誕してくれると一番嬉しいのですけどね(?)@runceel