簡體   English   中英

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桌面上,通過顯示空白窗口並接收PointerPressed事件,可以很好地工作。 但是在Windows Phone中,我只到達應用程序啟動徽標,什么也沒有發生。

這個最小應用程序的兩個平台之間有什么區別? 對於Windows Phone 8.1平台,我是否需要創建一些DirectX或繪圖圖面?

我使用的是Windows 10主機以及Visual Studio2015。謝謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM