簡體   English   中英

在哪里取消訂閱 Xamarin.Forms 條目中的事件?

[英]Where to unsubscribe events in a Xamarin.Forms Entry?

我有一個自定義條目繼承自 Xamarin.Forms 條目以添加 CompletedCommand 屬性。 為此,我在構造函數中訂閱了 Xamarin.Forms 條目的 Completed Event 並調用可綁定的屬性命令。

我的問題是,有必要退訂嗎? 如果是這樣,哪里是這樣做的好地方?

public class Entry : Xamarin.Forms.Entry
{
    public static readonly BindableProperty CompletedCommandProperty = BindableProperty.Create(
                                                     propertyName: nameof(CompletedCommand),
                                                     returnType: typeof(ICommand),
                                                     declaringType: typeof(Entry),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay);

    /// <summary>
    /// The Command to execute when this entry is completed (Enter pressed).
    /// </summary>
    public ICommand CompletedCommand
    {
        get => (ICommand)GetValue(CompletedCommandProperty);
        set => SetValue(CompletedCommandProperty, value);
    }

    public Entry() : base()
    {
        Completed += Entry_Completed;
    }

    private void Entry_Completed(object sender, EventArgs e)
    {
        CompletedCommand?.Execute(CompletedCommandParameter);
    }
}

谷歌搜索“xamarin 取消訂閱事件”給出了很多有趣的結果。 我覺得,如果可能的話,您想使用 EventToCommandBehavior(我相信是 xamarin 工具包的一部分?)來避免使用事件。 但如果你不能這樣做,取消訂閱可能是不必要的,但仍然是最佳做法。

來源: https://github.com/xamarin/xamarin-forms-samples/issues/188

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM