Visual studio 在c+;中,从回调内部调用UI线程上的某些内容时出现问题+/cx

Visual studio 在c+;中,从回调内部调用UI线程上的某些内容时出现问题+/cx,visual-studio,asynchronous,dispatcher,c++-cx,ui-thread,Visual Studio,Asynchronous,Dispatcher,C++ Cx,Ui Thread,这是我的回调函数: void VideoCapturerSampleCallback(struct LmiVideoCapturer_* capturer, const LmiVideoFrame* videoFrame, LmiVoidPtr userData) { //TODO will have to be on ui thread; Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow-

这是我的回调函数:

void VideoCapturerSampleCallback(struct LmiVideoCapturer_* capturer, const LmiVideoFrame* videoFrame, LmiVoidPtr userData)
{
  //TODO will have to be on ui thread;
  Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(0, ref new Windows::UI::Core::DispatchedHandler([this]()
{
    Windows::UI::Xaml::Media::Imaging::BitmapImage^ bitmapImage =
        ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
    auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/1.jpg");
    if (first){
        first = false;
    }
    else{
        uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/1.jpg");
        first = true;
    }
    bitmapImage->UriSource = uri;
    media->Source = bitmapImage;
}));
}
MainPage::MainPage()
{
InitializeComponent();
media = this->player;
context = this;
}
每次调用回调时,基本上都会更改图片,现在,我只需更改资产中的两张图片,以查看它是否有效。 这是我在日志中看到的错误:

Error   17  error C1903: unable to recover from previous error(s); stopping compilation C:\Users\Alin Rosu\Downloads\App2\App2\MainPage.xaml.cpp    109 1   App2
Error   16  error C3482: 'this' can only be used as a lambda capture within a non-static member function    C:\Users\Alin Rosu\Downloads\App2\App2\MainPage.xaml.cpp    93  1   App2
19  IntelliSense: 'this' may only be used inside a nonstatic member function    c:\Users\Alin Rosu\Downloads\App2\App2\MainPage.xaml.cpp    93  144 App2
18  IntelliSense: function "Windows::UI::Core::CoreDispatcher::RunAsync" cannot be called with the given argument list
        argument types are: (int, Windows::UI::Core::DispatchedHandler ^)
        object type is: Windows::UI::Core::CoreDispatcher ^ c:\Users\Alin Rosu\Downloads\App2\App2\MainPage.xaml.cpp    93  84  App2
现在是否可以在回调中使用[this],而不会出现错误? 我是一名Android开发人员,因此对我来说,对此的逻辑响应是创建一个全局变量App2::MainPage,然后在构造函数中,将“this”保存在其中,这样我就可以在那里拥有上下文,并尝试用它调用RunAsync,但它不起作用。
是否可以用另一种方式实例化DispatchedHandler?

如下声明上下文:

App2::MainPage^ context;
然后在主页面功能中进行设置:

void VideoCapturerSampleCallback(struct LmiVideoCapturer_* capturer, const LmiVideoFrame* videoFrame, LmiVoidPtr userData)
{
  //TODO will have to be on ui thread;
  Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(0, ref new Windows::UI::Core::DispatchedHandler([this]()
{
    Windows::UI::Xaml::Media::Imaging::BitmapImage^ bitmapImage =
        ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
    auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/1.jpg");
    if (first){
        first = false;
    }
    else{
        uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/1.jpg");
        first = true;
    }
    bitmapImage->UriSource = uri;
    media->Source = bitmapImage;
}));
}
MainPage::MainPage()
{
InitializeComponent();
media = this->player;
context = this;
}
然后我在VideoCaptureSampleCallback中这样做:

void VideoCapturerSampleCallback(struct LmiVideoCapturer_* capturer, const LmiVideoFrame* videoFrame, LmiVoidPtr userData)
{
App2::MainPage^ ctx = context;
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([ctx]()
{
    //TODO will need to set in here
    Windows::UI::Xaml::Media::Imaging::BitmapImage^ bitmapImage =
        ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
    auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/1.jpg");
    if (first){
        XTRACE(L"===================FIIIIIIIIIIIIRST =======================\n");
        first = false;
    }
    else{
        XTRACE(L"===================FIIIIIIIIIIIIRST FAAAALSEEEEE=======================\n");
        uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/2.jpg");
        first = true;
    }
    bitmapImage->UriSource = uri;
    media->Source = bitmapImage;
}));
}
这解决了我的问题,并且我能够在UI线程上修改UI