适用于Windows Phone 8.1的最小应用程序(无XAML):与桌面应用程序的差异

时间:2015-12-10 15:43:22

标签: c++ windows windows-phone-8.1 window activation

我开始用C ++开发Windows Runtime应用程序。我在Win32 / C ++开发方面经验丰富,但这对我来说是个新世界。

以下是我输入的最小,无XAML应用程序,从各种来源(MSDN,DirectXTutorial.com等)收集信息。

我的问题是:在Windows桌面中,通过显示空白窗口并接收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;
};

1 个答案:

答案 0 :(得分:0)

我发现它,似乎在没有DX交换链激活的情况下你无法在Phone 8.1中做任何事情。因此,最小的no-XAML应用程序涉及以下附加代码:

创建Direct3D设备

(以下划线为前缀的变量是类成员)

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);