c++ - 适用于 Windows Phone 8.1 的最小应用程序(无 XAML): differences with desktop app

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

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

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

我的问题是:在 Windows 桌面中,通过显示空白窗口和接收 PointerPressed 事件效果很好。但在 Windows Phone 中,我只到达应用程序启动 Logo ,但没有任何反应。

这个最小的应用程序在两个平台之间有什么区别?如果是 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;
};

最佳答案

我发现,如果 DX 交换链处于事件状态,您似乎无法在 Phone 8.1 中执行任何操作。因此,最小的非 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);

关于c++ - 适用于 Windows Phone 8.1 的最小应用程序(无 XAML): differences with desktop app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206041/

相关文章:

c++ - 我怎样才能从数组中只返回几个值

c++回车换行长字符串后跟较短的字符串

c# - 连接到 VPN 会停止 UWP 应用程序互联网通信

windows - Minidump 创建空转储文件

windows-phone-8.1 - 如何在 Windows Phone 8.1 中查找 Wi-Fi 连接的信号强度?

c++ - 有没有办法传递函数模板作为另一个函数中的参数?

c++ - 如何在 C++ 中自动添加和获取 128 位数字?

c++ - windows C++ opening printer with documentproperties get C6836 "Write Overrun"代码分析警告

c# - 如何在 Windows Phone 8.1 RT 应用程序中滚动到 ListView 的底部?

c# - 如何在 Windows Universal 上流式传输视频种子?