Windows Phone 8.1 的最小应用程序(无 XAML):与桌面应用程序的差异
Minimal app for Windows Phone 8.1 (no XAML) : differences with desktop app
我开始使用 C++ 开发 Windows 运行时应用程序。我在 Win32/C++ 开发方面经验丰富,但这对我来说是一个新世界。
以下是我输入的一个最小的、没有 XAML 的应用程序,它从各种来源(MSDN、DirectXTutorial.com 等)收集信息。
我的问题是:在 Windows 桌面中,显示空白 window 并接收 PointerPressed 事件效果很好。但是在 Windows Phone 中,我只到达了应用程序启动徽标,但没有任何反应。
这个最小的应用程序在两个平台之间有什么区别?如果是 Windows Phone 8.1 平台,我是否需要创建一些 DirectX 或绘图表面?
我正在使用 WIndows 10 主机加上 Visual Studio 2015。谢谢。
#include "pch.h"
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace Platform;
ref class dxAppView sealed : IFrameworkView
{
bool _bActive;
public:
virtual void Initialize(CoreApplicationView ^applicationView)
{
applicationView->Activated += ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &dxAppView::OnActivated);
CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs^>(this, &dxAppView::OnSuspending);
CoreApplication::Resuming += ref new EventHandler<Object ^>(this, &dxAppView::OnResuming);
_bActive = true;
}
virtual void SetWindow(CoreWindow ^window)
{
window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &dxAppView::OnPointerPressed);
window->Closed += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^, Windows::UI::Core::CoreWindowEventArgs ^>(this, &dxAppView::OnClosed);
}
virtual void Load(String ^entryPoint) {}
virtual void Run()
{
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
while (_bActive)
{
wnd->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
}
virtual void Uninitialize() {}
void OnActivated(CoreApplicationView ^sender, IActivatedEventArgs ^args)
{
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
wnd->Activate();
}
void OnPointerPressed(CoreWindow ^sender, PointerEventArgs^ args)
{
Windows::UI::Popups::MessageDialog dlg(L"Hi From Windows Runtime app.");
dlg.ShowAsync();
}
void OnSuspending(Platform::Object ^sender, Windows::ApplicationModel::SuspendingEventArgs ^args){}
void OnResuming(Platform::Object ^sender, Platform::Object ^args){}
void OnClosed(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::CoreWindowEventArgs ^args)
{
_bActive = false;
}
};
ref class dxAppFrameworkViewSource sealed : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
{
return ref new dxAppView;
}
};
[MTAThread]
int main(Array<String^>^ args)
{
CoreApplication::Run(ref new dxAppFrameworkViewSource());
return 0;
};
我发现了,似乎在 Phone 8.1 中没有 DX 交换链处于活动状态时您将无法执行任何操作。因此,最小的 no-XAML 应用程序涉及以下附加代码:
创建 Direct3D 设备
(下划线前缀的变量为class成员)
ComPtr<ID3D11DeviceContext> pDevCtx;
ComPtr<ID3D11Device> pDev;
ComPtr<IDXGISwapChain> pSwapChain;
D3D_FEATURE_LEVEL fLevel;
HRESULT hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&pDev,
&fLevel,
&pDevCtx);
创建交换链
pDevCtx.As(&_pDevCtx);
pDev.As(&_pDevice);
ComPtr<IDXGIDevice1> pDXGIDev;
pDev.As(&pDXGIDev);
ComPtr<IDXGIAdapter> pDXGIAdapter;
pDXGIDev->GetAdapter(&pDXGIAdapter);
ComPtr<IDXGIFactory2> pDXGIFactory;
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory2), &pDXGIFactory);
DXGI_SWAP_CHAIN_DESC1 swChDesc = { 0 };
swChDesc.BufferCount = 2;
swChDesc.SampleDesc.Count = 1;
swChDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swChDesc.SampleDesc.Quality = 0;
swChDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swChDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
pDXGIFactory->CreateSwapChainForCoreWindow(
_pDevice.Get(),
(IUnknown*) wnd,
&swChDesc,
nullptr,
_pSwapChain.GetAddressOf());
现在(翻转你的缓冲区)
while (_bActive)
{
wnd->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
Render();
}
其中 Render() 可以是这样的:
_pSwapChain->Present(1, NULL);
我开始使用 C++ 开发 Windows 运行时应用程序。我在 Win32/C++ 开发方面经验丰富,但这对我来说是一个新世界。
以下是我输入的一个最小的、没有 XAML 的应用程序,它从各种来源(MSDN、DirectXTutorial.com 等)收集信息。
我的问题是:在 Windows 桌面中,显示空白 window 并接收 PointerPressed 事件效果很好。但是在 Windows Phone 中,我只到达了应用程序启动徽标,但没有任何反应。
这个最小的应用程序在两个平台之间有什么区别?如果是 Windows Phone 8.1 平台,我是否需要创建一些 DirectX 或绘图表面?
我正在使用 WIndows 10 主机加上 Visual Studio 2015。谢谢。
#include "pch.h"
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace Platform;
ref class dxAppView sealed : IFrameworkView
{
bool _bActive;
public:
virtual void Initialize(CoreApplicationView ^applicationView)
{
applicationView->Activated += ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &dxAppView::OnActivated);
CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs^>(this, &dxAppView::OnSuspending);
CoreApplication::Resuming += ref new EventHandler<Object ^>(this, &dxAppView::OnResuming);
_bActive = true;
}
virtual void SetWindow(CoreWindow ^window)
{
window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &dxAppView::OnPointerPressed);
window->Closed += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^, Windows::UI::Core::CoreWindowEventArgs ^>(this, &dxAppView::OnClosed);
}
virtual void Load(String ^entryPoint) {}
virtual void Run()
{
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
while (_bActive)
{
wnd->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
}
virtual void Uninitialize() {}
void OnActivated(CoreApplicationView ^sender, IActivatedEventArgs ^args)
{
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
wnd->Activate();
}
void OnPointerPressed(CoreWindow ^sender, PointerEventArgs^ args)
{
Windows::UI::Popups::MessageDialog dlg(L"Hi From Windows Runtime app.");
dlg.ShowAsync();
}
void OnSuspending(Platform::Object ^sender, Windows::ApplicationModel::SuspendingEventArgs ^args){}
void OnResuming(Platform::Object ^sender, Platform::Object ^args){}
void OnClosed(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::CoreWindowEventArgs ^args)
{
_bActive = false;
}
};
ref class dxAppFrameworkViewSource sealed : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
{
return ref new dxAppView;
}
};
[MTAThread]
int main(Array<String^>^ args)
{
CoreApplication::Run(ref new dxAppFrameworkViewSource());
return 0;
};
我发现了,似乎在 Phone 8.1 中没有 DX 交换链处于活动状态时您将无法执行任何操作。因此,最小的 no-XAML 应用程序涉及以下附加代码:
创建 Direct3D 设备
(下划线前缀的变量为class成员)
ComPtr<ID3D11DeviceContext> pDevCtx;
ComPtr<ID3D11Device> pDev;
ComPtr<IDXGISwapChain> pSwapChain;
D3D_FEATURE_LEVEL fLevel;
HRESULT hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&pDev,
&fLevel,
&pDevCtx);
创建交换链
pDevCtx.As(&_pDevCtx);
pDev.As(&_pDevice);
ComPtr<IDXGIDevice1> pDXGIDev;
pDev.As(&pDXGIDev);
ComPtr<IDXGIAdapter> pDXGIAdapter;
pDXGIDev->GetAdapter(&pDXGIAdapter);
ComPtr<IDXGIFactory2> pDXGIFactory;
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory2), &pDXGIFactory);
DXGI_SWAP_CHAIN_DESC1 swChDesc = { 0 };
swChDesc.BufferCount = 2;
swChDesc.SampleDesc.Count = 1;
swChDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swChDesc.SampleDesc.Quality = 0;
swChDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swChDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
CoreWindow^ wnd = CoreWindow::GetForCurrentThread();
pDXGIFactory->CreateSwapChainForCoreWindow(
_pDevice.Get(),
(IUnknown*) wnd,
&swChDesc,
nullptr,
_pSwapChain.GetAddressOf());
现在(翻转你的缓冲区)
while (_bActive)
{
wnd->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
Render();
}
其中 Render() 可以是这样的:
_pSwapChain->Present(1, NULL);