Saturday, October 14, 2023
HomeiOS Developmentc# - Xamarin.iOS AddObserver not firing for change occasion for udpate to...

c# – Xamarin.iOS AddObserver not firing for change occasion for udpate to UIView.Window


I am making an attempt to implement a lifecycle impact in Xamarin.Types, however am having hassle for the iOS model. For some cause, I can not seem to observe the window altering notification occasion. Under is my code:

public class CustomLifeCycleEffectRouter : PlatformEffect
{
    non-public const NSKeyValueObservingOptions ObservingOptions = NSKeyValueObservingOptions.Preliminary | NSKeyValueObservingOptions.New;

    UIView? _nativeView;
    CustomLifeCycleEffect? _lifeCycleEffect;
    IDisposable _isLoadedObserverDisposable;

    protected override void OnAttached()
    {
        _lifeCycleEffect = Factor.Results.OfType<CustomLifeCycleEffect>().FirstOrDefault() ?? throw new ArgumentNullException($"The impact {nameof(CustomLifeCycleEffect)} cannot be null.");

        _nativeView = Management ?? Container;

        _isLoadedObserverDisposable = _nativeView?.AddObserver("window", ObservingOptions, isWindowAttachedObserver);
    }

    protected override void OnDetached()
    {
        _lifeCycleEffect?.RaiseUnloadedEvent(Factor);
        _isLoadedObserverDisposable.Dispose();
    }
    
    non-public void isWindowAttachedObserver(NSObservedChange nsObservedChange)
    {
        if (_nativeView.Window != null)
            _lifeCycleEffect?.RaiseLoadedEvent(Factor);
        else
            _lifeCycleEffect?.RaiseUnloadedEvent(Factor);
    }
}

I’m properly conscious that the Xamarin.Neighborhood Toolkit has an analogous impact, but it surely fires the occasion to early; I would like it to fireplace once I can navigate up the hiearchy to the basis mum or dad. Can anyone see an issue?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments