VS2022下Xamarin Forms Android NFC标签读取异常求助
解决Xamarin Forms Android NFC标签读取后Entry不更新的问题
1. 补全AndroidManifest.xml配置
当前权限配置缺少NFC事件的Intent过滤规则,系统无法将NFC标签发现事件传递给应用。需要在<activity>标签内添加以下内容:
<intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <action android:name="android.nfc.action.TECH_DISCOVERED" /> <action android:name="android.nfc.action.TAG_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
同时在Resources/xml目录下创建nfc_tech_filter.xml文件(若xml目录不存在需手动创建),内容为:
<?xml version="1.0" encoding="utf-8"?> <resources> <tech-list> <tech>android.nfc.tech.IsoDep</tech> <tech>android.nfc.tech.NfcA</tech> <tech>android.nfc.tech.NfcB</tech> <tech>android.nfc.tech.NfcF</tech> <tech>android.nfc.tech.NfcV</tech> <tech>android.nfc.tech.Ndef</tech> <tech>android.nfc.tech.NdefFormatable</tech> <tech>android.nfc.tech.MifareClassic</tech> <tech>android.nfc.tech.MifareUltralight</tech> </tech-list> </resources>
2. 完善MainActivity代码
Android系统通过Intent传递NFC事件,需重写OnNewIntent方法将事件转发给Plugin.NFC:
using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using Plugin.NFC; using Android.Content; namespace Scan_NFC.Droid { [Activity(Label = "Scan_NFC", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); CrossNFC.Init(this); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); CrossNFC.Current.OnNewIntent(intent); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } } }
3. 修复MainPage代码中的冗余与日志排查
- 移除构造函数中手动查找
meuEntry的代码:meuEntry = (Entry)FindByName("meuEntry");,Xamarin会自动为x:Name生成对应的字段,手动赋值可能导致引用异常。 - 添加日志输出,确认NFC事件是否触发:
using Plugin.NFC; using System; using Xamarin.Forms; namespace Scan_NFC { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); CrossNFC.Current.OnMessageReceived += NfcMessageReceived; CrossNFC.Current.OnTagListeningStatusChanged += (isListening) => System.Diagnostics.Debug.WriteLine(isListening ? "NFC监听已启动" : "NFC监听已停止"); } private void NfcMessageReceived(ITagInfo tagInfo) { System.Diagnostics.Debug.WriteLine("已触发NFC消息接收事件"); if (tagInfo == null) { System.Diagnostics.Debug.WriteLine("TagInfo为空"); return; } var idBytes = tagInfo.Identifier; var message = BitConverter.ToString(idBytes); System.Diagnostics.Debug.WriteLine($"读取到NFC标签ID:{message}"); Device.BeginInvokeOnMainThread(() => { meuEntry.Text = $"Tag NFC Lida - ID: {message}"; DisplayAlert("Tag NFC Lida", $"ID: {message}", "OK"); }); } protected override void OnAppearing() { base.OnAppearing(); System.Diagnostics.Debug.WriteLine("MainPage OnAppearing"); CrossNFC.Current.StartListening(new System.Collections.Generic.List<string>(), false); } protected override void OnDisappearing() { base.OnDisappearing(); System.Diagnostics.Debug.WriteLine("MainPage OnDisappearing"); CrossNFC.Current.StopListening(); } } }
4. 验证Plugin.NFC版本
确保项目中引用的Plugin.NFC包是最新稳定版,兼容当前Xamarin Forms版本,避免版本不匹配导致的事件不触发问题。
完成以上修改后,重新部署应用,测试NFC标签读取:
- 查看输出窗口的日志,确认是否打印"已触发NFC消息接收事件"和标签ID
- 若日志正常输出但Entry仍不更新,检查
meuEntry的绑定是否正确,或尝试用Label替代Entry测试UI更新是否正常
内容的提问来源于stack exchange,提问作者Eduardo Elias
相关产品推荐
相关产品推荐

