如何将函数调用或触发事件从Native组件转换为C#/ XAML组件? [英] How does one make function calls or trigger events from a Native component into a C#/XAML component?

查看:168
本文介绍了如何将函数调用或触发事件从Native组件转换为C#/ XAML组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个带有Native(DirectX / D3D)组件和C#/ XAML组件的WP8应用程序。



Native组件绘制一个UI元素, C#/ XAML组件有它周围的应用程序(和其他东西)的控制。通常,我从C#/ XAML组件向下发送信息到Native组件。但有一些时候,当我想在C#/ XAML组件中基于何时在Native组件中完成处理时触发事件。在一个计划的功能中,我想象一个C#/ XAML进度条,在Native组件中触发的事件保持最新。



不幸的是,尽管一些平庸的尝试从Native组件传递到C#/ XAML的信息,我没有取得什么进展,我在水中感到死。



我非常感谢任何指导这个。感谢您阅读。

解决方案

经过多番探索(以及在这个线程中回答的一些人的帮助)能够找到此解决方案:从C ++代码调用C#方法WP8



为了简单起见,我将在这里复制:





<$ p $

p> //。h
[Windows :: Foundation :: Metadata :: WebHostHidden]
public interface class ICallback
{
public:
virtual void Exec(Platform :: String ^ Command,Platform :: String ^ Param);
};
//.cpp
ICallback ^ CSCallback = nullptr;
void Direct3DInterop :: SetCallback(ICallback ^ Callback)
{
CSCallback = Callback;
}
// ...

if(CSCallback!= nullptr)
CSCallback-> Exec(Command,Param);

C#
public class CallbackImpl:ICallback
{
public void Exec(String Command,String Param)
{
//执行一些C#代码,如果你调用UI的东西,你将需要调用这个
//Deployment.Current.Dispatcher.BeginInvoke(()=> {
// // Lambda code
/ /}
}
}
// ...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback(CI);


I am developing a WP8 application with a Native (DirectX/D3D) component and a C#/XAML component.

The Native component draws to a UI element and the C#/XAML component has the controls of the app (and other things) surrounding it. Usually, I send information from the C#/XAML component down to the Native component. But there are certain times when I would like to trigger events in the C#/XAML component based on when processing is done in the Native component. In one planned feature, I envision a C#/XAML progress bar that is kept up to date by events triggered in the Native component.

Unfortunately, despite some mediocre attempts to get information passed from the Native component to the C#/XAML one, I haven't made much progress and I feel dead in the water.

I would greatly appreciate any guidance on this. Thanks for reading.

解决方案

After much poking around (and much help from some of the guys that answered in this thread!) I was able to find this solution: Calling C# method from C++ code in WP8

I will reproduce it here for simplicity's sake:

After a lot of headaches trying to figure out the required code, I think it's worth posting the final version here

C++/CX

//.h
[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:
    virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};
//.cpp
ICallback ^CSCallback = nullptr;
void Direct3DInterop::SetCallback( ICallback ^Callback)
{
    CSCallback = Callback;
}
//...

if (CSCallback != nullptr)
    CSCallback->Exec( "Command", "Param" );

C#
public class CallbackImpl : ICallback
{
    public void Exec(String Command, String Param)
    {
        //Execute some C# code, if you call UI stuff you will need to call this too
        //Deployment.Current.Dispatcher.BeginInvoke(() => { 
        // //Lambda code
        //}
    }
}
//...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback( CI);

这篇关于如何将函数调用或触发事件从Native组件转换为C#/ XAML组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆