Dart 中的事件和 EventHandle

问题描述 投票:0回答:2

我来自 C#,我(或多或少)了解事件的逻辑及其工作原理。现在,我必须将一个事件范例(带有数据传递)从 C# 导出到 Dart,但我不明白它在 Dart 上是如何工作的。有谁能耐心给我解释一下吗?谢谢

编辑:这些是我必须翻译的代码片段

Class Engine.cs

public class Engine {
    [...]
    public event EventHandler<EngineComputationEventArgs> ComputationCompleted;

     protected virtual void OnComputationCompleted(Result result) {
         var evt = ComputationCompleted;
         if (evt != null) {
             evt(this, new EngineComputationEventArgs(result));
         }
     }
}

Class Example.cs

[...]

engine.ComputationCompleted += (sender, e) => {
    Console.WriteLine("PPE {0}", e.Result.Ppe);
};

[...]

EngineComputationEventArgs.cs

public class EngineComputationEventArgs : EventArgs {

    public EngineComputationEventArgs(Result result) {
        Result = result;
    }

    public Result Result { get; private set; }

}
dart events event-handling
2个回答
1
投票

Dart 没有像 C# 那样内置“事件”类型。您通常使用

Stream
作为事件发射器,并使用相应的
StreamController
向其添加事件。

示例:

class Engine {
  final StreamController<EngineComputationEventArgs> _computationCompleted =
     StreamController.broadcast(/*sync: true*/);

  Stream<EngineComputationEventArgs> get computationCompleted => 
      _computationCompleted.stream;

  void onComputationCompleted(Result result) {
    if (_computationCompleted.hasListener) {
      _computationCompleted.add(EngineComputationEventArgs(result));
    }
  }
}

然后,您可以通过监听流来添加监听器:

engine.computationCompleted.forEach((e) =>
    print("PPE ${e.result.ppe}"));

var subscription = engine.computationCompleted.listen((e) =>
    print("PPE ${e.result.ppe}"));
// Can call `subscription.cancel()` later to stop listening.

你的“args”类可能是(因为我对

EventArgs
一无所知)

class EngineComputationEventArgs extends EventArgs {
  final Result result;
 
  EngineComputationEventArgs(this.result);
}

0
投票

你不能应用这个概念吗? 在我的理解中,c# 事件只是调用列表中的常用函数。

// import 'package:timer0/timer0.dart' as timer0;
import 'dart:async';

// A class similar to event syntax of c#.
class Event {
  List<void Function(Object senderInfo)> eventHandlers = [];

  void add(Function(Object) handler) {
    eventHandlers.add(handler);
  }

  void remove(Function(Object) handler) {
    eventHandlers.remove(handler);
  }

  void invoke(Object senderInfo) {
    for (var f in eventHandlers) {
      f(senderInfo);
    }
  }
}

// The event sender class.
class EventTx {
  late Timer tm;
  Event ev = Event();

  void add(Function(Object) handler) {
    ev.add(handler);
  }

  void remove(Function(Object) handler) {
    ev.remove(handler);
  }

  void start(Duration period) {
    tm = Timer.periodic(period, (Timer tm) {
      ev.invoke(this);
    });
  }
}

// An event listener class.
class EventRx0 {
  void eventHandler(Object eventInfo) {
    EventTx evtx = eventInfo as EventTx;
    int tick = evtx.tm.tick;
    print('EventRx0: $tick');
  }
}

// Another event listener class.
class EventRx1 {
  void eventHandler(Object eventInfo) {
    EventTx evtx = eventInfo as EventTx;
    int tick = evtx.tm.tick;
    print('EventRx1: $tick');
  }
}

void main() {
  EventTx evtx = EventTx();
  EventRx0 evrx0 = EventRx0();
  EventRx1 evrx1 = EventRx1();
  evtx.add(evrx0.eventHandler);
  evtx.add(evrx1.eventHandler);
  evtx.start(Duration(seconds: 1));
  // After 5 seconds, remove the event handler of evrx0
  // and then restart the interval timer.
  Timer(Duration(seconds: 5), () {
    evtx.tm.cancel();
    evtx.remove(evrx0.eventHandler);
    evtx.start(Duration(seconds: 1));
    // 5 seconds after restarting, the timer is stopped again.
    Timer(Duration(seconds: 5), () {
      evtx.tm.cancel();
    });
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.