From 4a44e16d585065572133145c3dc22d583d83bec6 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Fri, 22 Dec 2023 16:18:24 +0330 Subject: [PATCH] add custom content page creator --- .../Navigations/NavigationManagerBase.cs | 4 ++-- .../Navigations/DefaultNavigationManager.cs | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/CSharp/Cores/EasyMicroservices.UI.Cores.Mvvm/Navigations/NavigationManagerBase.cs b/src/CSharp/Cores/EasyMicroservices.UI.Cores.Mvvm/Navigations/NavigationManagerBase.cs index 2940e3c..ff171ad 100644 --- a/src/CSharp/Cores/EasyMicroservices.UI.Cores.Mvvm/Navigations/NavigationManagerBase.cs +++ b/src/CSharp/Cores/EasyMicroservices.UI.Cores.Mvvm/Navigations/NavigationManagerBase.cs @@ -17,7 +17,7 @@ public abstract class NavigationManagerBase /// /// /// - protected ConcurrentDictionary Pages { get; set; } = new ConcurrentDictionary(); + protected ConcurrentDictionary> Pages { get; set; } = new ConcurrentDictionary>(); /// /// @@ -76,6 +76,6 @@ public abstract class NavigationManagerBase public void RegisterPage(string pageName) where T : IPage, new() { - Pages.TryAdd(pageName, typeof(T)); + Pages.TryAdd(pageName, () => new T()); } } diff --git a/src/CSharp/MauiComponents/EasyMicroservices.UI.MauiComponents/Navigations/DefaultNavigationManager.cs b/src/CSharp/MauiComponents/EasyMicroservices.UI.MauiComponents/Navigations/DefaultNavigationManager.cs index 8cfa7d0..4ab4558 100644 --- a/src/CSharp/MauiComponents/EasyMicroservices.UI.MauiComponents/Navigations/DefaultNavigationManager.cs +++ b/src/CSharp/MauiComponents/EasyMicroservices.UI.MauiComponents/Navigations/DefaultNavigationManager.cs @@ -1,6 +1,7 @@ using EasyMicroservices.UI.Cores; using EasyMicroservices.UI.Cores.Interfaces; using EasyMicroservices.UI.Cores.Navigations; +using EasyMicroservices.UI.MauiComponents.Design.Pages; namespace EasyMicroservices.UI.MauiComponents.Navigations; public class DefaultNavigationManager : NavigationManagerBase @@ -23,9 +24,9 @@ public override Task PushAsync(string pageName, bo public override async Task PushDataAsync(TData data, string pageName, bool doClear = false) { - if (!Pages.TryGetValue(pageName, out Type pageTpe)) + if (!Pages.TryGetValue(pageName, out Func pageCreator)) throw new Exception($"Page {pageName} not found, did you register it?"); - var findPage = Activator.CreateInstance(pageTpe); + var findPage = pageCreator(); ContentPage page = findPage as ContentPage; if (page == null) throw new NotImplementedException($"Page {pageName} is not inherit Page!"); @@ -86,4 +87,20 @@ public override Task OpenBrowser(string url) { return Browser.OpenAsync(url, BrowserLaunchMode.SystemPreferred); } + + /// + /// + /// + /// + public void RegisterContentPage(string pageName) + where TView : ContentView, new() + { + Pages.TryAdd(pageName, () => + { + return new EasyContentPage() + { + Content = new TView() + }; + }); + } } \ No newline at end of file