How to use ViewDidAppear() in Xamarin.Forms?

MattHarringtonMattHarrington USMember, University

I'm porting a Xamarin.iOS app to Xamarin.Forms. In the iOS version, I have code in ViewDidAppear() and ViewDidDisappear() which handles remote control events for audio. For example:

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
            this.BecomeFirstResponder();
        }

In Xamarin.Forms, where should I put this code? Should I create a custom renderer as described in a blog post by Mike Bluestein I found?

Matt

Best Answer

Answers

  • MattHarringtonMattHarrington USMember, University

    Thanks for the response. I ended up creating a custom renderer, which was quite simple and did the trick. OnAppearing() and OnDisappearing() might also work for me, but I haven't written the Android portion of the audio code yet and I don't know what Android requires when it comes to handling remote control events. If it's a similar situation to iOS, I'll create an interface and use the DependencyService. Thanks for bringing it to my attention.

    It's true that audio code is not portable, and this erodes some of the benefits of using Forms. However, I'm still happy with choosing Forms over Xamarin.iOS.

  • BenPiBenPi USUniversity ✭✭

    If that audio part is the only big difference between your android and ios project, i think you would be good to go with IoC instaed of customizing the ViewDidAppear on iOS or some of the start-events on Android via custom renderer.
    The big advantage of abstraction is that you can share the logic controlling your stuff in the cross-plattform base and don't have to rewrite it.

Sign In or Register to comment.