我正在开发一个在raspberry-pi3上使用windows-10-t平台的应用程序。应用程序有几个页面,并在后台异步地侦听GPIO端口。它从GPIO收集数据并发送到WCFService,经过一段时间后,用户界面应该被来自WCFService的数据更新。我也尝试过使用任务、Dispatcher.Invoke等,但是没有什么能正常工作。我可以收集来自GPIO的数据,但不能更新UI。我做错了什么?
下面是带有静态变量的背景GPIO侦听器类(我也在其他页面中侦听GPIO ):
public sealed class GPIO{
private static MainPage mainpage;
public static event EventHandler ProgressUpdate;
public static void InitGPIO(MainPage sender)
{
mainpage = sender;
DataPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);
DataPin.ValueChanged += DataPin_ValueChanged;
}
public static void DataPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
Task.Run(() => AddData(0));
}
}
public static async void AddData(int prm_Data)
{
// WCF-Service Operation
await Service.wsClient.GPIOValueAddition(prm_Data);
GPIO.ProgressUpdateOperation();
}
private static void ProgressUpdateOperation()
{
mainpage.GPIO_ProgressUpdate(typeof(GPIO), new EventArgs());
}
}下面是包含要更新的UI的页面:
public sealed partial class MainPage : Page
{
public MainPage()
{
GPIO.InitGPIO(this);
GPIO.ProgressUpdate += GPIO_ProgressUpdate;
}
public void GPIO_ProgressUpdate(object sender, EventArgs e)
{
// WCF-Service Operation
service_data = (int)Service.wsClient.GetDataFromServicetoUpdateUI(parameter).Result;
// UI-update
txtUpdate.Text = service_data.ToString();
}
}编辑:,我忘了添加异常。“应用程序称为为不同线程封送的接口。(来自HRESULT: 0x80010E(RPC_E_WRONG_THREAD)的异常)”异常将抛出在AddData函数中,在DataPin_Valuechanged中调用。
发布于 2016-12-09 15:53:23
我在这里找到了解决方案:https://stackoverflow.com/a/27698035/1093584
下面是新的update函数:
public void GPIO_ProgressUpdate(object sender, EventArgs e)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
service_data = await Service.wsClient.GetDataFromServicetoUpdateUI(parameter);
// UI-update
txtUpdate.Text = service_data.ToString();
});
}https://stackoverflow.com/questions/41061251
复制相似问题