简体   繁体   中英

Transition to the existing instance of ContentPage in Xamarin.Forms

I have a Xamarin.Forms application with MyWebViewPage: ContentPage , which set in the App.xaml.cs as MainPage. Every time when I use await Navigation.PushAsync(new MyWebViewPage()) for transition on the MainPage from other pages in the application, a new instance of type MyWebViewPage has created in memory. Can I realize transition on the existing instance of page MyWebViewPage without creating new instance of this type? I use media player on this page which linked with SignalR and this player plays sounds as many times as instances of this type was created. So I need to have only one instance of this type. Thank you for any help.

Have you tried saving the instance and navigating to the same instance? So instead of doing:

await Navigation.PushAsync(new MyWebViewPage());

You do:

await Navigation.PushAsync(_webViewPage ??= new MyWebViewPage());

Or if you don't like the ternary:

if (_webViewPage == null)
    _webViewPage = new MyWebViewPage();

await Navigation.PushAsync(_webViewPage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM