|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 6 | +using JohnBauerPictureViewer.Classes; |
| 7 | +using Windows.ApplicationModel; |
| 8 | +using Windows.ApplicationModel.Activation; |
| 9 | +using Windows.Foundation; |
| 10 | +using Windows.Foundation.Collections; |
| 11 | +using Windows.UI.Xaml; |
| 12 | +using Windows.UI.Xaml.Controls; |
| 13 | +using Windows.UI.Xaml.Controls.Primitives; |
| 14 | +using Windows.UI.Xaml.Data; |
| 15 | +using Windows.UI.Xaml.Input; |
| 16 | +using Windows.UI.Xaml.Media; |
| 17 | +using Windows.UI.Xaml.Navigation; |
| 18 | +using Spree.Classes; |
| 19 | +using Windows.UI.Xaml.Media.Imaging; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Windows.Storage; |
| 22 | +using Windows.Storage.Streams; |
| 23 | + |
| 24 | +namespace JohnBauerPictureViewer |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Provides application-specific behavior to supplement the default Application class. |
| 28 | + /// </summary> |
| 29 | + sealed partial class App : Application |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Initializes the singleton application object. This is the first line of authored code |
| 33 | + /// executed, and as such is the logical equivalent of main() or WinMain(). |
| 34 | + /// </summary> |
| 35 | + public App() |
| 36 | + { |
| 37 | + this.InitializeComponent(); |
| 38 | + this.Suspending += OnSuspending; |
| 39 | + } |
| 40 | + |
| 41 | + public static ImageFileData ApplicationData { get; internal set; } |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Invoked when the application is launched normally by the end user. Other entry points |
| 47 | + /// will be used such as when the application is launched to open a specific file. |
| 48 | + /// </summary> |
| 49 | + /// <param name="e">Details about the launch request and process.</param> |
| 50 | + protected override void OnLaunched(LaunchActivatedEventArgs e) |
| 51 | + { |
| 52 | +#if DEBUG |
| 53 | + if (System.Diagnostics.Debugger.IsAttached) |
| 54 | + { |
| 55 | + this.DebugSettings.EnableFrameRateCounter = true; |
| 56 | + } |
| 57 | +#endif |
| 58 | + Frame rootFrame = Window.Current.Content as Frame; |
| 59 | + |
| 60 | + // Do not repeat app initialization when the Window already has content, |
| 61 | + // just ensure that the window is active |
| 62 | + if (rootFrame == null) |
| 63 | + { |
| 64 | + // Create a Frame to act as the navigation context and navigate to the first page |
| 65 | + rootFrame = new Frame(); |
| 66 | + |
| 67 | + rootFrame.NavigationFailed += OnNavigationFailed; |
| 68 | + |
| 69 | + if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) |
| 70 | + { |
| 71 | + //TODO: Load state from previously suspended application |
| 72 | + } |
| 73 | + |
| 74 | + // Place the frame in the current Window |
| 75 | + Window.Current.Content = rootFrame; |
| 76 | + } |
| 77 | + |
| 78 | + if (e.PrelaunchActivated == false) |
| 79 | + { |
| 80 | + if (rootFrame.Content == null) |
| 81 | + { |
| 82 | + // When the navigation stack isn't restored navigate to the first page, |
| 83 | + // configuring the new page by passing required information as a navigation |
| 84 | + // parameter |
| 85 | + rootFrame.Navigate(typeof(MainPage), e.Arguments); |
| 86 | + } |
| 87 | + // Ensure the current window is active |
| 88 | + Window.Current.Activate(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Invoked when Navigation to a certain page fails |
| 94 | + /// </summary> |
| 95 | + /// <param name="sender">The Frame which failed navigation</param> |
| 96 | + /// <param name="e">Details about the navigation failure</param> |
| 97 | + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) |
| 98 | + { |
| 99 | + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Invoked when application execution is being suspended. Application state is saved |
| 104 | + /// without knowing whether the application will be terminated or resumed with the contents |
| 105 | + /// of memory still intact. |
| 106 | + /// </summary> |
| 107 | + /// <param name="sender">The source of the suspend request.</param> |
| 108 | + /// <param name="e">Details about the suspend request.</param> |
| 109 | + private void OnSuspending(object sender, SuspendingEventArgs e) |
| 110 | + { |
| 111 | + var deferral = e.SuspendingOperation.GetDeferral(); |
| 112 | + //TODO: Save application state and stop any background activity |
| 113 | + deferral.Complete(); |
| 114 | + } |
| 115 | + |
| 116 | + private static Settings _Settings; |
| 117 | + public static Settings Settings { get { if (_Settings == null) { _Settings = new Settings(); } return _Settings; } } |
| 118 | + |
| 119 | + public static StorageFolder PictureStorageFolder { get { return KnownFolders.PicturesLibrary; } } |
| 120 | + public static StorageFolder VideoStorageFolder { get { return KnownFolders.VideosLibrary; } } |
| 121 | + public static StorageFolder MusicStorageFolder { get { return KnownFolders.MusicLibrary; } } |
| 122 | + |
| 123 | + public static string WatchoutIP { get; set; } |
| 124 | + public static string WatchoutPort { get; set; } |
| 125 | + public static string Show1 { get; set; } |
| 126 | + public static string Show2 { get; set; } |
| 127 | + public static string Que1 { get; set; } |
| 128 | + public static string Que2 { get; set; } |
| 129 | + public static string Que3 { get; set; } |
| 130 | + public static int PauseMilliseconds { get; set; } |
| 131 | + public static int NumberOfPicturesToSearchFor { get { return 5; } } |
| 132 | + |
| 133 | + private static async Task<bool> isFilePresent(StorageFolder storageFolder, string fileName) |
| 134 | + { |
| 135 | + try |
| 136 | + { |
| 137 | + var item = await storageFolder.TryGetItemAsync(fileName); |
| 138 | + if (item != null) |
| 139 | + { |
| 140 | + var prop = await item.GetBasicPropertiesAsync(); |
| 141 | + var size = prop.Size; |
| 142 | + |
| 143 | + return size > 0; |
| 144 | + } |
| 145 | + } |
| 146 | + catch (Exception E) |
| 147 | + { |
| 148 | + |
| 149 | + } |
| 150 | + return false; |
| 151 | + } |
| 152 | + private static async Task<BitmapImage> StorageFileToBitmapImage(StorageFile savedStorageFile)//, RoutedEventHandler bitmapImage_ImageOpened) |
| 153 | + { |
| 154 | + using (IRandomAccessStream fileStream = await savedStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) |
| 155 | + { |
| 156 | + BitmapImage bitmapImage = new BitmapImage(); |
| 157 | + //bitmapImage.DecodePixelHeight = 100; |
| 158 | + //bitmapImage.DecodePixelWidth = 100; |
| 159 | + //bitmapImage.ImageOpened += bitmapImage_ImageOpened; |
| 160 | + |
| 161 | + await bitmapImage.SetSourceAsync(fileStream); |
| 162 | + |
| 163 | + |
| 164 | + return bitmapImage; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + public static async Task<BitmapImage> GetBitmapImage(string FileName)//, RoutedEventHandler bitmapImage_ImageOpened) |
| 170 | + { |
| 171 | + StorageFolder storageFolder = PictureStorageFolder; |
| 172 | + |
| 173 | + if (await isFilePresent(storageFolder, FileName)) |
| 174 | + { |
| 175 | + StorageFile file = await storageFolder.GetFileAsync(FileName); |
| 176 | + |
| 177 | + BitmapImage B = await StorageFileToBitmapImage(file);//, bitmapImage_ImageOpened); |
| 178 | + |
| 179 | + return B; |
| 180 | + } |
| 181 | + return null; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments