Giter Site home page Giter Site logo

Comments (13)

franckbour avatar franckbour commented on August 15, 2024 1

You probably have to subscribe on NFC events elsewhere than OnAppearing.
A quick fix for the sample will be like this:

bool _eventsAlreadySubscribed = false;
void SubscribeEvents()
{
    if (_eventsAlreadySubscribed)
        return;
    _eventsAlreadySubscribed = true;

    CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived;
    [...]
}

from plugin.nfc.

dm-CaT avatar dm-CaT commented on August 15, 2024 1

@franckbour, I took your test project and the issue is reproduced.
Here is video with your project: youtube

The problem is when you read a NFC card the page in non-modal navigation stack disappears and appears (OnDisapearing and OnAppearing are called).
OnAppearing method is used primarily to load some data. And loading occurs every time when a NFC is read.

from plugin.nfc.

saamerm avatar saamerm commented on August 15, 2024 1

@pazunino if you see @dm-CaT ‘s comment, you cannot reliably use the OnAppearing and OnDisappearing from Android. So instead try to use a view model and use the constructor and destructor of the view model instead

from plugin.nfc.

Senso4sLab avatar Senso4sLab commented on August 15, 2024 1

Is any progress with this issue? I think that this issue limited the usage of NFC in Xamarin.Forms. I tested with AppShell and the same problem occurred.

from plugin.nfc.

AlanGusLive avatar AlanGusLive commented on August 15, 2024 1

I use another workaround for my MAUI application. In the OnAppearing() and OnDisappearing() functions, I directly return when the application is not navigating between pages (called when NFC is used). In AppShell.xaml.cs, I set a static bool to true in the function OnNavigating(ShellNavigatingEventArgs args). After in All OnAppearing(), I test this bool to directly return or to set it to false and execute all lines in OnAppearing(). In All OnDisappearing() I test this bool to directly return or execute all lines. So with a card on phone NFC, the 2 functions OnAppearing() and OnDisappearing() are called but there is a return.

This is the MAUI sample code tested in Android:
Plugin.NFCWorkaround.zip

image

image

from plugin.nfc.

dm-CaT avatar dm-CaT commented on August 15, 2024

It doesn't matter where the subscription is done.

  1. Create blank Xamarin Forms Project.
  2. Init CrossNFC in Android project, set required permissions.
  3. Change in the shared project app.xaml.cs Main page to the navigation page
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
        }
  1. Add button to the MainPage. Bind to ListenCommand
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Catyari.Xam.Nfc.Mobile.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

        <Button Text="Listen" Command="{Binding ListenCommand}"/>
    </StackLayout>

</ContentPage>
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            this.ListenCommand = new Command(() => 
            {
                CrossNFC.Current.StartListening();
                CrossNFC.Current.OnMessageReceived += this.OnNfcMessage;
            });
            this.BindingContext = this;

            InitializeComponent();
        }

        private void OnNfcMessage(ITagInfo tagInfo)
        {
        }

        public ICommand ListenCommand { get; }


        protected override void OnAppearing()
        {
            base.OnAppearing(); // set breakpoint here
        }
    }
  1. Run app
  2. Set breakpoint in OnAppearing method.
  3. Click Listen button.
  4. Place card on phone NFC sensor.

Current result: app stops on the breakpoint in OnAppearing method

from plugin.nfc.

dm-CaT avatar dm-CaT commented on August 15, 2024

I did some googling about that. The reason is that the MainActivity is paused before new intent processing.

from plugin.nfc.

dm-CaT avatar dm-CaT commented on August 15, 2024

The only one way I found to avoid it is to push page in modal navigation stack. This case OnAppearing is not called.

from plugin.nfc.

timothy-gibson avatar timothy-gibson commented on August 15, 2024

I'm also encountering this issue. OnDisappearing() is called before the event is handled and OnAppearing() is called after the event is handled. @dm-CaT did you find any other way around it?

from plugin.nfc.

franckbour avatar franckbour commented on August 15, 2024

@dm-CaT, @timothy-gibson

  1. Create blank Xamarin Forms Project.
  2. Init CrossNFC in Android project, set required permissions.
  3. Change in the shared project app.xaml.cs Main page to the navigation page
  4. Add button to the MainPage. Bind to ListenCommand
  5. Run app
  6. Set breakpoint in OnAppearing method.
  7. Click Listen button.
  8. Place card on phone NFC sensor.

Current result: app stops on the breakpoint in OnAppearing method

I followed these reproduction steps but I didn't reproduce the final result, app didn't stop.
This is my test project: TestIssue27.zip

But I maybe misunderstood the issue... If so, please provide a reproduction sample and some clear explanations.

from plugin.nfc.

dm-CaT avatar dm-CaT commented on August 15, 2024

@timothy-gibson,

@dm-CaT did you find any other way around it?

No, I didn't. To way around I took a look into Xamarin Forms source code. I was looking for a place where OnDisappearing/OnAppearing are called as the result of the MainActivity deactivation and activation back. But I didn't find such code because XF source code is too entangled.
I think if we'll find this piece of code we'll be able avoid somehow OnAppearing/OnDesappearing calls.

from plugin.nfc.

saamerm avatar saamerm commented on August 15, 2024

@dm-CaT and @timothy-gibson I made it work in my ViewModel perfectly in a stable app. All i did was ensure that I assigned the event handlers only once, and then in the destructor of the viewmodel, and every navigation away from the page, i unassigned the event handlers

from plugin.nfc.

pazunino avatar pazunino commented on August 15, 2024

Hi @saamerm I am stuck with this issue. Can you explain how do you workaround it? I didn't understand your last message. Thanks.

from plugin.nfc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.