如何使用 iOS / Xamarin 从 viewmodel 扫描 NFC 标签
How can I scan NFC-Tags with iOS / Xamarin from viewmodel
我有一个适用于 android / iOS 的跨平台 Xamarin.Forms .net 标准应用程序,我想添加 nfc 扫描功能。
对于我的第一次测试,我将所有内容都放入了我的 AppDelegate class。
此代码有效:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, INFCNdefReaderSessionDelegate
{
public NFCNdefReaderSession Session { get; set; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
if (Session == null)
{
Session = new NFCNdefReaderSession(this, null, true);
}
Session = new NFCNdefReaderSession(this, null, true);
Session?.BeginSession();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public void DidInvalidate(NFCNdefReaderSession session, NSError error)
{
Console.WriteLine("ServiceToolStandard DidInvalidate: " + error.ToString());
}
public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
{
var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
var message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("ServiceToolStandard DidDetect: " + message);
}
}
我的问题:
之后,我将所有内容都放入了一个新的 NfcScanner class 中,并希望从我的 ScanAsync 中调用函数视图模型。如果我 运行 这段代码,一切看起来都很好。当我按下 phone 上的按钮时扫描开始,它显示蓝色勾号,但随后 它永远不会进入已实现的方法之一 DidDetect, 无效化。你知道可能是什么原因吗?
public class NfcScanner : INfcScanner, INFCNdefReaderSessionDelegate
{
public string ErrorText { get; private set; }
private NFCNdefReaderSession _session;
private TaskCompletionSource<string> _tcs;
public Task<string> ScanAsync()
{
if (!NFCNdefReaderSession.ReadingAvailable)
{
throw new InvalidOperationException("Reading NDEF is not available");
}
_tcs = new TaskCompletionSource<string>();
_session = new NFCNdefReaderSession(this, null, true);
_session.BeginSession();
return _tcs.Task;
}
public void DidInvalidate(NFCNdefReaderSession session, NSError error)
{
Console.WriteLine("ServiceToolStandard DidInvalidate: " + error.ToString());
_tcs.TrySetException(new Exception(error?.LocalizedFailureReason));
}
public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
{
Console.WriteLine("ServiceToolStandard DidDetect msgs " + messages.Length);
var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
var message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("ServiceToolStandard DidDetect msg " + message);
_tcs.SetResult(message);
}
public IntPtr Handle { get; }
public void Dispose()
{
Console.WriteLine("ServiceToolStandard Dispose");
}
}
我也尝试制作一个 void Scan() 方法并直接从 AppDelegate 调用它。结果是一样的。扫描 window 打开并显示蓝色勾号,但它从未到达方法 diddetect、didinvalidate。
编辑 07.02.2018 16:56:
我在示例代码中看到,它们的 class 派生自 UITableViewController。
这是我的代码和示例代码之间唯一真正的区别。在我从 ViewController 中导出 NfcScanner class 后,它起作用了。但我不确定为什么以及它是否是正确的方法?
public class NfcScanner : UIViewController, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
非常感谢您的帮助!
我试图找到一个答案,为什么它在我从 UIViewController 派生后起作用。我想我在本教程中找到了它:Core NFC Tutorial
Creating a session, we can specify a delegate in our
NFCNDEFReaderSession class. I would like to use the NFCHelper class as
the delegate, so we must first adhere to the delegate protocol,
NFCNDEFReaderSessionDelegate. This delegate is based on an Objective-C
object, so we must first also adhere to NSObject.
NFCNDEFReaderSessionDelegate has two delegate methods we must
implement:
UIViewController 继承自 NSObject。现在,在我将代码更改为:
后它也可以使用
public class NfcScanner : NSObject, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
这个问题有点老了,而且仍然出现在搜索结果的顶部,所以我只是想为任何正在寻找它的人分享一个新的答案。为了访问任何 NFC 功能,您可以使用 Plugin.NFC nuget 包。它也支持使用 iOS 写入 NFC 标签,并且还在 GitHub 存储库中提供了一个很好的示例。您应该能够直接从您的视图中使用它
如果您确实需要建模,但我建议您根据 MVVM 原则从您的视图中调用它。
我最近使用它并在 How to get started with NFC and Xamarin in 5 minutes 上记录了我的旅程。
我有一个适用于 android / iOS 的跨平台 Xamarin.Forms .net 标准应用程序,我想添加 nfc 扫描功能。
对于我的第一次测试,我将所有内容都放入了我的 AppDelegate class。 此代码有效:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, INFCNdefReaderSessionDelegate
{
public NFCNdefReaderSession Session { get; set; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
if (Session == null)
{
Session = new NFCNdefReaderSession(this, null, true);
}
Session = new NFCNdefReaderSession(this, null, true);
Session?.BeginSession();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public void DidInvalidate(NFCNdefReaderSession session, NSError error)
{
Console.WriteLine("ServiceToolStandard DidInvalidate: " + error.ToString());
}
public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
{
var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
var message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("ServiceToolStandard DidDetect: " + message);
}
}
我的问题:
之后,我将所有内容都放入了一个新的 NfcScanner class 中,并希望从我的 ScanAsync 中调用函数视图模型。如果我 运行 这段代码,一切看起来都很好。当我按下 phone 上的按钮时扫描开始,它显示蓝色勾号,但随后 它永远不会进入已实现的方法之一 DidDetect, 无效化。你知道可能是什么原因吗?
public class NfcScanner : INfcScanner, INFCNdefReaderSessionDelegate
{
public string ErrorText { get; private set; }
private NFCNdefReaderSession _session;
private TaskCompletionSource<string> _tcs;
public Task<string> ScanAsync()
{
if (!NFCNdefReaderSession.ReadingAvailable)
{
throw new InvalidOperationException("Reading NDEF is not available");
}
_tcs = new TaskCompletionSource<string>();
_session = new NFCNdefReaderSession(this, null, true);
_session.BeginSession();
return _tcs.Task;
}
public void DidInvalidate(NFCNdefReaderSession session, NSError error)
{
Console.WriteLine("ServiceToolStandard DidInvalidate: " + error.ToString());
_tcs.TrySetException(new Exception(error?.LocalizedFailureReason));
}
public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
{
Console.WriteLine("ServiceToolStandard DidDetect msgs " + messages.Length);
var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
var message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("ServiceToolStandard DidDetect msg " + message);
_tcs.SetResult(message);
}
public IntPtr Handle { get; }
public void Dispose()
{
Console.WriteLine("ServiceToolStandard Dispose");
}
}
我也尝试制作一个 void Scan() 方法并直接从 AppDelegate 调用它。结果是一样的。扫描 window 打开并显示蓝色勾号,但它从未到达方法 diddetect、didinvalidate。
编辑 07.02.2018 16:56:
我在示例代码中看到,它们的 class 派生自 UITableViewController。 这是我的代码和示例代码之间唯一真正的区别。在我从 ViewController 中导出 NfcScanner class 后,它起作用了。但我不确定为什么以及它是否是正确的方法?
public class NfcScanner : UIViewController, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
非常感谢您的帮助!
我试图找到一个答案,为什么它在我从 UIViewController 派生后起作用。我想我在本教程中找到了它:Core NFC Tutorial
Creating a session, we can specify a delegate in our NFCNDEFReaderSession class. I would like to use the NFCHelper class as the delegate, so we must first adhere to the delegate protocol, NFCNDEFReaderSessionDelegate. This delegate is based on an Objective-C object, so we must first also adhere to NSObject. NFCNDEFReaderSessionDelegate has two delegate methods we must implement:
UIViewController 继承自 NSObject。现在,在我将代码更改为:
后它也可以使用public class NfcScanner : NSObject, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
这个问题有点老了,而且仍然出现在搜索结果的顶部,所以我只是想为任何正在寻找它的人分享一个新的答案。为了访问任何 NFC 功能,您可以使用 Plugin.NFC nuget 包。它也支持使用 iOS 写入 NFC 标签,并且还在 GitHub 存储库中提供了一个很好的示例。您应该能够直接从您的视图中使用它 如果您确实需要建模,但我建议您根据 MVVM 原则从您的视图中调用它。
我最近使用它并在 How to get started with NFC and Xamarin in 5 minutes 上记录了我的旅程。