2

私はちょうどRxHOLNETを読んでいました。検索時(例ではWindowsフォームを使用):

var moves = Observable.FromEvent<MouseEventArgs>(frm, "MouseMove");

一部のWPFMVVMセットアップでViewModelへの移動への参照をインスタンス化して渡すにはどうすればよいですか?私の理解では、ViewModel内でこのデータストリームをフィルタリングしようとすることは理にかなっています。

または、TextBoxへのキーボード入力に対して同様のことを行う方法は?このシナリオでは、たとえば、テキストマスキング動作をXAMLのコントロールにアタッチせず、代わりに、VMのオブザーバーにキーボード入力のフィルター処理と検証を許可します。

私は完全に軌道から外れていますか?

4

3 に答える 3

5

これは、MVVM方式でWebサービスディクショナリを実装する方法の例です。これには3つの部分があります。

  1. ObservablePropertyBackingクラス、IObservableも実装する(Tの)プロパティのバッキング
  2. MyViewModelクラス。これには、バッキングストレージとしてObservablePropertyBackingを使用するプロパティCurrentTextが含まれています。また、このプロパティの値を監視し、それを使用してディクショナリWebサービスを呼び出します。
  3. TextBoxを含むMainView.xaml。そのTextプロパティは、ビューモデルのCurrentTextプロパティに双方向でバインドされています。

MyViewModel.cs:

class MyViewModel: INotifyPropertyChanged
{
    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    #endregion

    public MyViewModel()
    {
        SetupProperties();
    }

    #region CurrentText

    /*  We use a special class for backing of the CurrentText property. This object
     *  holds the value of the property and also dispatches each change in an observable 
     *  sequence, i.e. it implements IObservable<T>.
     */
    private ObservablePropertyBacking<string> _textInput;
    public string CurrentText
    {
        get { return _textInput.Value; }
        set
        {
            if (value == _textInput.Value) { return; }
            _textInput.Value = value;
            RaisePropertyChanged("CurrentText");
        }
    }

    #endregion

    /*  Create property backing storage and subscribe UpdateDictionary to the observable 
        *  sequence. Since UpdateDictionary calls a web service, we throttle the sequence.
        */
    private void SetupProperties()
    {
        _textInput = new ObservablePropertyBacking<string>();
        _textInput.Throttle(TimeSpan.FromSeconds(1)).Subscribe(UpdateDictionary);
    }

    private void UpdateDictionary(string text)
    {
        Debug.WriteLine(text);
    }
}

ObservablePropertyBacking.cs:

public class ObservablePropertyBacking<T> : IObservable<T>
{
    private Subject<T> _innerObservable = new Subject<T>();

    private T _value;
    public T Value
    {
        get { return _value; }
        set
        {
            _value = value;
            _innerObservable.OnNext(value);
        }
    }

    public IDisposable Subscribe(IObserver<T> observer)
    {
        return _innerObservable
            .DistinctUntilChanged()
            .AsObservable()
            .Subscribe(observer);
    }
}

MainPage.xaml:

  <Window 
    x:Class="RxMvvm_3435956.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">
    <Grid>
      <TextBox
        Text="{Binding CurrentText, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
  </Window>
于 2010-08-10T20:15:43.887 に答える
1

これは役立つかもしれません:リアクティブエクステンション(Rx)+ MVVM =?

于 2010-08-08T21:36:58.903 に答える
1

キーボードサンプルを実行する最も簡単な方法は、テキストをViewModelのプロパティに双方向でバインドすることです。テキストセッターはSubject、コードの残りの部分がの基礎として使用するプライベートに書き込むことができますIObservable<string>。そこから、HOLサンプルを完成させることができます。

マウスの動きは、一般に、ViewModelに入れるには「ビュー」すぎると見なされますが、それから外れたロジックが十分に複雑な場合は、実行させるICommandか、ロジックを動作に入れることができます。の場合、 ViewModelで取得できるプロパティをICommandコマンドにWhenExecuted IObservable含めることができます。`

于 2010-08-09T06:39:03.827 に答える