在非UWP应用程序中使用Windows混合现实

问题描述 投票:1回答:3

我正在维护一个C#WPF应用程序,我想为它添加Windows混合现实支持。

将应用程序移植到UWP可能不是一个好主意,因为该应用程序支持许多其他没有UWP变体的API。例如Oculus,OSVR和OpenVR(Vive)支持。不过,我没有足够的UWP经验。

那么,是否有可能在非UWP应用程序中使用混合现实UWP API?或者将中间件API移植到UWP并不是那么可怕?

c# uwp windows-10 windows-10-universal windows-mixed-reality
3个回答
1
投票

是的,可以在任何非UWP应用程序中使用UWP API,因为所有UWP API实际上都是COM。我通常更喜欢使用C++/WinRT,但它有C ++ 17语言的限制。

如果您无法接受此限制,则可以使用经典的COM

    Microsoft::WRL::ComPtr<ABI::Windows::UI::Input::Spatial::ISpatialInteractionManagerStatics> interactionManagerStatic;
    Windows::Foundation::GetActivationFactory(
        Microsoft::WRL::Wrappers::HStringReference(InterfaceName_Windows_UI_Input_Spatial_ISpatialInteractionManagerStatics).Get(),
        &interactionManagerStatic);

    Microsoft::WRL::ComPtr<ABI::Windows::UI::Input::Spatial::ISpatialInteractionManager> interactionManager;
    if (FAILED(interactionManagerStatic->GetForCurrentView(interactionManager.GetAddressOf())))
    {
        return -1;
    }

1
投票

可悲的是,混合现实API都内置于UWP平台,要运行MR,它需要在UWP应用程序中。唯一的另一种方法是将项目构建为Steam VR应用程序,但我不认为这与您要实现的目标兼容。

我最好的建议是尝试让您的项目尽可能跨平台。将所有逻辑放在Netcore / PCL项目中,并在单独的项目中为WPF和UWP提供两个不同的UI层。


1
投票

如果要从WPF / winforms项目访问它,也可以在c#中实现该接口。要使用下面的代码,请添加对Microsoft.Windows.SDK.Contracts nuget包的引用,例如:https://www.nuget.org/packages/Microsoft.Windows.SDK.Contracts/10.0.18362.2002-preview

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.UI.Input.Spatial;

namespace UWPInterop
{
    //MIDL_INTERFACE("5C4EE536-6A98-4B86-A170-587013D6FD4B")
    //ISpatialInteractionManagerInterop : public IInspectable
    //{
    //public:
    //    virtual HRESULT STDMETHODCALLTYPE GetForWindow(
    //        /* [in] */ __RPC__in HWND window,
    //        /* [in] */ __RPC__in REFIID riid,
    //        /* [iid_is][retval][out] */ __RPC__deref_out_opt void** spatialInteractionManager) = 0;

    //};
    [System.Runtime.InteropServices.Guid("5C4EE536-6A98-4B86-A170-587013D6FD4B")]
    [System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIInspectable)]
    interface ISpatialInteractionManagerInterop
    {
        SpatialInteractionManager GetForWindow(IntPtr Window, [System.Runtime.InteropServices.In] ref Guid riid);
    }

    //Helper to initialize SpatialInteractionManager
    public static class SpatialInteractionManagerInterop
    {
        public static SpatialInteractionManager GetForWindow(IntPtr hWnd)
        {
            ISpatialInteractionManagerInterop spatialInteractionManagerInterop = (ISpatialInteractionManagerInterop)WindowsRuntimeMarshal.GetActivationFactory(typeof(SpatialInteractionManager));
            Guid guid = typeof(SpatialInteractionManager).GUID;

            return spatialInteractionManagerInterop.GetForWindow(hWnd, ref guid);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.