From 1c14530139473be0680310109f49165675821132 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Aug 2021 00:50:48 +0100 Subject: [PATCH] WIP: more pages + reflection fix for navigation --- MobileApp1/App.xaml.cs | 4 +- MobileApp1/Helpers/ContentPageHelper.cs | 56 +++++++++++--- MobileApp1/Helpers/ContentViewHelper.cs | 56 +++++++++++--- MobileApp1/MobileApp1.csproj | 10 ++- MobileApp1/Services/CorporationMockService.cs | 4 +- .../Views/Content/Login/LoginView.xaml.cs | 7 +- .../Content/Login/PasswordResetView.xaml.cs | 2 +- MobileApp1/Views/FlyoutMenuPage.xaml | 4 +- MobileApp1/Views/HomePage.xaml.cs | 2 +- MobileApp1/Views/LogoutPage.xaml | 21 ++++++ MobileApp1/Views/LogoutPage.xaml.cs | 25 +++++++ .../{BlankPage.xaml => MyStuffPage.xaml} | 4 +- ...{BlankPage.xaml.cs => MyStuffPage.xaml.cs} | 4 +- MobileApp1/Views/SettingsPage.xaml | 32 +++++--- MobileApp1/Views/SettingsPage.xaml.cs | 75 ++++++++++++++++++- 15 files changed, 249 insertions(+), 57 deletions(-) create mode 100644 MobileApp1/Views/LogoutPage.xaml create mode 100644 MobileApp1/Views/LogoutPage.xaml.cs rename MobileApp1/Views/{BlankPage.xaml => MyStuffPage.xaml} (89%) rename MobileApp1/Views/{BlankPage.xaml.cs => MyStuffPage.xaml.cs} (70%) diff --git a/MobileApp1/App.xaml.cs b/MobileApp1/App.xaml.cs index b600287..3322487 100644 --- a/MobileApp1/App.xaml.cs +++ b/MobileApp1/App.xaml.cs @@ -17,8 +17,8 @@ namespace MobileApp1 //MainPage = new AppShell(); - //MainPage = new NavigationPage(new LoginPage()); - MainPage = new MainPage(); + MainPage = new NavigationPage(new LoginPage()); + //MainPage = new MainPage(); } protected override void OnStart() diff --git a/MobileApp1/Helpers/ContentPageHelper.cs b/MobileApp1/Helpers/ContentPageHelper.cs index 24786fd..2065558 100644 --- a/MobileApp1/Helpers/ContentPageHelper.cs +++ b/MobileApp1/Helpers/ContentPageHelper.cs @@ -6,13 +6,43 @@ namespace MobileApp1.Views public class ContentPageHelper { - public static App GetApp(ContentPage view) => GetParentElement(view); + public static App GetApp(ContentPage view) => (App)GetParentElement(view); + //{ + // if (view == null) return null; - public static Application GetApplication(ContentPage view) => GetParentElement(view); + // Element item = view.Parent; - public static MainPage GetMainPage(ContentPage view) => GetParentElement(view); + // while (true) + // { + // if (item == null) break; + // if (item is App) break; - public static T GetParentElement(ContentPage view) + // item = item.Parent; + // } + + // return (item as App); + //} + + public static Application GetApplication(ContentPage view) => (Application)GetParentElement(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 MainPage GetMainPage(ContentPage view) => (MainPage)GetParentElement(view); + + public static object GetParentElement(ContentPage view) { if (view == null) return default(T); @@ -26,14 +56,16 @@ namespace MobileApp1.Views item = item.Parent; } - try - { - return (T)Convert.ChangeType(item, typeof(T)); - } - catch (InvalidCastException) - { - return default(T); - } + //try + //{ + // return (T)Convert.ChangeType(item, typeof(T)); + //} + //catch (InvalidCastException) + //{ + // return default(T); + //} + + return item; } public static void SetDetailPage(ContentPage view, Type pageType, bool useNavigationPage = true) => SetDetailPage(view, (Page)Activator.CreateInstance(pageType), useNavigationPage); diff --git a/MobileApp1/Helpers/ContentViewHelper.cs b/MobileApp1/Helpers/ContentViewHelper.cs index ee36ad0..49cdf9c 100644 --- a/MobileApp1/Helpers/ContentViewHelper.cs +++ b/MobileApp1/Helpers/ContentViewHelper.cs @@ -6,13 +6,43 @@ namespace MobileApp1.Views public class ContentViewHelper { - public static App GetApp(ContentView view) => GetParentElement(view); + public static App GetApp(ContentView view) => (App)GetParentElement(view); + //{ + // if (view == null) return null; - public static Application GetApplication(ContentView view) => GetParentElement(view); + // Element item = view.Parent; - public static ContentPage GetContentPage(ContentView view) => GetParentElement(view); + // while (true) + // { + // if (item == null) break; + // if (item is App) break; - public static T GetParentElement(ContentView view) + // item = item.Parent; + // } + + // return (item as App); + //} + + public static Application GetApplication(ContentView view) => (Application)GetParentElement(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 ContentPage GetContentPage(ContentView view) => (ContentPage)GetParentElement(view); + + public static object GetParentElement(ContentView view) { if (view == null) return default(T); @@ -26,14 +56,16 @@ namespace MobileApp1.Views item = item.Parent; } - try - { - return (T)Convert.ChangeType(item, typeof(T)); - } - catch (InvalidCastException) - { - return default(T); - } + //try + //{ + // return (T)Convert.ChangeType(item, typeof(T)); + //} + //catch (InvalidCastException) + //{ + // return default(T); + //} + + return item; } public static void SetMainPage(ContentView view, Page page) diff --git a/MobileApp1/MobileApp1.csproj b/MobileApp1/MobileApp1.csproj index 27ed372..ca1c69e 100644 --- a/MobileApp1/MobileApp1.csproj +++ b/MobileApp1/MobileApp1.csproj @@ -35,14 +35,20 @@ + + AboutPage.xaml + LoadingIndicatorView.xaml CompanyHeaderView.xaml - - BlankPage.xaml + + LogoutPage.xaml + + + MyStuffPage.xaml PasswordResetPage.xaml diff --git a/MobileApp1/Services/CorporationMockService.cs b/MobileApp1/Services/CorporationMockService.cs index ee6fa35..fb415c0 100644 --- a/MobileApp1/Services/CorporationMockService.cs +++ b/MobileApp1/Services/CorporationMockService.cs @@ -31,7 +31,7 @@ public ServiceResult Logout() { - System.Threading.Thread.Sleep(6000); + System.Threading.Thread.Sleep(2000); this.DisplayName = string.Empty; this.IsLoggedIn = false; @@ -45,7 +45,7 @@ public ServiceResult RequestPasswordReset(string username) { - System.Threading.Thread.Sleep(6000); + System.Threading.Thread.Sleep(2000); this.DisplayName = string.Empty; this.IsLoggedIn = false; diff --git a/MobileApp1/Views/Content/Login/LoginView.xaml.cs b/MobileApp1/Views/Content/Login/LoginView.xaml.cs index 7ea557e..964fd14 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.GetParentElement(this); + LoginPage cp = (LoginPage)ContentViewHelper.GetParentElement(this); if (cp != null) { cp.IsBusy = value; @@ -69,16 +69,15 @@ namespace MobileApp1.Views this.IsBusy = false; - //ContentViewHelper.SetMainPage(this, new AppShell()); - //await cp.DisplayAlert("Clicked", "Login", "OK"); - if (result.IsSuccess) { /// part 2 } + ContentViewHelper.SetMainPage(this, new MainPage()); + }); } diff --git a/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs b/MobileApp1/Views/Content/Login/PasswordResetView.xaml.cs index 9400270..2b05959 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.GetParentElement(this); + LoginPage cp = (LoginPage)ContentViewHelper.GetParentElement(this); if (cp != null) { cp.IsBusy = value; diff --git a/MobileApp1/Views/FlyoutMenuPage.xaml b/MobileApp1/Views/FlyoutMenuPage.xaml index c691061..710b3b4 100644 --- a/MobileApp1/Views/FlyoutMenuPage.xaml +++ b/MobileApp1/Views/FlyoutMenuPage.xaml @@ -26,10 +26,10 @@ - + - + diff --git a/MobileApp1/Views/HomePage.xaml.cs b/MobileApp1/Views/HomePage.xaml.cs index b8a5643..c90d035 100644 --- a/MobileApp1/Views/HomePage.xaml.cs +++ b/MobileApp1/Views/HomePage.xaml.cs @@ -17,7 +17,7 @@ namespace MobileApp1.Views { await Task.Run(() => { - ContentPageHelper.SetDetailPage(this, typeof(BlankPage)); + ContentPageHelper.SetDetailPage(this, typeof(MyStuffPage)); }); } diff --git a/MobileApp1/Views/LogoutPage.xaml b/MobileApp1/Views/LogoutPage.xaml new file mode 100644 index 0000000..de79c13 --- /dev/null +++ b/MobileApp1/Views/LogoutPage.xaml @@ -0,0 +1,21 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/MobileApp1/Views/LogoutPage.xaml.cs b/MobileApp1/Views/LogoutPage.xaml.cs new file mode 100644 index 0000000..811d9cc --- /dev/null +++ b/MobileApp1/Views/LogoutPage.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 LogoutPage : ContentPage + { + public LogoutPage() + { + InitializeComponent(); + } + + private async void button1_Clicked(object sender, EventArgs e) + { + await Task.Run(() => + { + ContentPageHelper.SetMainPage(this, new LoginPage()); + }); + } + + } +} \ No newline at end of file diff --git a/MobileApp1/Views/BlankPage.xaml b/MobileApp1/Views/MyStuffPage.xaml similarity index 89% rename from MobileApp1/Views/BlankPage.xaml rename to MobileApp1/Views/MyStuffPage.xaml index d258d81..807d808 100644 --- a/MobileApp1/Views/BlankPage.xaml +++ b/MobileApp1/Views/MyStuffPage.xaml @@ -1,9 +1,9 @@  + Title="My Stuff"> diff --git a/MobileApp1/Views/BlankPage.xaml.cs b/MobileApp1/Views/MyStuffPage.xaml.cs similarity index 70% rename from MobileApp1/Views/BlankPage.xaml.cs rename to MobileApp1/Views/MyStuffPage.xaml.cs index 55ed661..4a73b91 100644 --- a/MobileApp1/Views/BlankPage.xaml.cs +++ b/MobileApp1/Views/MyStuffPage.xaml.cs @@ -3,9 +3,9 @@ using Xamarin.Forms; namespace MobileApp1.Views { - public partial class BlankPage : ContentPage + public partial class MyStuffPage : ContentPage { - public BlankPage() + public MyStuffPage() { InitializeComponent(); diff --git a/MobileApp1/Views/SettingsPage.xaml b/MobileApp1/Views/SettingsPage.xaml index 936119e..7325ee5 100644 --- a/MobileApp1/Views/SettingsPage.xaml +++ b/MobileApp1/Views/SettingsPage.xaml @@ -1,7 +1,7 @@  @@ -14,19 +14,31 @@ diff --git a/MobileApp1/Views/SettingsPage.xaml.cs b/MobileApp1/Views/SettingsPage.xaml.cs index ddc3d87..ac76f67 100644 --- a/MobileApp1/Views/SettingsPage.xaml.cs +++ b/MobileApp1/Views/SettingsPage.xaml.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; - using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,9 +8,80 @@ namespace MobileApp1.Views [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SettingsPage : ContentPage { + protected bool isBusy = false; + public SettingsPage() { InitializeComponent(); } + + protected override void OnAppearing() + { + base.OnAppearing(); + + if (pickerBox1.Items.Count > 0) pickerBox1.SelectedIndex = 0; + } + + private bool IsBusy + { + get => isBusy; + set + { + isBusy = value; + + Device.BeginInvokeOnMainThread(() => + { + loadingIndicatorView1.IsRunning = value; + checkBox1.IsEnabled = switchBox1.IsEnabled = datePicker1.IsEnabled = pickerBox1.IsEnabled = timePicker1.IsEnabled = !value; + button1.IsEnabled = !value; + }); + + } + } + + private async void button1_Clicked(object sender, EventArgs e) + { + await Task.Run(() => + { + //if (this.Parent == null) + //{ + // return; + //} + + //ContentPage cp = ContentViewHelper.GetContentPage(this); + //if (cp == null) + //{ + // return; + //} + + //App ap = ContentViewHelper.GetApp(this); + //if (ap == null) + //{ + // return; + //} + + this.IsBusy = true; + + //ServiceResult result = ap.CorporationService.Login(textbox1.Text, textbox2.Text); + + loadingIndicatorView1.Message = "Saved"; + + Device.BeginInvokeOnMainThread(() => + { + DisplayAlert("Settings", "Saved", "OK"); + }); + + this.IsBusy = false; + + //ContentViewHelper.SetMainPage(this, new AppShell()); + + //if (result.IsSuccess) + //{ + + //} + + }); + } + } } \ No newline at end of file