public async Task <int> ProjectAsync(Type viewType, DeviceInformation device = null) { int mainViewId = ApplicationView.GetForCurrentView().Id; int?secondViewId = null; var view = CoreApplication.CreateNewView(); await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { secondViewId = ApplicationView.GetForCurrentView().Id; var rootFrame = new Frame(); rootFrame.Navigate(viewType, null); Window.Current.Content = rootFrame; Window.Current.Activate(); }); if (secondViewId.HasValue) { if (device == null) { await ProjectionManager.StartProjectingAsync(secondViewId.Value, mainViewId); } else { await ProjectionManager.StartProjectingAsync(secondViewId.Value, mainViewId, device); } } return(mainViewId); }
public async Task StartProjectingAsync(int currentViewId, int newViewId) { if (ProjectionManager.ProjectionDisplayAvailable) { if (_selectedDeviceInformation == null) { _selectedDeviceInformation = await DeviceInformation.CreateFromIdAsync(SelectedDeviceID); } if (_selectedDeviceInformation != null) { bool isFound = false; do { try { await ProjectionManager.StartProjectingAsync(newViewId, currentViewId, _selectedDeviceInformation); isFound = true; } catch (ArgumentException) { // just ignore the exception, the projection is anyway going on isFound = true; } catch (Exception) { // TODO: Unexpected exception handling } } while (!isFound); } } }
private async void PhotosGrid_ItemClick(object sender, ItemClickEventArgs e) { FileInformation photo = (FileInformation)e.ClickedItem; if (projectionWindow == null) { CoreApplicationView newView = CoreApplication.CreateNewView(); int projectionView = 0; await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Frame frame = new Frame(); frame.Navigate(typeof(ProjectedPhoto), photo, new SuppressNavigationTransitionInfo()); Window.Current.Content = frame; Window.Current.Activate(); projectionWindow = Window.Current; projectionView = ApplicationView.GetForCurrentView().Id; }); await ProjectionManager.StartProjectingAsync(projectionView, ApplicationView.GetForCurrentView().Id); } else { await projectionWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { (projectionWindow.Content as Frame).Navigate(typeof(ProjectedPhoto), photo, new SuppressNavigationTransitionInfo()); }); } }
private async void DevicePicker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { // 更新 picker 上設備的 status sender.SetDisplayStatus(args.SelectedDevice, "connecting", DevicePickerDisplayStatusOptions.ShowProgress); // 取得目前選到設備的資訊 activeDevice = args.SelectedDevice; // 現在 view 的 Id 與 CoreDispatcher int currentViewId = ApplicationView.GetForCurrentView().Id; CoreDispatcher currentDispatcher = Window.Current.Dispatcher; // 建立新的 view, if (projectionInstance.ProjectionViewPageControl == null) { await CoreApplication.CreateNewView().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // 建立新 viewe 的生命管理器 projectionInstance.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView(); projectionInstance.MainViewId = currentViewId; var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionPage), projectionInstance); // 這裏的 Window 代表是新建立這個 view 的 Window // 但是要等到呼叫 ProjectionManager.StartProjectingAsync 才會顯示 Window.Current.Content = rootFrame; Window.Current.Activate(); }); } // 直接切換到指定的 view id //bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(projectionInstance.ProjectionViewPageControl.Id); // 通知要使用新的 view projectionInstance.ProjectionViewPageControl.StartViewInUse(); try { txtViewId.Text = $"{projectionInstance.ProjectionViewPageControl.Id}, {currentViewId}"; await ProjectionManager.StartProjectingAsync(projectionInstance.ProjectionViewPageControl.Id, currentViewId, activeDevice); player.Pause(); sender.SetDisplayStatus(args.SelectedDevice, "connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); } catch (Exception ex) { sender.SetDisplayStatus(args.SelectedDevice, ex.Message, DevicePickerDisplayStatusOptions.ShowRetryButton); if (ProjectionManager.ProjectionDisplayAvailable == false) { throw; } } }); }
private async Task SetupSubPage() { await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { _subWindowApplicationView = ApplicationView.GetForCurrentView(); Window.Current.Content = new SubPage(); Window.Current.Activate(); }); await ProjectionManager.StartProjectingAsync(_subWindowApplicationView.Id, ApplicationView.GetForCurrentView().Id); }
private async void StartProjecting(DeviceInformation selectedDisplay) { // If projection is already in progress, then it could be shown on the monitor again // Otherwise, we need to create a new view to show the presentation if (this.ProjectionViewPageControl == null) { // First, create a new, blank view await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // ViewLifetimeControl is a wrapper to make sure the view is closed only // when the app is done with it this.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView(); // Assemble some data necessary for the new page var initData = new ProjectionViewPageInitializationData(); initData.MainDispatcher = thisDispatcher; initData.ProjectionViewPageControl = this.ProjectionViewPageControl; initData.MainViewId = thisViewId; // Display the page in the view. Note that the view will not become visible // until "StartProjectingAsync" is called var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionViewPage), initData); Window.Current.Content = rootFrame; // The call to Window.Current.Activate is required starting in Windos 10. // Without it, the view will never appear. Window.Current.Activate(); }); } try { // Start/StopViewInUse are used to signal that the app is interacting with the // view, so it shouldn't be closed yet, even if the user loses access to it rootPage.ProjectionViewPageControl.StartViewInUse(); // Show the view on a second display that was selected by the user if (selectedDisplay != null) { await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId, selectedDisplay); } else { await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId); } rootPage.ProjectionViewPageControl.StopViewInUse(); } catch (InvalidOperationException) { System.Diagnostics.Debug.WriteLine("Start projection failed"); } }
private async void StartProjecting_Click(object sender, RoutedEventArgs e) { // If projection is already in progress, then it could be shown on the monitor again // Otherwise, we need to create a new view to show the presentation if (rootPage.ProjectionViewPageControl == null) { // First, create a new, blank view var thisDispatcher = Window.Current.Dispatcher; await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // ViewLifetimeControl is a wrapper to make sure the view is closed only // when the app is done with it rootPage.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView(); // Assemble some data necessary for the new page var initData = new ProjectionViewPageInitializationData(); initData.MainDispatcher = thisDispatcher; initData.ProjectionViewPageControl = rootPage.ProjectionViewPageControl; initData.MainViewId = thisViewId; // Display the page in the view. Note that the view will not become visible // until "StartProjectingAsync" is called var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionViewPage), initData); Window.Current.Content = rootFrame; // The call to Window.Current.Activate is required starting in Windos 10. // Without it, the view will never appear. Window.Current.Activate(); }); } try { // Start/StopViewInUse are used to signal that the app is interacting with the // view, so it shouldn't be closed yet, even if the user loses access to it rootPage.ProjectionViewPageControl.StartViewInUse(); // Show the view on a second display (if available) or on the primary display await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId); rootPage.ProjectionViewPageControl.StopViewInUse(); rootPage.NotifyUser("Projection started with success", NotifyType.StatusMessage); } catch (InvalidOperationException) { rootPage.NotifyUser("The projection view is being disposed", NotifyType.ErrorMessage); } }
/// <summary> /// 开始投影 /// </summary> /// <param name="d">投影设备</param> /// <returns></returns> private static bool StartProjection(DeviceInformation d) { IAsyncAction projection = ProjectionManager.StartProjectingAsync(0, 0, d); int count = 5; while (count-- > 0) { Thread.Sleep(count * 1000); if (ProjectionManager.ProjectionDisplayAvailable) { return(true); } } return(false); }
internal static async void Navigate(Type ProjectedSourcePageType, Object object1) { int?secondViewId = null; if (ProjectionManager.ProjectionDisplayAvailable) { if (windows.Count == 0) { int thisViewId; thisViewId = ApplicationView.GetForCurrentView().Id; var thisDispatcher = Window.Current.Dispatcher; await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { secondViewId = ApplicationView.GetForCurrentView().Id; // Display the page in the view. Not visible until “StartProjectionAsync” called Frame ProjectionFrame = new Frame(); ProjectionFrame.Navigate(ProjectedSourcePageType, object1); Window.Current.Content = ProjectionFrame; windows.Add(Window.Current); Window.Current.Activate(); }); // Show the view on a second display if (secondViewId.HasValue) { await ProjectionManager.StartProjectingAsync(secondViewId.Value, thisViewId); } } else { foreach (Window w in windows) { await w.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (w.Visible == true) { Frame f = (Frame)w.Content; f.Navigate(ProjectedSourcePageType, object1); } }); } } } // Read more at https://blogs.windows.com/buildingapps/2015/12/07/optimizing-apps-for-continuum-for-phone/#SZEzLFPFjA9SGHkk.99 }
private async void internalStartProjecting() { try { // If projection is already in progress, then it could be shown on the monitor again // Otherwise, we need to create a new view to show the presentation if (projecting == false && externalViewId == -1) // && Window.Current != null) { mainViewId = ApplicationView.GetForCurrentView().Id; // First, create a new, blank view var thisDispatcher = Window.Current.Dispatcher; await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Assemble some data necessary for the new page mainDispatcher = thisDispatcher; externalViewId = ApplicationView.GetForCurrentView().Id; // Display the page in the view. Note that the view will not become visible // until "StartProjectingAsync" is called var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionPage)); Window.Current.Content = rootFrame; // The call to Window.Current.Activate is required starting in Windos 10. // Without it, the view will never appear. Window.Current.Activate(); }); } projecting = true; // Start/StopViewInUse are used to signal that the app is interacting with the // view, so it shouldn't be closed yet, even if the user loses access to it if (projecting) { // Show the view on a second display (if available) or on the primary display await ProjectionManager.StartProjectingAsync(externalViewId, mainViewId); } } catch (Exception ex) { Debug.WriteLine("StartProjection: " + ex.Message); } }
private async Task <bool> TryProjectionManagerCastAsync(DeviceInformation device) { bool projectionManagerCastAsyncSucceeded = false; if ((activeDevice == null && ProjectionManager.ProjectionDisplayAvailable && device == null) || device != null) { thisViewId = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Id; // If projection is already in progress, then it could be shown on the monitor again // Otherwise, we need to create a new view to show the presentation if (rootPage.ProjectionViewPageControl == null) { // First, create a new, blank view var thisDispatcher = Window.Current.Dispatcher; await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // ViewLifetimeControl is a wrapper to make sure the view is closed only // when the app is done with it rootPage.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView(); // Assemble some data necessary for the new page pvb.MainPageDispatcher = thisDispatcher; pvb.ProjectionViewPageControl = rootPage.ProjectionViewPageControl; pvb.MainViewId = thisViewId; // Display the page in the view. Note that the view will not become visible // until "StartProjectingAsync" is called var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionViewPage), pvb); Window.Current.Content = rootFrame; Window.Current.Activate(); }); } try { // Start/StopViewInUse are used to signal that the app is interacting with the // view, so it shouldn't be closed yet, even if the user loses access to it rootPage.ProjectionViewPageControl.StartViewInUse(); try { //if (ProjectionManager.ProjectionDisplayAvailable) //{ // // Show the view on a second display (if available) or on the primary display // await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId); //} //else //{ await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId, device); //} } catch (Exception ex) { if (!ProjectionManager.ProjectionDisplayAvailable) { throw ex; } } if (pvb.ProjectedPage != null) { this.player.Pause(); await pvb.ProjectedPage.SetMediaSource(this.player.Source, this.player.Position); } if (device != null) { activeDevice = device; activeCastConnectionHandler = pvb; } projectionManagerCastAsyncSucceeded = true; } catch (Exception) { rootPage.NotifyUser("The projection view is being disposed", NotifyType.ErrorMessage); } ApplicationView.GetForCurrentView().ExitFullScreenMode(); } return(projectionManagerCastAsyncSucceeded); }
private async void Picker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { // Set status to Connecting picker.SetDisplayStatus(args.SelectedDevice, "Connecting", DevicePickerDisplayStatusOptions.ShowProgress); // Getting the selected device improves debugging DeviceInformation selectedDevice = args.SelectedDevice; thisViewId = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Id; // If projection is already in progress, then it could be shown on the monitor again // Otherwise, we need to create a new view to show the presentation if (rootPage.ProjectionViewPageControl == null) { // First, create a new, blank view var thisDispatcher = Window.Current.Dispatcher; await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // ViewLifetimeControl is a wrapper to make sure the view is closed only // when the app is done with it rootPage.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView(); // Assemble some data necessary for the new page pvb.MainPageDispatcher = thisDispatcher; pvb.ProjectionViewPageControl = rootPage.ProjectionViewPageControl; pvb.MainViewId = thisViewId; // Display the page in the view. Note that the view will not become visible // until "StartProjectingAsync" is called var rootFrame = new Frame(); rootFrame.Navigate(typeof(ProjectionViewPage), pvb); Window.Current.Content = rootFrame; Window.Current.Activate(); }); } try { // Start/StopViewInUse are used to signal that the app is interacting with the // view, so it shouldn't be closed yet, even if the user loses access to it rootPage.ProjectionViewPageControl.StartViewInUse(); try { await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId, selectedDevice); } catch (Exception ex) { if (!ProjectionManager.ProjectionDisplayAvailable || pvb.ProjectedPage == null) { throw ex; } } // ProjectionManager currently can throw an exception even when projection has started.\ // Re-throw the exception when projection has not been started after calling StartProjectingAsync if (ProjectionManager.ProjectionDisplayAvailable && pvb.ProjectedPage != null) { this.player.Pause(); await pvb.ProjectedPage.SetMediaSource(this.player.Source, this.player.Position); activeDevice = selectedDevice; // Set status to Connected picker.SetDisplayStatus(args.SelectedDevice, "Connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); picker.Hide(); } else { rootPage.NotifyUser(string.Format("Projection has failed to '{0}'", selectedDevice.Name), NotifyType.ErrorMessage); // Set status to Failed picker.SetDisplayStatus(args.SelectedDevice, "Connection Failed", DevicePickerDisplayStatusOptions.ShowRetryButton); } } catch (Exception) { rootPage.NotifyUser(string.Format("Projection has failed to '{0}'", selectedDevice.Name), NotifyType.ErrorMessage); // Set status to Failed try { picker.SetDisplayStatus(args.SelectedDevice, "Connection Failed", DevicePickerDisplayStatusOptions.ShowRetryButton); } catch { } } } catch (Exception ex) { UnhandledExceptionPage.ShowUnhandledException(ex); } }); }