每次方法调用时可观察到的新数据

发布于 2024-12-05 18:51:59 字数 557 浏览 1 评论 0原文

这对于那些熟悉技术的人来说可能非常简单,但是每当调用我的方法时,我如何直接向给定的可观察对象提供新数据?

    IObservable<int> _myObservable; 

    void ThingsCallMe(int someImportantNumber)
    {
        // Current pseudo-code seeking to be replaced with something that would compile?
        _myObservable.Add(someImportantNumber);
    }

    void ISpyOnThings()
    {
        _myObservable.Subscribe(
            i =>
            Console.WriteLine("stole your number " + i.ToString()));
    }

我也不知道我应该使用哪种可观察的,仅在特殊情况下才能到达 OnCompleted() 的观察?

this may be really simple to those in the know-how, but how can i directly provide new data to a given observable, whenever a method of mine is invoked?

    IObservable<int> _myObservable; 

    void ThingsCallMe(int someImportantNumber)
    {
        // Current pseudo-code seeking to be replaced with something that would compile?
        _myObservable.Add(someImportantNumber);
    }

    void ISpyOnThings()
    {
        _myObservable.Subscribe(
            i =>
            Console.WriteLine("stole your number " + i.ToString()));
    }

i also dont know what kind of observable i should employ, one that gets to OnCompleted() under special circumstances only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-12-12 18:51:59

这是基本答案。我稍微修改了你的代码。

Subject<int> _myObservable = new Subject<int>(); 

void ThingsCallMe(int someImportantNumber)
{
    // Current pseudo-code seeking to be replaced with something that would compile?
    _myObservable.OnNext(someImportantNumber);
}

void ISpyOnThings()
{
    _myObservable.Subscribe(
        i =>
        Console.WriteLine("stole your number " + i.ToString()));
}

这应该有效。主题只是一个 IObservable 和一个 IObserver。您可以调用 OnCompleted、OnError 等。

Here's the basic answer. I modified your code slightly.

Subject<int> _myObservable = new Subject<int>(); 

void ThingsCallMe(int someImportantNumber)
{
    // Current pseudo-code seeking to be replaced with something that would compile?
    _myObservable.OnNext(someImportantNumber);
}

void ISpyOnThings()
{
    _myObservable.Subscribe(
        i =>
        Console.WriteLine("stole your number " + i.ToString()));
}

This should work. A subject is simply an IObservable and an IObserver. You can call OnCompleted, OnError, etc.

雨巷深深 2024-12-12 18:51:59

我测试并得到了这个工作:

static ObservableCollection<int> myCol = new ObservableCollection<int>();

static void Main(string[] args)
{
  ((INotifyCollectionChanged)myCol).CollectionChanged += new NotifyCollectionChangedEventHandler(Program_CollectionChanged);

  ThingsCallMe(4);
  ThingsCallMe(14);
}

static void ThingsCallMe(int someImportantNumber)
{
    myCol.Add(someImportantNumber);
}

static void Program_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    Debug.WriteLine(e.NewItems.ToString());
}

I tested and got this working:

static ObservableCollection<int> myCol = new ObservableCollection<int>();

static void Main(string[] args)
{
  ((INotifyCollectionChanged)myCol).CollectionChanged += new NotifyCollectionChangedEventHandler(Program_CollectionChanged);

  ThingsCallMe(4);
  ThingsCallMe(14);
}

static void ThingsCallMe(int someImportantNumber)
{
    myCol.Add(someImportantNumber);
}

static void Program_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    Debug.WriteLine(e.NewItems.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文