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



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

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

那么,是否知道是否可以在非UWP应用程序中使用混合现实UWP API?也许将中间件API移植到UWP并不那么令人恐惧?

,如果要从WPF/Winforms项目访问它,也可以在C#中实现接口。要使用下面的代码,请添加引用到Microsoft.windows.sdk.contracts Nuget软件包,例如:https://www.nuget.org/packages/packages/microsoft.windows.windows.sdk.sdk.contracts/10.0.0.0.0.18362.2002-preview/P>

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

是的,可以在任何非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;
    }

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

我最好的建议是尝试使您的项目尽可能跨平台。将所有逻辑放入NetCore/PCL项目中,并在单独的项目中使用两个不同的UI层。

最新更新