forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenario2.xaml.cs
180 lines (156 loc) · 7.94 KB
/
Scenario2.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using SDKTemplate;
using SecondaryViewsHelpers;
using System;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SDKSample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Scenario2:Page
{
MainPage rootPage = MainPage.Current;
CoreDispatcher dispatcher;
int thisViewId;
public Scenario2()
{
InitializeComponent();
thisViewId = ApplicationView.GetForCurrentView().Id;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ProjectionManager.ProjectionDisplayAvailableChanged -= ProjectionManager_ProjectionDisplayAvailableChanged;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// An app can query if a second display is available...
UpdateTextBlock(ProjectionManager.ProjectionDisplayAvailable);
// ...or listen for when a display is attached or detached
ProjectionManager.ProjectionDisplayAvailableChanged += ProjectionManager_ProjectionDisplayAvailableChanged;
dispatcher = Window.Current.Dispatcher;
}
private async void ProjectionManager_ProjectionDisplayAvailableChanged(object sender, object e)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
UpdateTextBlock(ProjectionManager.ProjectionDisplayAvailable);
});
}
private void UpdateTextBlock(bool screenAvailable)
{
if (screenAvailable)
{
this.displayAvailableStatus.Text = "Display is available";
this.startprojecting_button.Visibility = Visibility.Visible;
this.requestandstartprojecting_button.Visibility = Visibility.Collapsed;
}
else
{
this.displayAvailableStatus.Text = "Display is not available, select one:";
this.startprojecting_button.Visibility = Visibility.Collapsed;
this.requestandstartprojecting_button.Visibility = Visibility.Visible;
}
}
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
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 = dispatcher;
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);
}
}
private async void RequestAndStartProjecting_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;
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
Rect pickerLocation = new Rect(470.0, 0.0, 200.0, 300.0);
await ProjectionManager.RequestStartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId, pickerLocation, Windows.UI.Popups.Placement.Above);
rootPage.ProjectionViewPageControl.StopViewInUse();
rootPage.NotifyUser("Projection started with success", NotifyType.StatusMessage);
}
catch (InvalidOperationException)
{
rootPage.NotifyUser("The projection view is being disposed", NotifyType.ErrorMessage);
}
}
}
}