Extend WP8 Splash Screen to respect SplashScreen and SplashScreenDelay preferences from config file

This commit is contained in:
Georgi Alexandrov 2015-03-05 15:56:44 +02:00
parent d2e62a551c
commit 688b138e31

View File

@ -40,26 +40,35 @@ namespace WPCordovaClassLib.Cordova.Commands
/// </summary> /// </summary>
public class SplashScreen : BaseCommand public class SplashScreen : BaseCommand
{ {
private readonly Preferences preferences = new Preferences();
private Popup popup; private Popup popup;
private bool autohide = true;
private static bool WasShown = false; private static bool WasShown = false;
public SplashScreen() public SplashScreen()
{ {
Image SplashScreen = new Image(); this.LoadPreferencesFromConfiguration();
BitmapImage splash_image = new BitmapImage();
splash_image.SetSource(Application.GetResourceStream(new Uri(@"SplashScreenImage.jpg", UriKind.Relative)).Stream); Image SplashScreen = new Image()
SplashScreen.Source = splash_image; {
Height = Application.Current.Host.Content.ActualHeight,
Width = Application.Current.Host.Content.ActualWidth,
Stretch = Stretch.Fill
};
var imageResource = Application.GetResourceStream(preferences.SplashScreenImage);
if (imageResource != null)
{
BitmapImage splash_image = new BitmapImage();
splash_image.SetSource(imageResource.Stream);
SplashScreen.Source = splash_image;
}
// Instansiate the popup and set the Child property of Popup to SplashScreen // Instansiate the popup and set the Child property of Popup to SplashScreen
popup = new Popup() {IsOpen = false, Child = SplashScreen }; this.popup = new Popup() { IsOpen = false, Child = SplashScreen };
// Orient the popup accordingly // Orient the popup accordingly
popup.HorizontalAlignment = HorizontalAlignment.Stretch; this.popup.HorizontalAlignment = HorizontalAlignment.Stretch;
popup.VerticalAlignment = VerticalAlignment.Center; this.popup.VerticalAlignment = VerticalAlignment.Center;
LoadConfigValues();
} }
public override void OnInit() public override void OnInit()
@ -68,27 +77,52 @@ namespace WPCordovaClassLib.Cordova.Commands
if (!WasShown) if (!WasShown)
{ {
WasShown = true; WasShown = true;
show(); this.show();
} }
} }
void LoadConfigValues() private static void GetPreference(XDocument document, string preferenceName, Action<string> action)
{
var attribute = from results in document.Descendants()
where (string)results.Attribute("name") == preferenceName
select (string)results.Attribute("value");
var value = attribute.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(value))
{
action(value);
}
}
private void LoadPreferencesFromConfiguration()
{ {
StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative)); StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
if (streamInfo != null) if (streamInfo != null)
{ {
StreamReader sr = new StreamReader(streamInfo.Stream); using (StreamReader sr = new StreamReader(streamInfo.Stream))
//This will Read Keys Collection for the xml file
XDocument document = XDocument.Parse(sr.ReadToEnd());
var preferences = from results in document.Descendants()
where (string)results.Attribute("name") == "AutoHideSplashScreen"
select (string)results.Attribute("value") == "true";
if (preferences.Count() > 0 && preferences.First() == false)
{ {
autohide = false; //This will Read Keys Collection for the xml file
XDocument document = XDocument.Parse(sr.ReadToEnd());
GetPreference(document, "AutoHideSplashScreen", value =>
{
var autoHideSplashScreen = false;
if (bool.TryParse(value, out autoHideSplashScreen))
{
preferences.AutoHideSplashScreen = autoHideSplashScreen;
}
});
GetPreference(document, "SplashScreenDelay", value =>
{
var splashScreenDelay = 0;
if (int.TryParse(value, out splashScreenDelay))
{
preferences.SplashScreenDelay = splashScreenDelay;
}
});
GetPreference(document, "SplashScreen", value => preferences.SplashScreenImage = new Uri(value, UriKind.Relative));
} }
} }
} }
@ -97,12 +131,12 @@ namespace WPCordovaClassLib.Cordova.Commands
{ {
Deployment.Current.Dispatcher.BeginInvoke(() => Deployment.Current.Dispatcher.BeginInvoke(() =>
{ {
if (popup.IsOpen) if (this.popup.IsOpen)
{ {
return; return;
} }
popup.Child.Opacity = 0; this.popup.Child.Opacity = 0;
Storyboard story = new Storyboard(); Storyboard story = new Storyboard();
DoubleAnimation animation; DoubleAnimation animation;
@ -119,22 +153,15 @@ namespace WPCordovaClassLib.Cordova.Commands
story.Begin(); story.Begin();
popup.IsOpen = true; this.popup.IsOpen = true;
if (autohide) if (this.preferences.AutoHideSplashScreen)
{ {
DispatcherTimer timer = new DispatcherTimer(); this.StartAutoHideTimer();
timer.Tick += (object sender, EventArgs e) =>
{
hide();
};
timer.Interval = TimeSpan.FromSeconds(1.2);
timer.Start();
} }
}); });
} }
public void hide(string options = null) public void hide(string options = null)
{ {
Deployment.Current.Dispatcher.BeginInvoke(() => Deployment.Current.Dispatcher.BeginInvoke(() =>
@ -163,5 +190,30 @@ namespace WPCordovaClassLib.Cordova.Commands
story.Begin(); story.Begin();
}); });
} }
private void StartAutoHideTimer()
{
var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(preferences.SplashScreenDelay) };
timer.Tick += (object sender, EventArgs e) =>
{
this.hide();
timer.Stop();
};
timer.Start();
}
private class Preferences
{
public bool AutoHideSplashScreen { get; set; }
public Uri SplashScreenImage { get; set; }
public int SplashScreenDelay { get; set; }
public Preferences()
{
this.SplashScreenDelay = 3000;
this.AutoHideSplashScreen = true;
this.SplashScreenImage = new Uri("SplashScreenImage.jpg", UriKind.Relative);
}
}
} }
} }