我正在维护一个Windows应用程序,我想在其中添加C#混合现实支持。
将该应用程序移植到UWP可能不是一个很好的主意,因为该应用程序支持许多没有UWP变体的其他app。例如,Oculus、OSVR和OpenVR(Vive)支持。不过,我没有足够的UWP经验来确定。
那么,有没有可能在非UWP应用程序中使用混合现实的UWP API呢?或者,也许将中间件API移植到UWP并不那么可怕?
发布于 2019-04-25 10:05:37
是的,在任何非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;
}发布于 2019-06-25 18:52:42
如果希望从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);
}
}
}发布于 2018-04-05 11:22:10
遗憾的是,混合现实API都内置于UWP平台中,要运行MR,它需要在一个UWP应用程序中。唯一的其他方法是建立一个项目作为一个蒸汽虚拟现实应用程序,但我不认为这是兼容的,你正在努力实现。
我最好的建议是让你的项目尽可能的跨平台。将所有的逻辑放在一个Netcore / PCL项目中,并在不同的项目中为WPF和UWP提供两个不同的UI层。
https://stackoverflow.com/questions/47941106
复制相似问题