question

Dev1Qc avatar image
0 Votes"
Dev1Qc asked JarvanZhang-MSFT commented

How to know From Xamarin.ios which Page is active

Any Solution to know if content page is active or called inside a AppDelegate.cs ?
because iv installed a plugin NFC, and i want to active the plugin only when NFC.xaml page is active, not when starting the application.
in this code am trying to begin session only When page NFC.xaml is active or called
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// if(App.Current==NFC.xaml) any way to know the content page ?
Session = new NFCNdefReaderSession(this, null, true);
Session?.BeginSession();
}
}
}

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i want to active the plugin only when NFC.xaml page is active, not when starting the application

Hi, Dev1Qc. In Xamarin.Forms, Page class provides the Appearing event which is executed when the page appears to the screen. Try detecting the Appearing event of the 'NFC.xaml' page to start up the plugin.

You could create a public static property in the App.xaml.cs to check if the plugin has been started up. Here is the related code, you could refer to it.

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        if (!App.isActive)
        {
            //start up the plugin
            App.isActive = true;
        }
    }
}

public partial class App : Application
{
    public static bool isActive;
    public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage());
    }
}
0 Votes 0 ·

Thanks for ur answer , the nfc that i installed implement the interface (INFCNdefReaderSessionDelegate) so only AppDelegate can implement from this interface.. i cant call the plugin inside MainPage !!

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello @Dev1Qc ,​

Welcome to our Microsoft Q&A platform!

the nfc that i installed implement the interface ... so only AppDelegate can implement from this interface.. i cant call the plugin inside MainPage !!

Try using MessagingCenter between the 'NFC.xaml' page and the AppDelegate class. Send a message in the OnAppearing to the AppDelegate class to perform the work.

Here is the related doc, you could refer to:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for ur answer , it solved my problem

0 Votes 0 ·

My pleasure. Happy coding!

1 Vote 1 ·