詳細訊號

你可以使用 [Signal (detailed = true)] 屬性編寫詳細訊號。

public class Emitter : Object {
    [Signal (detailed = true)]
    public signal void detailed_signal ();

    public void emit_with_detail (string detail) {
        this.detailed_signal[detail] ();
    }
}

void main () {
    var emitter = new Emitter ();

    // Connect only when the detail is "foo".
    emitter.detailed_signal["foo"].connect (() => {
       print ("Received the signal with 'foo'.\n");
    });

    // Connect to the signal, whatever is the detail.
    emitter.detailed_signal.connect (() => {
        print ("Received the signal.\n");
    });

    emitter.emit_with_detail ("foo"); // Both handlers will be triggered.
    emitter.emit_with_detail ("bar"); // Only the general handler will be triggered.
}

此功能通常與 notify 訊號一起使用,即任何基於 Object 的類具有,並且在屬性更改時傳送。這裡的詳細資訊是屬性的名稱,因此你可以選擇僅為其中一些訊號連線此訊號。

public class Person : Object {
    public string name { get; set; }
    public int age { get; set; }
}

void main () {
    var john = new Person () { name = "John", age = 42 });
    john.notify["age"].connect (() => {
        print ("Happy birthday!");
    });
    john.age++;
}