如何将字符串从托管C#/ Xaml WP8组件传递到D3D组件?

时间:2013-04-08 15:41:46

标签: c# .net windows-phone-7 directx windows-phone-8

我的目标:将字符串传递给C ++ - 从C#/ Xaml编写的Windows Phone Runtime组件编写的Direct3DComponent。

我的理解:这是使用D3DInterop完成的。

更具体地说,我正在使用带有Xaml应用程序模板的D3D并尝试将值传递给在加载DrawingSurfaceGrid时创建的Direct3DBackground对象。

模板作者通过仅复制诸如整数之类的简单数据类型来完成此操作,但没有给出编组字符串的示例。我得出结论,最终解决方案将比简单的变量赋值更复杂,例如第一次调用首先分配足够大的缓冲区,然后第二次实际复制到字节中。

首先是项目的托管部分,它将数据传递给D3DComponent,然后是接收数据的类Direct3DBackground类声明。

    private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e)
    {
        if (m_d3dBackground == null)
        {
            m_d3dBackground = new Direct3DBackground();

            // Set window bounds in dips
            m_d3dBackground.WindowBounds = new Windows.Foundation.Size(
                (float)Application.Current.Host.Content.ActualWidth,
                (float)Application.Current.Host.Content.ActualHeight
                );

            // Set native resolution in pixels
            m_d3dBackground.NativeResolution = new Windows.Foundation.Size(
                (float)Math.Floor(Application.Current.Host.Content.ActualWidth * Application.Current.Host.Content.ScaleFactor / 100.0f + 0.5f),
                (float)Math.Floor(Application.Current.Host.Content.ActualHeight * Application.Current.Host.Content.ScaleFactor / 100.0f + 0.5f)
                );

            // Set render resolution to the full native resolution
            m_d3dBackground.RenderResolution = m_d3dBackground.NativeResolution;

            // Get the location of the model from the query string
            string ModelLocation;
            if (this.NavigationContext.QueryString.ContainsKey("ModelLocation"))
            {
                ModelLocation = this.NavigationContext.QueryString["ModelLocation"];
            }

            // then somehow pass the value of ModelLocation to m_d3dBackground
            /// ???

            // Hook-up native component to DrawingSurfaceBackgroundGrid
            DrawingSurfaceBackground.SetBackgroundContentProvider(m_d3dBackground.CreateContentProvider());
            DrawingSurfaceBackground.SetBackgroundManipulationHandler(m_d3dBackground);
        }
    }

接收此数据的C ++类:

public ref class Direct3DBackground sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler
{
public:
    Direct3DBackground();

    Windows::Phone::Graphics::Interop::IDrawingSurfaceBackgroundContentProvider^ CreateContentProvider();

    // IDrawingSurfaceManipulationHandler
    virtual void SetManipulationHost(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ manipulationHost);

    event RequestAdditionalFrameHandler^ RequestAdditionalFrame;

    property Windows::Foundation::Size WindowBounds;
    property Windows::Foundation::Size NativeResolution;
    property Windows::Foundation::Size RenderResolution;
    // property System::String ModelLocationUri; // The solution is more complex than this

protected:
    // Event Handlers
    void OnPointerPressed(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
    void OnPointerReleased(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
    void OnPointerMoved(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);

internal:
    HRESULT Connect(_In_ IDrawingSurfaceRuntimeHostNative* host, _In_ ID3D11Device1* device);
    void Disconnect();

    HRESULT PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Inout_ DrawingSurfaceSizeF* desiredRenderTargetSize);
    HRESULT Draw(_In_ ID3D11Device1* device, _In_ ID3D11DeviceContext1* context, _In_ ID3D11RenderTargetView* renderTargetView);

private:
    CubeRenderer^ m_renderer;
    BasicTimer^ m_timer;
};

1 个答案:

答案 0 :(得分:3)

这就是为什么你有Platform.String传递字符串的原因。将以下属性添加到D3DInterop类:

property Platform::String ^Name;

现在您可以使用C#代码设置它:

m_d3dInterop.Name = "Whatever you like";