c# - UWP“当前上下文中不存在名称调度程序”
问题描述
在 UWP C# 应用程序中,需要后台(即工作线程)线程来使用 UI 线程来显示图像。但是不知道怎么编译Dispatcher.RunAsync()
。
using Foundation;
using System;
using UIKit;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using System.Threading;
using System.Windows.Threading; <<<<<<<<<< gets error
using Windows.UI.Core; <<<<<<<<<< gets error
public async static void process_frame()
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// "the name dispatcher does not exist in current context"
//UI code here:
display_frame();
});
}
public void display_frame()
{
var data = NSData.FromArray(System_Hub_Socket.packet_frame_state.buffer);
UIImageView_camera_frame.Image = UIImage.LoadFromData(data);
}
最新方法
public async static void process_frame( /* ax obsolete: byte[] camera_frame_buffer, int frame_size_bytes */ )
{
await Task.Run( () => { viewcontroller.display_frame(); } );
}
// [3]
// Copies latest frame from camera to UIImageView on iPad.
// UI THREAD
public Task display_frame()
{
var data = NSData.FromArray ( System_Hub_Socket.packet_frame_state.buffer);
<<<<<< ERROR
UIImageView_camera_frame.Image = UIImage.LoadFromData( data );
return null;
}
最新方法的错误
解决方案
查看代码中的using
语句:
using UIKit;
...
using Windows.UI.Core;
这不可能发生。UIKit
是 Xamarin.iOS,平台特定的命名空间,并且Windows.UI.Core
是 Windows 平台特定的命名空间,这两者决不能混合在一个文件中(除了带有#if
指令的共享项目,但这里不是这种情况)。
Xamarin 有助于编写跨平台应用程序,但您仍然不能在不可用的操作系统上使用特定于平台的 API。Windows 有Dispatcher
一种在 UI 线程上运行代码的方法,但这个概念在 iOS 上不可用,而是使用InvokeOnMainThread
方法。
因此,如果您正在编写特定于平台的 iOS 项目中的代码,则必须使用 iOS API。如果您正在编写特定于平台的 UWP 项目中的代码,则必须使用 UWP API - 诸如此类的东西Dispatcher
在那里可以毫无问题地工作。
最后,如果您在 .NET Standard 库中编写代码,则不能直接编写任何特定于平台的代码,并且必须使用依赖注入来定义一个接口,在该接口后面隐藏平台特定 API 的使用。
推荐阅读
- shell - 我在自己安装了 6.7 版本后安装了 5.1.6-1 dse cassandra
- r - 在R中使用download.file之前如何检查url中是否存在文件
- python-3.x - 将时间戳转换为日期应用于 Python 中的多个列
- c++ - 无法使用 Qt Creator 在 Windows 中构建 yaml-cpp
- javascript - 上传多个文件并显示进度条
- angular - 当以角度禁用表格中的按钮时,下一行按钮会随着数组长度的变化而变化
- javascript - ASP.NET Web API 中的表单
- python-3.x - 如何使用 FastAPI 返回元素
- sql - Postgresql - 如何从没有出现特定数据的同一个表中获取记录
- angular - 需要帮助解决 Angular 9 TypeScript 错误(TypeCasting)