AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ApplicationViewManager.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections;
6 using UnityEngine;
7 
8 #if !UNITY_EDITOR && UNITY_WSA && !(ENABLE_IL2CPP && NET_STANDARD_2_0)
9 using System.Threading.Tasks;
10 using Windows.ApplicationModel.Core;
11 using Windows.UI.Core;
12 using Windows.UI.ViewManagement;
13 using Windows.UI.Xaml;
14 using Windows.UI.Xaml.Controls;
15 #endif
16 namespace HoloToolkit.Unity
17 {
18  public delegate void ReturnValueCallback<TReturnValue>(TReturnValue returnValue);
19 
26  public class ApplicationViewManager : MonoBehaviour
27  {
28  private void Start()
29  {
30 #if !UNITY_EDITOR && UNITY_WSA && !(ENABLE_IL2CPP && NET_STANDARD_2_0)
31  UnityEngine.WSA.Application.InvokeOnUIThread(
32  () =>
33  {
34  Full3DViewId = ApplicationView.GetForCurrentView().Id;
35  }, true);
36 #endif
37  }
38 
39 #if !UNITY_EDITOR && UNITY_WSA && !(ENABLE_IL2CPP && NET_STANDARD_2_0)
40  static int Full3DViewId { get; set; }
41  static System.Collections.Concurrent.ConcurrentDictionary<int, Action<object>> CallbackDictionary
42  = new System.Collections.Concurrent.ConcurrentDictionary<int, Action<object>>();
43 #endif
44 
49 #if !UNITY_EDITOR && UNITY_WSA && !(ENABLE_IL2CPP && NET_STANDARD_2_0)
50  public static async void CallbackReturnValue(object returnValue)
51  {
52  var viewId = ApplicationView.GetForCurrentView().Id;
53  var view = CoreApplication.GetCurrentView();
54  if (CallbackDictionary.TryRemove(viewId, out var cb))
55  {
56  try
57  {
58  cb(returnValue);
59  }
60  catch (Exception)
61  {
62 
63  }
64  await ApplicationViewSwitcher.TryShowAsStandaloneAsync(ApplicationViewManager.Full3DViewId).AsTask();
65  view.CoreWindow.Close();
66  }
67  }
68 #else
69  public static void CallbackReturnValue(object returnValue)
70  {
71  }
72 #endif
73  public IEnumerator OnLaunchXamlView<TReturnValue>(string xamlPageName, Action<TReturnValue> callback, object pageNavigateParameter = null)
81  {
82  bool isCompleted = false;
83 #if !UNITY_EDITOR && UNITY_WSA && !(ENABLE_IL2CPP && NET_STANDARD_2_0)
84  object returnValue = null;
85  CoreApplicationView newView = CoreApplication.CreateNewView();
86  int newViewId = 0;
87  var dispt = newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
88  {
89  //This happens when User switch view back to Main App manually
90  void CoreWindow_VisibilityChanged(CoreWindow sender, VisibilityChangedEventArgs args)
91  {
92  if (args.Visible == false)
93  {
94  CallbackReturnValue(null);
95  }
96  }
97  newView.CoreWindow.VisibilityChanged += CoreWindow_VisibilityChanged;
98  Frame frame = new Frame();
99  var pageType = Type.GetType(Windows.UI.Xaml.Application.Current.GetType().AssemblyQualifiedName.Replace(".App,", $".{xamlPageName},"));
100  var appv = ApplicationView.GetForCurrentView();
101  newViewId = appv.Id;
102  var cb = new Action<object>(rval =>
103  {
104  returnValue = rval;
105  isCompleted = true;
106  });
107  frame.Navigate(pageType,pageNavigateParameter);
108  CallbackDictionary[newViewId] = cb;
109  Window.Current.Content = frame;
110  Window.Current.Activate();
111 
112  }).AsTask();
113  yield return new WaitUntil(() => dispt.IsCompleted || dispt.IsCanceled || dispt.IsFaulted);
114  Task viewShownTask = null;
115  UnityEngine.WSA.Application.InvokeOnUIThread(
116  () =>
117  {
118  viewShownTask = ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId).AsTask();
119  },
120  true);
121  yield return new WaitUntil(() => viewShownTask.IsCompleted || viewShownTask.IsCanceled || viewShownTask.IsFaulted);
122  yield return new WaitUntil(() => isCompleted);
123  try
124  {
125  if (returnValue is TReturnValue)
126  {
127  callback?.Invoke((TReturnValue)returnValue);
128  }
129  else
130  {
131  callback?.Invoke(default(TReturnValue));
132  }
133  }
134  catch (Exception ex)
135  {
136  Debug.LogError(ex);
137  }
138 #else
139  isCompleted = true;
140  yield return new WaitUntil(() => isCompleted);
141 #endif
142  }
143  }
144 }
ApplicationViewManager ( For XAML UWP project) can switch app to Plan View, populate an Application V...
static void CallbackReturnValue(object returnValue)
Call this method with Application View Dispatcher, or in Application View Thread, will return to Full3D View and close Application View
delegate void ReturnValueCallback< TReturnValue >(TReturnValue returnValue)