diff --git a/Source/Windows10/Prism.Windows/Navigation/NavigationServiceRegistry.cs b/Source/Windows10/Prism.Windows/Navigation/NavigationServiceRegistry.cs deleted file mode 100644 index f29793f5b..000000000 --- a/Source/Windows10/Prism.Windows/Navigation/NavigationServiceRegistry.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel; -using Prism.Ioc; -using Prism.Services; -using Windows.UI.Core; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; - -namespace Prism.Navigation -{ - public static class NavigationServiceRegistry - { - private static Dictionary _instances { get; } = new Dictionary(); - - /// - /// Creates a navigation service - /// - /// Optional default getures tied to this Frame - /// INavigationService - [EditorBrowsable(EditorBrowsableState.Never)] - public static INavigationService CreateNavigationService(this IContainerProvider container, params Gesture[] gestures) - { - return CreateNavigationService(container, null, Window.Current.CoreWindow, gestures); - } - - /// - /// Creates a navigation service - /// - /// Required XAML Frame - /// Optional default getures tied to this Frame - /// INavigationService - [EditorBrowsable(EditorBrowsableState.Never)] - public static INavigationService CreateNavigationService(this IContainerProvider container, Frame frame, params Gesture[] gestures) - { - return CreateNavigationService(container, frame, Window.Current.CoreWindow, gestures); - } - - /// - /// Creates a navigation service - /// - /// Optional default getures tied to this Frame - /// INavigationService - [EditorBrowsable(EditorBrowsableState.Never)] - public static INavigationService CreateNavigationService(this IContainerProvider container, CoreWindow window, params Gesture[] gestures) - { - return CreateNavigationService(container, null, window, gestures); - } - - /// - /// Creates a navigation service - /// - /// Required XAML Frame - /// Optional default getures tied to this Frame - /// INavigationService - [EditorBrowsable(EditorBrowsableState.Never)] - public static INavigationService CreateNavigationService(this IContainerProvider container, Frame frame, CoreWindow window, params Gesture[] gestures) - { - if (frame is null) - frame = new Frame(); - - if (_instances.ContainsKey(frame)) - return _instances[frame]; - - var gesture_service = GestureServiceRegistry.GetForCurrentView(window); - var navigation_service = container.Resolve(PrismApplicationBase.NavigationServiceParameterName, (typeof(Frame), frame)); - foreach (var gesture in gestures) - { - switch (gesture) - { - case Gesture.Back: - gesture_service.BackRequested += async (s, e) => await navigation_service.GoBackAsync(); - break; - case Gesture.Forward: - gesture_service.ForwardRequested += async (s, e) => await navigation_service.GoForwardAsync(default(INavigationParameters)); - break; - case Gesture.Refresh: - gesture_service.RefreshRequested += async (s, e) => await navigation_service.RefreshAsync(); - break; - } - } - - _instances.Add(frame, navigation_service); - return navigation_service; - } - - [EditorBrowsable(EditorBrowsableState.Never)] - public static void ClearCache() => _instances.Clear(); - } -} diff --git a/Source/Windows10/Prism.Windows/PrismApplicationBase.core.cs b/Source/Windows10/Prism.Windows/PrismApplicationBase.cs similarity index 54% rename from Source/Windows10/Prism.Windows/PrismApplicationBase.core.cs rename to Source/Windows10/Prism.Windows/PrismApplicationBase.cs index 5cc14273d..b50c1203c 100644 --- a/Source/Windows10/Prism.Windows/PrismApplicationBase.core.cs +++ b/Source/Windows10/Prism.Windows/PrismApplicationBase.cs @@ -1,37 +1,36 @@ -using Prism.Events; -using Prism.Ioc; -using Prism.Logging; -using Prism.Navigation; -using Prism.Mvvm; -using System; -using System.Linq; +using System; +using System.ComponentModel; using System.Diagnostics; +using System.Linq; using System.Threading; using System.Threading.Tasks; -using Windows.UI.Xaml; +using Prism.Events; +using Prism.Ioc; +using Prism.Logging; +using Prism.Modularity; +using Prism.Mvvm; +using Prism.Navigation; using Prism.Services; using Windows.ApplicationModel.Activation; +using Windows.Foundation; using Windows.Storage; -using Prism.Modularity; +using Windows.UI.Core; +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Prism { - public abstract partial class PrismApplicationBase + public abstract class PrismApplicationBase : Application, IPrismApplicationEvents { + static int _initialized = 0; public new static PrismApplicationBase Current => (PrismApplicationBase)Application.Current; private static readonly SemaphoreSlim _startSemaphore = new SemaphoreSlim(1, 1); public const string NavigationServiceParameterName = "navigationService"; - private readonly bool _logStartingEvents = false; public PrismApplicationBase() { InternalInitialize(); - _logger.Log("[App.Constructor()]", Category.Info, Priority.None); - (this as IPrismApplicationEvents).WindowCreated += (s, e) => - { - Container.SetupForCurrentView(e.Window.CoreWindow); - }; + base.Suspending += async (s, e) => { if (ApplicationData.Current.LocalSettings.Values.ContainsKey("Suspend_Data")) @@ -63,25 +62,14 @@ public PrismApplicationBase() private void InternalInitialize() { - // don't forget there is no logger yet - if (_logStartingEvents) - { - _logger.Log($"{nameof(PrismApplicationBase)}.{nameof(InternalInitialize)}", Category.Info, Priority.None); - } - // dependecy injection _containerExtension = CreateContainerExtension(); RegisterRequiredTypes(_containerExtension); - Debug.WriteLine("[App.RegisterTypes()]"); RegisterTypes(_containerExtension); - Debug.WriteLine("Dependency container has just been finalized."); _containerExtension.FinalizeExtension(); - // now we can start logging instead of debug/write - _logger = Container.Resolve(); - // finalize the application ConfigureViewModelLocator(); @@ -89,23 +77,14 @@ private void InternalInitialize() InitializeModules(); } - static int _initialized = 0; - private ILoggerFacade _logger; private void CallOnInitializedOnce() { - // don't forget there is no logger yet - if (_logStartingEvents) - { - _logger.Log($"{nameof(PrismApplicationBase)}.{nameof(CallOnInitializedOnce)}", Category.Info, Priority.None); - } - // once and only once, ever if (Interlocked.Increment(ref _initialized) == 1) { - NavigationService = Container.CreateNavigationService(SupportedNavigationGestures()); + NavigationService = CreateNavigationService(null, null, SupportedNavigationGestures()); - _logger.Log("[App.OnInitialize()]", Category.Info, Priority.None); OnInitialized(); } } @@ -113,25 +92,18 @@ private void CallOnInitializedOnce() private async Task InternalStartAsync(StartArgs startArgs) { await _startSemaphore.WaitAsync(); - if (_logStartingEvents) - { - _logger.Log($"{nameof(PrismApplicationBase)}.{nameof(InternalStartAsync)}({startArgs})", Category.Info, Priority.None); - } try { CallOnInitializedOnce(); TestResuming(startArgs); - _logger.Log($"[App.OnStart(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None); OnStart(startArgs); - _logger.Log($"[App.OnStartAsync(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None); await OnStartAsync(startArgs); Window.Current.Activate(); } catch (Exception ex) { - _logger.Log($"ERROR {ex.Message}", Category.Exception, Priority.High); - Debugger.Break(); + Container.Resolve().Log(ex.ToString(), Category.Exception, Priority.High); } finally { @@ -153,26 +125,6 @@ private static void TestResuming(StartArgs startArgs) } } - private static DateTime? SuspendData - { - get - { - if (ApplicationData.Current.LocalSettings.Values.TryGetValue("Suspend_Data", out var value) - && value != null - && DateTime.TryParse(value.ToString(), out var date)) - { - return date; - } - else - { - return null; - } - } - set => ApplicationData.Current.LocalSettings.Values["Suspend_Data"] = value; - } - - #region overrides - protected virtual void OnSuspending() { /* empty */ } protected virtual Task OnSuspendingAsync() => Task.CompletedTask; @@ -198,7 +150,7 @@ protected virtual void ConfigureViewModelLocator() if (view is Page page && page.Frame != null) { - navigationService = Container.CreateNavigationService(page.Frame, SupportedNavigationGestures()); + navigationService = CreateNavigationService(page.Frame, null, SupportedNavigationGestures()); } return Container.Resolve(viewModelType, (typeof(INavigationService), navigationService)); @@ -233,6 +185,115 @@ protected virtual void RegisterRequiredTypes(IContainerRegistry containerRegistr containerRegistry.RegisterSingleton(); } + #region Factory Methods + + private INavigationService CreateNavigationService(Frame frame, CoreWindow window, params Gesture[] gestures) + { + if (frame is null) + frame = new Frame(); + + if (window is null) + window = Window.Current.CoreWindow; + + var gesture_service = CreateGestureServiceForWindow(window); + var navigation_service = Container.Resolve(NavigationServiceParameterName, (typeof(Frame), frame)); + foreach (var gesture in gestures) + { + switch (gesture) + { + case Gesture.Back: + gesture_service.BackRequested += async (s, e) => await navigation_service.GoBackAsync(); + break; + case Gesture.Forward: + gesture_service.ForwardRequested += async (s, e) => await navigation_service.GoForwardAsync(default(INavigationParameters)); + break; + case Gesture.Refresh: + gesture_service.RefreshRequested += async (s, e) => await navigation_service.RefreshAsync(); + break; + } + } + + return navigation_service; + } + + IGestureService CreateGestureServiceForWindow(CoreWindow window) + { + var service = Container.Resolve((typeof(CoreWindow), window)); + + // remove when closed + void Window_Closed(CoreWindow sender, CoreWindowEventArgs args) + { + window.Closed -= Window_Closed; + if (service is IDestructibleGestureService disposable) + { + disposable.Destroy(window); + } + } + window.Closed += Window_Closed; + + return service; + } + + #endregion + + #region Sealed Application Methods + + protected override sealed async void OnActivated(IActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnFileActivated(FileActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnSearchActivated(SearchActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnShareTargetActivated(ShareTargetActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); + protected override sealed async void OnLaunched(LaunchActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Launch)); + protected override sealed async void OnBackgroundActivated(BackgroundActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Background)); + + protected override void OnWindowCreated(WindowCreatedEventArgs args) + { + base.OnWindowCreated(args); + _windowCreated?.Invoke(this, args); + } + +#endregion + +#region Prism Events + +#pragma warning disable CS0067 // unused events + [EditorBrowsable(EditorBrowsableState.Never)] + private new event EventHandler Resuming; + + [EditorBrowsable(EditorBrowsableState.Never)] + private new event SuspendingEventHandler Suspending; + + [EditorBrowsable(EditorBrowsableState.Never)] + private new event EnteredBackgroundEventHandler EnteredBackground; + + [EditorBrowsable(EditorBrowsableState.Never)] + private new event LeavingBackgroundEventHandler LeavingBackground; + +#pragma warning restore CS0067 + + EnteredBackgroundEventHandler _enteredBackground; + event EnteredBackgroundEventHandler IPrismApplicationEvents.EnteredBackground + { + add { _enteredBackground += value; } + remove { _enteredBackground -= value; } + } + + LeavingBackgroundEventHandler _leavingBackground; + event LeavingBackgroundEventHandler IPrismApplicationEvents.LeavingBackground + { + add { _leavingBackground += value; } + remove { _leavingBackground -= value; } + } + + TypedEventHandler _windowCreated; + event TypedEventHandler IPrismApplicationEvents.WindowCreated + { + add { _windowCreated += value; } + remove { _windowCreated -= value; } + } + #endregion } } diff --git a/Source/Windows10/Prism.Windows/PrismApplicationBase.events.cs b/Source/Windows10/Prism.Windows/PrismApplicationBase.events.cs deleted file mode 100644 index d1993cba8..000000000 --- a/Source/Windows10/Prism.Windows/PrismApplicationBase.events.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using Windows.Foundation; -using Windows.UI.Xaml; - -namespace Prism -{ - public abstract partial class PrismApplicationBase : IPrismApplicationEvents - { -#pragma warning disable CS0067 // unused events - [EditorBrowsable(EditorBrowsableState.Never)] - private new event EventHandler Resuming; - [EditorBrowsable(EditorBrowsableState.Never)] - private new event SuspendingEventHandler Suspending; - [EditorBrowsable(EditorBrowsableState.Never)] - private new event EnteredBackgroundEventHandler EnteredBackground; - [EditorBrowsable(EditorBrowsableState.Never)] - private new event LeavingBackgroundEventHandler LeavingBackground; -#pragma warning restore CS0067 - - EnteredBackgroundEventHandler _enteredBackground; - event EnteredBackgroundEventHandler IPrismApplicationEvents.EnteredBackground - { - add { _enteredBackground += value; } - remove { _enteredBackground -= value; } - } - LeavingBackgroundEventHandler _leavingBackground; - event LeavingBackgroundEventHandler IPrismApplicationEvents.LeavingBackground - { - add { _leavingBackground += value; } - remove { _leavingBackground -= value; } - } - TypedEventHandler _windowCreated; - event TypedEventHandler IPrismApplicationEvents.WindowCreated - { - add { _windowCreated += value; } - remove { _windowCreated -= value; } - } - } -} diff --git a/Source/Windows10/Prism.Windows/PrismApplicationBase.xaml.cs b/Source/Windows10/Prism.Windows/PrismApplicationBase.xaml.cs deleted file mode 100644 index d577b7429..000000000 --- a/Source/Windows10/Prism.Windows/PrismApplicationBase.xaml.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Windows.ApplicationModel.Activation; -using Windows.UI.Xaml; - -namespace Prism -{ - public abstract partial class PrismApplicationBase : Application - { - protected override sealed async void OnActivated(IActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnFileActivated(FileActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnSearchActivated(SearchActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnShareTargetActivated(ShareTargetActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Activate)); - protected override sealed async void OnLaunched(LaunchActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Launch)); - protected override sealed async void OnBackgroundActivated(BackgroundActivatedEventArgs e) => await InternalStartAsync(new StartArgs(e, StartKinds.Background)); - - protected override void OnWindowCreated(WindowCreatedEventArgs args) - { - base.OnWindowCreated(args); - _windowCreated?.Invoke(this, args); - } - } -} diff --git a/Source/Windows10/Prism.Windows/ResumeArgs.cs b/Source/Windows10/Prism.Windows/ResumeArgs.cs index 95beb3953..0251fa14a 100644 --- a/Source/Windows10/Prism.Windows/ResumeArgs.cs +++ b/Source/Windows10/Prism.Windows/ResumeArgs.cs @@ -4,7 +4,6 @@ namespace Prism { - public class ResumeArgs : IResumeArgs, IActivatedEventArgs { public ActivationKind Kind { get; set; } diff --git a/Source/Windows10/Prism.Windows/Services/Gesture/GestureServiceRegistry.cs b/Source/Windows10/Prism.Windows/Services/Gesture/GestureServiceRegistry.cs deleted file mode 100644 index 6a33e41d0..000000000 --- a/Source/Windows10/Prism.Windows/Services/Gesture/GestureServiceRegistry.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Prism.Ioc; -using System; -using System.Collections.Generic; -using Windows.UI.Core; -using Windows.UI.Xaml; -using System.ComponentModel; - -namespace Prism.Services -{ - public static class GestureServiceRegistry - { - private static Dictionary _cache { get; } - = new Dictionary(); - - [EditorBrowsable(EditorBrowsableState.Never)] - public static IGestureService GetForCurrentView(CoreWindow window = null) - { - if (!_cache.ContainsKey(window ?? Window.Current.CoreWindow)) - { - throw new Exception("Not setup for current view."); - } - return _cache[Window.Current.CoreWindow]; - } - - [EditorBrowsable(EditorBrowsableState.Never)] - public static void SetupForCurrentView(this IContainerProvider container, CoreWindow window) - { - if (_cache.ContainsKey(window)) - { - throw new Exception("Already setup for current view."); - } - var service = container.Resolve((typeof(CoreWindow), window)); - _cache.Add(window, service); - - // remove when closed - - void Window_Closed(CoreWindow sender, CoreWindowEventArgs args) - { - window.Closed -= Window_Closed; - if (_cache.ContainsKey(window)) - { - if(_cache[window] is IDestructibleGestureService disposable) - { - disposable.Destroy(window); - } - - _cache.Remove(window); - } - } - window.Closed += Window_Closed; - } - - [EditorBrowsable(EditorBrowsableState.Never)] - public static void ResetCache() => _cache.Clear(); - } -} diff --git a/Source/Windows10/Prism.Windows/StartArgs.cs b/Source/Windows10/Prism.Windows/StartArgs.cs index 8eb420478..018a33b0b 100644 --- a/Source/Windows10/Prism.Windows/StartArgs.cs +++ b/Source/Windows10/Prism.Windows/StartArgs.cs @@ -1,6 +1,5 @@ using Windows.ApplicationModel.Activation; - namespace Prism { public class StartArgs : IStartArgs