代码之家  ›  专栏  ›  技术社区  ›  Doug Null

UWP“名称分派器在当前上下文中不存在”

  •  -1
  • Doug Null  · 技术社区  · 7 年前

    在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;
    }
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   Martin Zikmund    7 年前

    看着 using 代码中的语句:

    using UIKit;
    ...
    using Windows.UI.Core; 
    

    这是不可能发生的。 UIKit 是一个Xamarin.iOS、特定于平台的命名空间和 Windows.UI.Core 是特定于Windows平台的命名空间,决不能将这两个命名空间混合在一个文件中(除了与共享的项目) #if 指令,但这里的情况并非如此)。

    Xamarin有助于编写跨平台应用程序,但您仍然无法在操作系统上使用特定于平台的API,因为这些API在操作系统上不可用。Windows有 Dispatcher InvokeOnMainThread

    因此,如果您正在编写特定于平台的iOS项目中的代码,则必须使用iOS API。如果您正在从特定的UWP项目中编写平台中的代码,那么必须使用UWP API,例如 调度员 在那里工作没有问题。

    最后,如果在.NET标准库中编写代码,则不能直接编写任何特定于平台的代码,必须使用 dependency injection

    推荐文章