我的目标:将字符串从 C#/Xaml 编写的 Windows Phone 运行时组件传递到 C++ 编写的 Direct3DComponent。
我的理解:这是使用 D3DInterop 完成的。
更具体地说,我将 D3D 与 Xaml 应用程序模板一起使用,并尝试将值传递给加载 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;
};