33
|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Windows; |
|
6 |
using System.Windows.Controls; |
|
7 |
using System.Windows.Data; |
|
8 |
using System.Windows.Documents; |
|
9 |
using System.Windows.Input; |
|
10 |
using System.Windows.Media; |
|
11 |
using System.Windows.Media.Imaging; |
|
12 |
using System.Windows.Shapes; |
|
13 |
using System.Windows.Threading; |
|
14 |
using Microsoft.Surface; |
|
15 |
using Microsoft.Surface.Presentation; |
|
16 |
using Microsoft.Surface.Presentation.Controls; |
|
17 |
using System.Windows.Controls.Primitives; |
|
18 |
|
|
19 |
namespace SliderTest |
|
20 |
{ |
|
21 |
/// <summary> |
|
22 |
/// Interaction logic for SurfaceWindow1.xaml |
|
23 |
/// </summary> |
|
24 |
public partial class SurfaceWindow1 : SurfaceWindow |
|
25 |
{ |
|
26 |
DispatcherTimer timer; |
|
27 |
bool isDragging=false; |
|
28 |
bool finishedDragging = false; |
|
29 |
|
|
30 |
/// <summary> |
|
31 |
/// Default constructor. |
|
32 |
/// </summary> |
|
33 |
public SurfaceWindow1() |
|
34 |
{ |
|
35 |
InitializeComponent(); |
|
36 |
initmedia(); |
|
37 |
// Add handlers for Application activation events |
|
38 |
AddActivationHandlers(); |
|
39 |
} |
|
40 |
|
|
41 |
/// <summary> |
|
42 |
/// Occurs when the window is about to close. |
|
43 |
/// </summary> |
|
44 |
/// <param name="e"></param> |
|
45 |
protected override void OnClosed(EventArgs e) |
|
46 |
{ |
|
47 |
base.OnClosed(e); |
|
48 |
|
|
49 |
// Remove handlers for Application activation events |
|
50 |
RemoveActivationHandlers(); |
|
51 |
} |
|
52 |
|
|
53 |
/// <summary> |
|
54 |
/// Adds handlers for Application activation events. |
|
55 |
/// </summary> |
|
56 |
private void AddActivationHandlers() |
|
57 |
{ |
|
58 |
// Subscribe to surface application activation events |
|
59 |
ApplicationLauncher.ApplicationActivated += OnApplicationActivated; |
|
60 |
ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed; |
|
61 |
ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated; |
|
62 |
} |
|
63 |
|
|
64 |
/// <summary> |
|
65 |
/// Removes handlers for Application activation events. |
|
66 |
/// </summary> |
|
67 |
private void RemoveActivationHandlers() |
|
68 |
{ |
|
69 |
// Unsubscribe from surface application activation events |
|
70 |
ApplicationLauncher.ApplicationActivated -= OnApplicationActivated; |
|
71 |
ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed; |
|
72 |
ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated; |
|
73 |
} |
|
74 |
|
|
75 |
/// <summary> |
|
76 |
/// This is called when application has been activated. |
|
77 |
/// </summary> |
|
78 |
/// <param name="sender"></param> |
|
79 |
/// <param name="e"></param> |
|
80 |
private void OnApplicationActivated(object sender, EventArgs e) |
|
81 |
{ |
|
82 |
//TODO: enable audio, animations here |
|
83 |
} |
|
84 |
|
|
85 |
/// <summary> |
|
86 |
/// This is called when application is in preview mode. |
|
87 |
/// </summary> |
|
88 |
/// <param name="sender"></param> |
|
89 |
/// <param name="e"></param> |
|
90 |
private void OnApplicationPreviewed(object sender, EventArgs e) |
|
91 |
{ |
|
92 |
//TODO: Disable audio here if it is enabled |
|
93 |
|
|
94 |
//TODO: optionally enable animations here |
|
95 |
} |
|
96 |
|
|
97 |
/// <summary> |
|
98 |
/// This is called when application has been deactivated. |
|
99 |
/// </summary> |
|
100 |
/// <param name="sender"></param> |
|
101 |
/// <param name="e"></param> |
|
102 |
private void OnApplicationDeactivated(object sender, EventArgs e) |
|
103 |
{ |
|
104 |
//TODO: disable audio, animations here |
|
105 |
} |
|
106 |
|
|
107 |
#region Media |
|
108 |
|
|
109 |
private void initmedia() |
|
110 |
{ |
|
111 |
media.UnloadedBehavior = MediaState.Manual; |
|
112 |
media.LoadedBehavior = MediaState.Manual; |
|
113 |
media.ScrubbingEnabled = true; |
|
114 |
} |
|
115 |
|
|
116 |
private void media_MediaOpened(object sender, RoutedEventArgs e) |
|
117 |
{ |
|
118 |
initslider(); |
|
119 |
timer.Start(); |
|
120 |
} |
|
121 |
#endregion |
|
122 |
|
|
123 |
#region Timer |
|
124 |
private void initTimer() |
|
125 |
{ |
|
126 |
timer = new DispatcherTimer(); |
|
127 |
timer.Interval = new TimeSpan(0, 0, 0, 0, 100); |
|
128 |
timer.Tick += new EventHandler(timer_Tick); |
|
129 |
} |
|
130 |
|
|
131 |
void timer_Tick(object sender, EventArgs e) |
|
132 |
{ |
|
133 |
if (!isDragging) |
|
134 |
{ |
|
135 |
slider.Value = media.Position.TotalMilliseconds; |
|
136 |
} |
|
137 |
if (finishedDragging) |
|
138 |
{ |
|
139 |
int SliderValue = (int)slider.Value; |
|
140 |
TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue); |
|
141 |
media.Position = ts; |
|
142 |
media.Play(); |
|
143 |
isDragging = false; |
|
144 |
finishedDragging = false; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
#endregion |
|
149 |
|
|
150 |
#region Slider |
|
151 |
|
|
152 |
private void initslider() |
|
153 |
{ |
|
154 |
slider.Maximum = media.NaturalDuration.TimeSpan.TotalMilliseconds; |
|
155 |
} |
|
156 |
|
|
157 |
//works ^^ |
|
158 |
private void sliderPosition_DragStarted( |
|
159 |
object sender, DragStartedEventArgs e) |
|
160 |
{ |
|
161 |
isDragging = true; |
|
162 |
media.Pause(); |
|
163 |
} |
|
164 |
//works ^^ |
|
165 |
private void sliderPosition_DragCompleted( |
|
166 |
object sender, DragCompletedEventArgs e) |
|
167 |
{ |
|
168 |
finishedDragging = true; |
|
169 |
} |
|
170 |
#endregion |
|
171 |
|
|
172 |
|
|
173 |
private void buttonPlay_Click(object sender, RoutedEventArgs e) |
|
174 |
{ |
|
175 |
media.Play(); |
|
176 |
} |
|
177 |
|
|
178 |
private void SurfaceWindow_Activated(object sender, EventArgs e) |
|
179 |
{ |
|
180 |
initTimer(); |
|
181 |
} |
|
182 |
|
|
183 |
|
|
184 |
private void surfacebuttonPlay_ContactDown(object sender, ContactEventArgs e) |
|
185 |
{ |
|
186 |
media.Play(); |
|
187 |
} |
|
188 |
private void surfacebuttonPlay_Click(object sender, RoutedEventArgs e) |
|
189 |
{ |
|
190 |
media.Play(); |
|
191 |
} |
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
} |
|
196 |
} |