From 8d69e7ffabfc152935204df229debefc59a88ea7 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 3 Aug 2021 17:43:16 +0100 Subject: [PATCH] WIP: page setup minus my-stuff --- MobileApp1/Helpers/ContentPageHelper.cs | 79 +++++++++++++------ MobileApp1/Helpers/ContentViewHelper.cs | 53 +------------ MobileApp1/MobileApp1.csproj | 12 +++ MobileApp1/Views/AboutPage.xaml | 12 +++ MobileApp1/Views/AboutPage.xaml.cs | 20 +++++ .../Views/Content/Login/LoginView.xaml.cs | 2 +- .../Content/Login/PasswordResetView.xaml.cs | 2 +- MobileApp1/Views/FlyoutMenuPage.xaml | 40 ++++++++-- MobileApp1/Views/FlyoutMenuPage.xaml.cs | 35 +++++++- MobileApp1/Views/HomePage.xaml | 29 +++++++ MobileApp1/Views/HomePage.xaml.cs | 25 ++++++ MobileApp1/Views/MainPage.xaml | 2 +- MobileApp1/Views/MainPage.xaml.cs | 34 +------- MobileApp1/Views/SettingsPage.xaml | 51 ++++++++++++ MobileApp1/Views/SettingsPage.xaml.cs | 20 +++++ 15 files changed, 300 insertions(+), 116 deletions(-) create mode 100644 MobileApp1/Views/AboutPage.xaml create mode 100644 MobileApp1/Views/AboutPage.xaml.cs create mode 100644 MobileApp1/Views/HomePage.xaml create mode 100644 MobileApp1/Views/HomePage.xaml.cs create mode 100644 MobileApp1/Views/SettingsPage.xaml create mode 100644 MobileApp1/Views/SettingsPage.xaml.cs diff --git a/MobileApp1/Helpers/ContentPageHelper.cs b/MobileApp1/Helpers/ContentPageHelper.cs index 2b2aee7..24786fd 100644 --- a/MobileApp1/Helpers/ContentPageHelper.cs +++ b/MobileApp1/Helpers/ContentPageHelper.cs @@ -1,45 +1,69 @@ -using Xamarin.Forms; +using System; +using Xamarin.Forms; namespace MobileApp1.Views { public class ContentPageHelper { - public static App GetApp(ContentPage view) + public static App GetApp(ContentPage view) => GetParentElement(view); + + public static Application GetApplication(ContentPage view) => GetParentElement(view); + + public static MainPage GetMainPage(ContentPage view) => GetParentElement(view); + + public static T GetParentElement(ContentPage view) { - if (view == null) return null; + if (view == null) return default(T); Element item = view.Parent; while (true) { if (item == null) break; - if (item is App) break; + if (item is T) break; item = item.Parent; } - return (item as App); - } - - public static Application GetApplication(ContentPage view) - { - if (view == null) return null; - - Element item = view.Parent; - - while (true) + try { - if (item == null) break; - if (item is Application) break; - - item = item.Parent; + return (T)Convert.ChangeType(item, typeof(T)); + } + catch (InvalidCastException) + { + return default(T); } - - return (item as Application); } - public static void SetMainPage(ContentPage view, Page page) + public static void SetDetailPage(ContentPage view, Type pageType, bool useNavigationPage = true) => SetDetailPage(view, (Page)Activator.CreateInstance(pageType), useNavigationPage); + + public static void SetDetailPage(ContentPage view, Page page, bool useNavigationPage = true) + { + if (view == null) return; + if (view.Parent == null) return; + if (page == null) return; + + MainPage rs = GetMainPage(view); + if (rs == null) + { + return; + } + + Device.BeginInvokeOnMainThread(() => + { + if (useNavigationPage) + { + rs.Detail = new NavigationPage(page); + } + else + { + rs.Detail = page; + } + }); + } + + public static void SetMainPage(ContentPage view, Page page, bool useNavigationPage = false) { if (view == null) return; if (view.Parent == null) return; @@ -51,8 +75,17 @@ namespace MobileApp1.Views return; } - //rs.MainPage = new NavigationPage(new PasswordResetPage()); - rs.MainPage = page; + Device.BeginInvokeOnMainThread(() => + { + if (useNavigationPage) + { + rs.MainPage = new NavigationPage(page); + } + else + { + rs.MainPage = page; + } + }); } public static async void PushMainPageNavigation(ContentPage view, Page page) diff --git a/MobileApp1/Helpers/ContentViewHelper.cs b/MobileApp1/Helpers/ContentViewHelper.cs index b5cfedb..ee36ad0 100644 --- a/MobileApp1/Helpers/ContentViewHelper.cs +++ b/MobileApp1/Helpers/ContentViewHelper.cs @@ -6,24 +6,13 @@ namespace MobileApp1.Views public class ContentViewHelper { - public static ContentPage GetContentPage(ContentView view) - { - if (view == null) return null; + public static App GetApp(ContentView view) => GetParentElement(view); - Element item = view.Parent; + public static Application GetApplication(ContentView view) => GetParentElement(view); - while (true) - { - if (item == null) break; - if (item is ContentPage) break; + public static ContentPage GetContentPage(ContentView view) => GetParentElement(view); - item = item.Parent; - } - - return (item as ContentPage); - } - - public static T GetContentPage(ContentView view) + public static T GetParentElement(ContentView view) { if (view == null) return default(T); @@ -47,40 +36,6 @@ namespace MobileApp1.Views } } - public static App GetApp(ContentView view) - { - if (view == null) return null; - - Element item = view.Parent; - - while (true) - { - if (item == null) break; - if (item is App) break; - - item = item.Parent; - } - - return (item as App); - } - - public static Application GetApplication(ContentView view) - { - if (view == null) return null; - - Element item = view.Parent; - - while (true) - { - if (item == null) break; - if (item is Application) break; - - item = item.Parent; - } - - return (item as Application); - } - public static void SetMainPage(ContentView view, Page page) { if (view == null) return; diff --git a/MobileApp1/MobileApp1.csproj b/MobileApp1/MobileApp1.csproj index c8c5b6a..27ed372 100644 --- a/MobileApp1/MobileApp1.csproj +++ b/MobileApp1/MobileApp1.csproj @@ -59,9 +59,15 @@ LoginView.xaml + + HomePage.xaml + + + MSBuild:UpdateDesignTimeXaml + MSBuild:UpdateDesignTimeXaml @@ -74,5 +80,11 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + \ No newline at end of file diff --git a/MobileApp1/Views/AboutPage.xaml b/MobileApp1/Views/AboutPage.xaml new file mode 100644 index 0000000..f53f057 --- /dev/null +++ b/MobileApp1/Views/AboutPage.xaml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/MobileApp1/Views/AboutPage.xaml.cs b/MobileApp1/Views/AboutPage.xaml.cs new file mode 100644 index 0000000..4782f87 --- /dev/null +++ b/MobileApp1/Views/AboutPage.xaml.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms; +using Xamarin.Forms.Xaml; + +namespace MobileApp1.Views +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + public partial class AboutPage : ContentPage + { + public AboutPage() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/MobileApp1/Views/Content/Login/LoginView.xaml.cs b/MobileApp1/Views/Content/Login/LoginView.xaml.cs index ad0edad..7ea557e 100644 --- a/MobileApp1/Views/Content/Login/LoginView.xaml.cs +++ b/MobileApp1/Views/Content/Login/LoginView.xaml.cs @@ -24,7 +24,7 @@ namespace MobileApp1.Views { isBusy = value; - LoginPage cp = ContentViewHelper.GetContentPage(this); + LoginPage cp = ContentViewHelper.GetParentElement(this); if (cp != null) { cp.IsBusy = value; diff --git a/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs b/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs index 93fe3de..9400270 100644 --- a/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs +++ b/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs @@ -24,7 +24,7 @@ namespace MobileApp1.Views { isBusy = value; - LoginPage cp = ContentViewHelper.GetContentPage(this); + LoginPage cp = ContentViewHelper.GetParentElement(this); if (cp != null) { cp.IsBusy = value; diff --git a/MobileApp1/Views/FlyoutMenuPage.xaml b/MobileApp1/Views/FlyoutMenuPage.xaml index abbbf1c..c691061 100644 --- a/MobileApp1/Views/FlyoutMenuPage.xaml +++ b/MobileApp1/Views/FlyoutMenuPage.xaml @@ -10,30 +10,56 @@ - + + - + - - + + - + - + - + + + + diff --git a/MobileApp1/Views/FlyoutMenuPage.xaml.cs b/MobileApp1/Views/FlyoutMenuPage.xaml.cs index d8958cf..5052b47 100644 --- a/MobileApp1/Views/FlyoutMenuPage.xaml.cs +++ b/MobileApp1/Views/FlyoutMenuPage.xaml.cs @@ -1,4 +1,6 @@ -using Xamarin.Forms; +using MobileApp1.Models; +using System.Threading.Tasks; +using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace MobileApp1.Views @@ -9,6 +11,37 @@ namespace MobileApp1.Views public FlyoutMenuPage() { InitializeComponent(); + + listView1.ItemSelected += listView1_ItemSelected; } + + private async void listView1_ItemSelected(object sender, SelectedItemChangedEventArgs e) + { + await Task.Run(() => + { + var item = e.SelectedItem as FlyoutPageItem; + if (item == null) + { + return; + } + + MainPage mainPage = ContentPageHelper.GetMainPage(this); + if (mainPage == null) + { + return; + } + + ContentPageHelper.SetDetailPage(this, item.TargetType); + + Device.BeginInvokeOnMainThread(() => + { + listView1.SelectedItem = null; + + mainPage.IsPresented = false; + }); + }); + } + + } } \ No newline at end of file diff --git a/MobileApp1/Views/HomePage.xaml b/MobileApp1/Views/HomePage.xaml new file mode 100644 index 0000000..afd6775 --- /dev/null +++ b/MobileApp1/Views/HomePage.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/MobileApp1/Views/HomePage.xaml.cs b/MobileApp1/Views/HomePage.xaml.cs new file mode 100644 index 0000000..b8a5643 --- /dev/null +++ b/MobileApp1/Views/HomePage.xaml.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading.Tasks; +using Xamarin.Forms; +using Xamarin.Forms.Xaml; + +namespace MobileApp1.Views +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + public partial class HomePage : ContentPage + { + public HomePage() + { + InitializeComponent(); + } + + private async void button1_Clicked(object sender, EventArgs e) + { + await Task.Run(() => + { + ContentPageHelper.SetDetailPage(this, typeof(BlankPage)); + }); + } + + } +} \ No newline at end of file diff --git a/MobileApp1/Views/MainPage.xaml b/MobileApp1/Views/MainPage.xaml index c6e57fd..6ab3d64 100644 --- a/MobileApp1/Views/MainPage.xaml +++ b/MobileApp1/Views/MainPage.xaml @@ -8,7 +8,7 @@ - + diff --git a/MobileApp1/Views/MainPage.xaml.cs b/MobileApp1/Views/MainPage.xaml.cs index b2cca79..e156ed8 100644 --- a/MobileApp1/Views/MainPage.xaml.cs +++ b/MobileApp1/Views/MainPage.xaml.cs @@ -1,6 +1,4 @@ -using MobileApp1.Models; -using System; -using Xamarin.Forms; +using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace MobileApp1.Views @@ -11,36 +9,6 @@ namespace MobileApp1.Views public MainPage() { InitializeComponent(); - - flyoutPage.listView.ItemSelected += listView1_OnItemSelected; - } - - //private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e) - //{ - // var item = e.SelectedItem as MainPageFlyoutMenuItem; - // if (item == null) - // return; - - // var page = (Page)Activator.CreateInstance(item.TargetType); - // page.Title = item.Title; - - // Detail = new NavigationPage(page); - // IsPresented = false; - - // FlyoutPage.ListView.SelectedItem = null; - //} - - private void listView1_OnItemSelected(object sender, SelectedItemChangedEventArgs e) - { - var item = e.SelectedItem as FlyoutPageItem; - if (item == null) - { - return; - } - - Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType)); - flyoutPage.listView.SelectedItem = null; - IsPresented = false; } } diff --git a/MobileApp1/Views/SettingsPage.xaml b/MobileApp1/Views/SettingsPage.xaml new file mode 100644 index 0000000..936119e --- /dev/null +++ b/MobileApp1/Views/SettingsPage.xaml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MobileApp1/Views/SettingsPage.xaml.cs b/MobileApp1/Views/SettingsPage.xaml.cs new file mode 100644 index 0000000..ddc3d87 --- /dev/null +++ b/MobileApp1/Views/SettingsPage.xaml.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms; +using Xamarin.Forms.Xaml; + +namespace MobileApp1.Views +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + public partial class SettingsPage : ContentPage + { + public SettingsPage() + { + InitializeComponent(); + } + } +} \ No newline at end of file