何时何地调用RemoveObserver

时间:2016-04-25 14:50:52

标签: c# xamarin xamarin.ios nsnotificationcenter

我有一个UITextView子类,我在其中添加了一个NSNotificationCenter观察器。但是我又在哪里删除了观察者呢?

我的代码:

_textDidChangeNotification = UITextView.Notifications.ObserveTextDidChange(TextDidChange);

在Objective C中,我会在dealloc方法中执行此操作,但我不确定在C#中的相同位置

据我了解文档,我应该致电

_textDidChangeNotification.Dispose()

我曾尝试过

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (disposing)
        {
            _textDidChangeNotification.Dispose();
        }
    }

但永远不会被召唤。

完整的课程,按要求:

public class PlaceholderTextView : UITextView
{
    public string Placeholder 
    { 
        get { return PlaceholderLabel.Text; }
        set
        { 
            PlaceholderLabel.Text = value; 
            PlaceholderLabel.SizeToFit();
        }
    }

    protected UILabel PlaceholderLabel { get; set; }

    protected NSObject _textDidChangeNotification;

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            AdjustPlaceholderHidden();
        }
    }

    public PlaceholderTextView() 
    {
        SetupLayout();

        _textDidChangeNotification
        = UITextView.Notifications.ObserveTextDidChange(TextDidChange);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        _textDidChangeNotification.Dispose();
    }

    protected void SetupLayout()
    {
        PlaceholderLabel = new UILabel(new CGRect(0, 9, 0, 0));
        PlaceholderLabel.TextColor = UIColor.FromWhiteAlpha(0.702f, 1f);

        AddSubview(PlaceholderLabel);
    }

    protected void AdjustPlaceholderHidden()
    {
        if (Text.Length > 0)
        {
            PlaceholderLabel.Hidden = true;
        }
        else
        {
            PlaceholderLabel.Hidden = false;
        }
    }

    protected void TextDidChange(object sender, Foundation.NSNotificationEventArgs args)
    {
        AdjustPlaceholderHidden();
    }       
}

1 个答案:

答案 0 :(得分:0)

我会在ViewWillDisappear这样做:

public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear (animated);

        SubscribeMessages ();
    }

    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
        UnSubscribeMessages ();
    }

    public void SubscribeMessages ()
    {
        _hideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
        _showObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
    }

    public void UnSubscribeMessages ()
    {
        if (_hideObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver (_hideObserver);
        if (_showObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver(_showObserver);
    }
像Xamarin示例代码here

中的

ViewDidDisappear

<强>更新 我明白你的意思了,我怀疑某些事情阻止了自定义视图被垃圾收集。您是否看过blog post它可能会有所帮助。

同样来自此sample code,您似乎正在正确调用dispose,但它们会在ViewDidUnload here上清空自定义视图: