我是c#和UWP的新手,这是一项任务,我处于严格的截止日期,所以没有时间完全解决问题,所以请原谅凌乱的代码。
我有以下代码,它通过创建AMQP连接并在一个常量while循环中从Azure IOT Hub接收消息来工作。
它有适当的分配问题,但主要的是当我从访问另一个页面返回时,GUI停止更新文本字段。
实际上它确实从OnNavigatedTo更新它们,但接收消息的方法停止更新它们,但是在输出窗口中我可以看到debug.write正在接收消息,只是没有更新文本字段。
我想我需要在一个单独的线程上运行while循环,我不认为我已经在下面完成了这个,我已经读过Task.Run等并尝试了各种方法,但无法弄清楚。
public MainPage()
{
this.InitializeComponent();
if (receivedStarted != 1)
Receive();
}
private async void Receive()
{
await ReceiveMessages("1");
await ReceiveMessages("0");
}
/// <summary>
/// On Navigated to function from other pages
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Counter.Text = parsedCounter;
TimeText.Text = "The last activity was at " + parsedTime;
Contact_1.Content = ContactAddPopUp.contact[0, 0];
Contact_2.Content = ContactAddPopUp.contact[1, 0];
Contact_3.Content = ContactAddPopUp.contact[2, 0];
if (Settings.timeChanged == true)
{
if (DelayTimer != null)
{
DelayTimer.Cancel();
Timer();
}
}
}
/// <summary>
/// Receive messages from specified azure iothub on specified partition. The MessageManager parses the received message and displays it accordingly
/// </summary>
/// <param name="partition"></param>
/// <returns></returns>
public async Task ReceiveMessages(string partition)
{
DateTime offset;
offset = DateTime.UtcNow - TimeSpan.FromMinutes(1);
String primaryKey = "fghkfwhihelfihefjw;ojwef";
String sharedAccessPolicy = "iothubowner";
//String hubName = "RaspberryPirSensor";
String deviceName = "PIRSensor";
String eventHubEntity = "iothub-ehub-raspberryp-70680-20f0331ccb";
string port = "iakugfdkjhkjhaflhlkhalkfhse.servicebus.windows.net";
Address address = new Address(port, 5671, sharedAccessPolicy, primaryKey, "/", "amqps");
Connection connection = await Connection.Factory.CreateAsync(address);
Session session = new Session(connection);
string totalMilliseconds = ((long)(offset - new DateTime(StartOfEpoch, DateTimeKind.Utc)).TotalMilliseconds).ToString();
Map filters = new Map();
filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"),
new DescribedValue(
new Amqp.Types.Symbol("apache.org:selector-filter:string"),
"amqp.annotation.x-opt-enqueuedtimeutc > " + totalMilliseconds + ""));
ReceiverLink receiver = new ReceiverLink(session,
"my-receiver",
new global::Amqp.Framing.Source()
{
Address =
eventHubEntity + "/ConsumerGroups/$Default/Partitions/" + partition,
FilterSet = filters
}, null);
Amqp.Types.Symbol deviceIdKey = new Amqp.Types.Symbol("iothub-connection-device-id");
string deviceId = deviceName;
while (true)
{
if (timerCreated == false)
Timer();
receivedStarted = 1;
Amqp.Message m = await receiver.ReceiveAsync(10000);
if (m != null)
{
var id = m.MessageAnnotations.Map[deviceIdKey].ToString();
if (id == deviceId)
{
Data data = (Data)m.BodySection;
string msg = System.Text.Encoding.UTF8.GetString(data.Binary, 0, data.Binary.Length);
bool isValid = ValidateMessage(msg);
if (isValid)
{
receiver.Accept(m);
Counter.Text = parsedCounter;
Debug.Write("Receiving Message " + parsedCounter );
TimeText.Text = "The last activity was at " + parsedTime;
//Connection String
if (connection != null)
ConnectedTextBox.Text = "CONNECTED";
else
ConnectedTextBox.Text = "DISCONNECTED";
if (DelayTimer != null)
DelayTimer.Cancel();
}
else
{
receiver.Release(m);
}
}
}
}
}
答案 0 :(得分:0)
我实际上通过使用 - 解决了这个问题 this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; - 在MainPage的构造函数中。
每当我导航到页面时,我都会重新初始化页面,我想这会导致它与等待的任务失去同步。