Comments (13)
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.
@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.
@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.
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.
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
from plugin.nfc.
It doesn't matter where the subscription is done.
- Create blank Xamarin Forms Project.
- Init CrossNFC in Android project, set required permissions.
- Change in the shared project app.xaml.cs Main page to the navigation page
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
- 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
}
}
- Run app
- Set breakpoint in OnAppearing method.
- Click Listen button.
- Place card on phone NFC sensor.
Current result: app stops on the breakpoint in OnAppearing method
from plugin.nfc.
I did some googling about that. The reason is that the MainActivity is paused before new intent processing.
from plugin.nfc.
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.
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.
- Create blank Xamarin Forms Project.
- Init CrossNFC in Android project, set required permissions.
- Change in the shared project app.xaml.cs Main page to the navigation page
- Add button to the MainPage. Bind to ListenCommand
- Run app
- Set breakpoint in OnAppearing method.
- Click Listen button.
- 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 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.
@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.
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)
- Support for Waveshare ePaper Displays
- Native MiFare Commands
- How to solve Tag is not NDEF-compliant? HOT 2
- Stack Error during NFC scan HOT 1
- iOS 4099 error when BeginListening
- Scanning pasports and ID cards HOT 1
- Unable to build iOS for simulator using Xcode 14.3 and Xamarin.iOS 16.4.0.6 HOT 1
- Issue with NFC Tag detection in iOS - MAUI
- While using Navigation in MAUI on Android NFC tag is not scanned HOT 4
- Questions showing custom UI messages
- Write Tag Android got exception: Tag IO Error HOT 2
- IOS / Library not working after published to AppStore Connect / Testflight HOT 2
- working on formatting HOT 3
- Android NFC Read times slow
- [NOT A ISSUE] Question: AFI
- Using old ISO 15693 non NDEF cards HOT 1
- Though the device supports NFC, NFC is not available HOT 1
- CrossNFC.Current.StartListening() on Content Page load not working - .NET Maui Android App.
- Write VCARD data HOT 1
- Apple App Store submission fails validation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plugin.nfc.