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

[ad_1]

I am attempting to implement a lifecycle impact in Xamarin.Varieties, 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
{
    personal const NSKeyValueObservingOptions ObservingOptions = NSKeyValueObservingOptions.Preliminary | NSKeyValueObservingOptions.New;

    UIView? _nativeView;
    CustomLifeCycleEffect? _lifeCycleEffect;
    IDisposable _isLoadedObserverDisposable;

    protected override void OnAttached()
    {
        _lifeCycleEffect = Ingredient.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(Ingredient);
        _isLoadedObserverDisposable.Dispose();
    }
    
    personal void isWindowAttachedObserver(NSObservedChange nsObservedChange)
    {
        if (_nativeView.Window != null)
            _lifeCycleEffect?.RaiseLoadedEvent(Ingredient);
        else
            _lifeCycleEffect?.RaiseUnloadedEvent(Ingredient);
    }
}

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

[ad_2]

Leave a Reply