c# - 如何从 viewmodel 使用 iOS/Xamarin 扫描 NFC 标签
<p><p>我有一个适用于 Android/iOS 的跨平台 Xamarin.Forms .net 标准应用程序,我想添加 nfc 扫描功能。</p>
<p>对于我的第一个测试,我将所有内容都放入了我的 <em>AppDelegate</em> 类中。
<strong>此代码有效:</strong></p>
<pre><code>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.Records.Payload.Skip(3).ToArray();
var message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("ServiceToolStandard DidDetect: " + message);
}
}
</code></pre>
<h2><strong>我的问题:</strong></h2>
<p>之后,我将所有内容都放入了一个新的 <em>NfcScanner</em> 类中,并希望从我的 ViewModel 中调用 <em>ScanAsync</em> 函数。如果我运行这段代码,一切看起来都很好。当我按下手机上的按钮时扫描开始,它显示蓝色勾号,但随后<strong>它永远不会进入已实现的方法之一<em>DidDetect</em>、<em>DidInvalidate</em>。你知道可能是什么原因吗?</strong></p>
<pre><code>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.Records.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");
}
}
</code></pre>
<p>我还尝试制作一个无效的 <em>Scan()</em> 方法并直接从 <em>AppDelegate</em> 调用它。结果是一样的。扫描窗口打开并显示蓝色勾号,但它从未到达方法 diddetect、didinvalidate。</p>
<hr/>
<h2><strong>编辑 07.02.2018 16:56:</strong></h2>
<p>我在示例代码中看到,它们的类派生自 UITableViewController。
这是我的代码和示例代码之间唯一真正的区别。在我从 <em>ViewController</em> 派生我的 <em>NfcScanner</em> 类后,它起作用了。但我不确定为什么以及它是否是正确的方法?</p>
<pre><code>public class NfcScanner : UIViewController, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
</code></pre>
<p>非常感谢您的帮助!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我试图找到一个答案,为什么它在我从 <em>UIViewController</em> 派生后起作用。我想我在本教程中找到了它:<a href="http://jamesonquave.com/blog/core-nfc-tutorial-for-nfc-on-ios-devices/" rel="noreferrer noopener nofollow">Core NFC Tutorial</a> </p>
<blockquote>
<p>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. <strong>This delegate is based on an Objective-C
object, so we must first also adhere to NSObject.</strong>
NFCNDEFReaderSessionDelegate has two delegate methods we must
implement:</p>
</blockquote>
<p><em>UIViewController</em> 继承自 <em>NSObject</em>。现在我将代码更改为:</p>
<pre><code>public class NfcScanner : NSObject, INfcScanner, INFCNdefReaderSessionDelegate
{
...
}
</code></pre></p>
<p style="font-size: 20px;">关于c# - 如何从 viewmodel 使用 iOS/Xamarin 扫描 NFC 标签,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48645959/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48645959/
</a>
</p>
页:
[1]