diff --git a/Directory.Build.props b/Directory.Build.props index 4657315c7..d086b708b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 3.0.0-preview.10 + 3.0.0-preview.11 12.0 true diff --git a/src/Wpf.Ui.Demo.Mvvm/Usings.cs b/src/Wpf.Ui.Demo.Mvvm/Usings.cs index 9759042c6..47e40908a 100644 --- a/src/Wpf.Ui.Demo.Mvvm/Usings.cs +++ b/src/Wpf.Ui.Demo.Mvvm/Usings.cs @@ -3,6 +3,6 @@ // Copyright (C) Leszek Pomianowski and WPF UI Contributors. // All Rights Reserved. +global using System.Windows; global using CommunityToolkit.Mvvm.ComponentModel; global using CommunityToolkit.Mvvm.Input; -global using System.Windows; diff --git a/src/Wpf.Ui.Demo.Simple/MainWindow.xaml b/src/Wpf.Ui.Demo.Simple/MainWindow.xaml index 12f9e0e13..4d430b511 100644 --- a/src/Wpf.Ui.Demo.Simple/MainWindow.xaml +++ b/src/Wpf.Ui.Demo.Simple/MainWindow.xaml @@ -72,7 +72,7 @@ diff --git a/src/Wpf.Ui.Extension.Template.Compact/Usings.cs b/src/Wpf.Ui.Extension.Template.Compact/Usings.cs index 0e5696c96..3e9ee1523 100644 --- a/src/Wpf.Ui.Extension.Template.Compact/Usings.cs +++ b/src/Wpf.Ui.Extension.Template.Compact/Usings.cs @@ -1,5 +1,5 @@ -global using CommunityToolkit.Mvvm.ComponentModel; -global using CommunityToolkit.Mvvm.Input; -global using System; +global using System; global using System.Windows; +global using CommunityToolkit.Mvvm.ComponentModel; +global using CommunityToolkit.Mvvm.Input; global using Wpf.Ui.Services; diff --git a/src/Wpf.Ui.Extension.Template.Fluent/Usings.cs b/src/Wpf.Ui.Extension.Template.Fluent/Usings.cs index 0e5696c96..3e9ee1523 100644 --- a/src/Wpf.Ui.Extension.Template.Fluent/Usings.cs +++ b/src/Wpf.Ui.Extension.Template.Fluent/Usings.cs @@ -1,5 +1,5 @@ -global using CommunityToolkit.Mvvm.ComponentModel; -global using CommunityToolkit.Mvvm.Input; -global using System; +global using System; global using System.Windows; +global using CommunityToolkit.Mvvm.ComponentModel; +global using CommunityToolkit.Mvvm.Input; global using Wpf.Ui.Services; diff --git a/src/Wpf.Ui.Gallery/Usings.cs b/src/Wpf.Ui.Gallery/Usings.cs index 6fef748bb..bb18e5018 100644 --- a/src/Wpf.Ui.Gallery/Usings.cs +++ b/src/Wpf.Ui.Gallery/Usings.cs @@ -3,11 +3,6 @@ // Copyright (C) Leszek Pomianowski and WPF UI Contributors. // All Rights Reserved. -global using CommunityToolkit.Mvvm.ComponentModel; -global using CommunityToolkit.Mvvm.Input; -global using Microsoft.Extensions.Configuration; -global using Microsoft.Extensions.DependencyInjection; -global using Microsoft.Extensions.Hosting; global using System; global using System.Collections.Generic; global using System.Collections.ObjectModel; @@ -25,3 +20,8 @@ global using System.Windows.Markup; global using System.Windows.Media; global using System.Windows.Threading; +global using CommunityToolkit.Mvvm.ComponentModel; +global using CommunityToolkit.Mvvm.Input; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationCache.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationCache.cs index e74b6db3f..529a26d37 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationCache.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationCache.cs @@ -22,16 +22,34 @@ internal class NavigationCache if (cacheMode == NavigationCacheMode.Disabled) { +#if DEBUG + System + .Diagnostics + .Debug + .WriteLine($"Cache for {entryType} is disabled. Generating instance using action..."); +#endif + return generate.Invoke(); } if (!_entires.TryGetValue(entryType, out var value)) { +#if DEBUG + System + .Diagnostics + .Debug + .WriteLine($"{entryType} not found in cache, generating instance using action..."); +#endif + value = generate.Invoke(); _entires.Add(entryType, value); } +#if DEBUG + System.Diagnostics.Debug.WriteLine($"{entryType} found in cache."); +#endif + return value; } } diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationCacheMode.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationCacheMode.cs index 3fc83dc9f..2bd38c327 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationCacheMode.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationCacheMode.cs @@ -27,5 +27,5 @@ public enum NavigationCacheMode /// /// The page is cached and the cached instance is reused for every visit regardless of the cache size for the frame. /// - Reguired + Required } diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs index 3ebe529f0..e54335e33 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs @@ -239,8 +239,11 @@ private void AddToJournal(INavigationViewItem viewItem, bool isBackwardsNavigate #if DEBUG Debug.WriteLineIf(EnableDebugMessages, $"JOURNAL INDEX {_currentIndexInJournal}"); + if (Journal.Count > 0) + { Debug.WriteLineIf(EnableDebugMessages, $"JOURNAL LAST ELEMENT {Journal[^1]}"); + } #endif } @@ -263,26 +266,54 @@ private object GetNavigationItemInstance(INavigationViewItem viewItem) ?? throw new ArgumentNullException($"{nameof(_pageService.GetPage)} returned null"); } - return NavigationViewActivator.CreateInstance(viewItem.TargetPageType) - ?? throw new ArgumentException("Failed to create instance of the page"); + return _cache.Remember( + viewItem.TargetPageType, + viewItem.NavigationCacheMode, + ComputeCachedNavigationInstance + ) + ?? throw new ArgumentNullException( + $"Unable to get or create instance of {viewItem.TargetPageType} from cache." + ); - //return _cache.Remember(viewItem.TargetPageType, viewItem.NavigationCacheMode, () => - //{ - // if (_serviceProvider is not null) - // { - // return _serviceProvider.GetService(viewItem.TargetPageType) ?? - // new ArgumentNullException($"{nameof(_serviceProvider.GetService)} returned null"); - // } - - // if (_pageService is not null) - // { - // return _pageService.GetPage(viewItem.TargetPageType) ?? - // throw new ArgumentNullException($"{nameof(_pageService.GetPage)} returned null"); - // } - - // return NavigationViewActivator.CreateInstance(viewItem.TargetPageType) ?? - // throw new ArgumentException("Failed to create instance of the page"); - //}); + object? ComputeCachedNavigationInstance() => GetPageInstanceFromCache(viewItem.TargetPageType); + } + + private object? GetPageInstanceFromCache(Type? targetPageType) + { + if (targetPageType is null) + { + return default; + } + + if (_serviceProvider is not null) + { +#if DEBUG + System + .Diagnostics + .Debug + .WriteLine($"Getting {targetPageType} from cache using IServiceProvider."); +#endif + + return _serviceProvider.GetService(targetPageType) + ?? new ArgumentNullException($"{nameof(_serviceProvider.GetService)} returned null"); + } + + if (_pageService is not null) + { +#if DEBUG + System.Diagnostics.Debug.WriteLine($"Getting {targetPageType} from cache using IPageService."); +#endif + + return _pageService.GetPage(targetPageType) + ?? throw new ArgumentNullException($"{nameof(_pageService.GetPage)} returned null"); + } + +#if DEBUG + System.Diagnostics.Debug.WriteLine($"Getting {targetPageType} from cache using reflection."); +#endif + + return NavigationViewActivator.CreateInstance(targetPageType) + ?? throw new ArgumentException("Failed to create instance of the page"); } private static void ApplyAttachedProperties(INavigationViewItem viewItem, object pageInstance) diff --git a/src/Wpf.Ui/Interop/UnsafeNativeMethods.cs b/src/Wpf.Ui/Interop/UnsafeNativeMethods.cs index 2a706aa93..35e865406 100644 --- a/src/Wpf.Ui/Interop/UnsafeNativeMethods.cs +++ b/src/Wpf.Ui/Interop/UnsafeNativeMethods.cs @@ -297,11 +297,11 @@ public static bool ApplyWindowLegacyMicaEffect(IntPtr handle) // TODO: Validate HRESULT _ = Dwmapi.DwmSetWindowAttribute( - handle, - Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_MICA_EFFECT, - ref backdropPvAttribute, - Marshal.SizeOf(typeof(int)) - ); + handle, + Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_MICA_EFFECT, + ref backdropPvAttribute, + Marshal.SizeOf(typeof(int)) + ); return true; } diff --git a/tests/Wpf.Ui.UnitTests/Usings.cs b/tests/Wpf.Ui.UnitTests/Usings.cs index 047ddaba9..1d95163ae 100644 --- a/tests/Wpf.Ui.UnitTests/Usings.cs +++ b/tests/Wpf.Ui.UnitTests/Usings.cs @@ -3,7 +3,7 @@ // Copyright (C) Leszek Pomianowski and WPF UI Contributors. // All Rights Reserved. -global using NSubstitute; global using System; global using System.Windows; +global using NSubstitute; global using Xunit;