-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathApplicationExtensions.cs
executable file
·148 lines (129 loc) · 5.28 KB
/
ApplicationExtensions.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
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System;
using System.Windows;
using System.ComponentModel;
using System.Linq;
using Microsoft.Phone.Controls;
namespace TelegramClient.Extensions
{
public enum Theme
{
Light,
Dark
}
public static class ApplicationExtensions
{
private static readonly string PhoneLightThemeVisibility = "PhoneLightThemeVisibility";
private static bool _listeningToNavEvents;
private static bool _isNavigating;
public static Theme GetTheme(this Application application)
{
var visibility = (Visibility)Application.Current.Resources[PhoneLightThemeVisibility];
return (visibility == Visibility.Visible) ? Theme.Light : Theme.Dark;
}
public static bool IsDesignTime(this Application application)
{
return DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual);
}
public static void GoBack(this Application application)
{
var frame = application.RootVisual as PhoneApplicationFrame;
EnsureListeningToNaviation(frame);
if (frame == null)
return;
else if (_isNavigating)
return;
else if (frame.CanGoBack)
frame.GoBack();
}
public static void GoForward(this Application application)
{
var frame = application.RootVisual as PhoneApplicationFrame;
EnsureListeningToNaviation(frame);
if (frame == null)
return;
else if (_isNavigating)
return;
else if (frame.CanGoForward)
frame.GoForward();
}
public static void Navigate(this Application application, Uri uri)
{
var frame = application.RootVisual as PhoneApplicationFrame;
EnsureListeningToNaviation(frame);
//System.Diagnostics.Debug.WriteLine(string.Format("Navigating in App Extension. Frame Content: {0}, Current Source: {1}, Target: {2}, IsNavigating: {3}", frame.Content.GetType().ToString(), frame.CurrentSource, uri, _isNavigating));
if (frame == null)
return;
if (uri == null)
return;
if (uri.OriginalString == frame.CurrentSource.OriginalString)
return;
if (_isNavigating)
{
//System.Diagnostics.Debug.WriteLine("Cancelling nav b/c we're currently navigating");
return;
}
else
{
//System.Diagnostics.Debug.WriteLine("Calling frame.nav");
frame.Navigate(uri);
}
}
public static void Navigate(this Application application, string uri)
{
Navigate(application, new Uri(uri, UriKind.Relative));
}
public static void Navigate(this Application application, string uri, object context)
{
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = context;
Navigate(application, new Uri(uri, UriKind.Relative));
}
public static void Navigate(this Application application, Uri uri, object context)
{
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = context;
Navigate(application, uri);
}
private static void EnsureListeningToNaviation(PhoneApplicationFrame frame)
{
if (!_listeningToNavEvents)
{
frame.Navigating += (sender, e) => _isNavigating = true;
frame.Navigated += (sender, e) => _isNavigating = false;
frame.NavigationStopped += (sender, e) => _isNavigating = false;
frame.NavigationFailed += (sender, e) => _isNavigating = false;
_listeningToNavEvents = true;
}
}
public static PhoneApplicationPage GetActivePage(this Application application)
{
PhoneApplicationPage content = null;
if (application != null)
{
PhoneApplicationFrame rootVisual = application.RootVisual as PhoneApplicationFrame;
if (rootVisual != null)
{
content = rootVisual.Content as PhoneApplicationPage;
}
}
return content;
}
/// <summary>
/// Returns the first instance of a type implementing T from the Application's
/// ApplicationLifetimeObjects list, or null if one cannot be found.
/// </summary>
/// <typeparam name="T">The service's type.</typeparam>
/// <param name="application"></param>
public static T GetService<T>(this Application application)
{
return application.ApplicationLifetimeObjects.OfType<T>().FirstOrDefault();
}
}
}