void device_DeviceArrived(ProximityDevice sender)
{
//Compatible Device enters area
if (stance == WriteStage.PREP)
{
System.Diagnostics.Debug.WriteLine("Writestages won");
//Perhaps here
updateStatusRectangle(Colors.Yellow);
stance = WriteStage.WRITING;
updateStatusText("Writing...");
writeToTag(msg);
}
else
{
updateReceivedText("Device connected!");
}
}
private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
{
System.Diagnostics.Debug.WriteLine("Handler ran");
var rawMsg = message.Data.ToArray();
var ndefMessage = NdefMessage.FromByteArray(rawMsg);
foreach (NdefRecord record in ndefMessage)
{
System.Diagnostics.Debug.WriteLine("Record type: " + Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
var specType = record.CheckSpecializedType(false);
if (specType == typeof(NdefTextRecord))
{
var textrec = new NdefTextRecord(record);
updateReceivedText(textrec.Text);
}
}
}
上述事件和處理程序在手機與NFC設備接觸時執行。對於這個應用程序的意圖和目的,我需要確保在寫入卡之前,如果它已經有內容,它會提示用戶驗證覆蓋數據。我評論了我認爲應該去的地方,但就檢查消息而言,我不知道如何去做。我不能在沒有ProximityMessage的情況下調用處理程序,並且我不知道另一種查看消息的方式。使用覆蓋警告檢查內容標籤
問題:是否可以從device_DeviceArrived調用MessageReceivedHandler(或者檢查消息)? (注意:Debug.Writelines是爲了測試目的,而這只是一個快速的NFC編寫器,我正在扔在一起)。
更新:在試圖找到解決辦法時,我遇到了一個不同的問題。
public bool promptUserForOverwrite()
{
bool response = false;
Dispatcher.BeginInvoke(() =>
{
MessageBoxResult cc = MessageBox.Show("You are about to overwrite data. Proceed?", "Overwrite?", MessageBoxButton.OKCancel);
if (cc == MessageBoxResult.OK)
{
System.Diagnostics.Debug.WriteLine("MessageBox OK result");
response = true;
}
});
return response;
}
private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
{
System.Diagnostics.Debug.WriteLine("Handler ran");
var rawMsg = message.Data.ToArray();
var ndefMessage = NdefMessage.FromByteArray(rawMsg);
foreach (NdefRecord record in ndefMessage)
{
System.Diagnostics.Debug.WriteLine("Record type: " + Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
var specType = record.CheckSpecializedType(false);
if (specType == typeof(NdefTextRecord))
{
var textrec = new NdefTextRecord(record);
updateReceivedText(textrec.Text);
}
}
bool pow = promptUserForOverwrite();
if (!pow)
{
System.Diagnostics.Debug.WriteLine("Prompt returned");
//This always hits - pow is always false.
}
if (stance == WriteStage.WRITING && pow)
{
//writeToTag(msg);
}
}
這將作爲一種工作;問題是beginInvoke方法。我需要它來交叉線程訪問,但像這樣使用它似乎使它在稍後運行(當線程是免費的?)。 bool pow始終是錯誤的,即使我在messagebox上單擊確定(已調試,並且確實得到結果,但在我不能再使用它之後)。有沒有其他的方法可以用於Dispatcher?
不幸的是,我我已經嘗試了異步/等待,但不是這個實現。無論如何,問題依然存在;在執行時,它似乎跳過處理程序中的pow聲明,並且只在處理程序完成時才運行調度程序。 – Carlos
以新(難看)代碼更新 –
這看起來像它會根據需要工作,但由於某種原因它不起作用。我實現了它,但得到了System.AggregateException。在調試過程中,問題仍然存在:它似乎跳過了ContinueWith方法(在if語句中表示pow.Result尚未計算,在調試器中檢查pow的值。它運行在不同的時間嗎? – Carlos