【发布时间】:2019-05-06 05:29:23
【问题描述】:
在 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;
}
最新方法出错
【问题讨论】:
-
display_frame应该是public Task display_frame()如果你希望它像void,如果你需要返回一些东西,它应该是public Task<T>其中 T 是从返回的类型方法。然后你可以做await Task.Run(() => display_frame()); -
display_frame() 应该返回什么?我做了“public Task display_frame()”但是得到编译错误“不是所有的代码路径都返回一个值”。
-
所以我返回:return null;
-
return Task.CompletedTask();导致此错误:“不可调用的成员“Task.CompletedTask”不能像方法一样使用。”
-
对不起,我应该把
()关掉,它是一个属性而不是一个方法,做return Task.CompletedTask;文档(docs.microsoft.com/en-us/dotnet/api/…)
标签: c# uwp xamarin.ios xamarin.uwp