WIP: more pages + reflection fix for navigation

This commit is contained in:
Ray 2021-08-05 00:50:48 +01:00
parent 8d69e7ffab
commit 1c14530139
15 changed files with 249 additions and 57 deletions

View File

@ -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()

View File

@ -6,13 +6,43 @@ namespace MobileApp1.Views
public class ContentPageHelper
{
public static App GetApp(ContentPage view) => GetParentElement<App>(view);
public static App GetApp(ContentPage view) => (App)GetParentElement<App>(view);
//{
// if (view == null) return null;
public static Application GetApplication(ContentPage view) => GetParentElement<Application>(view);
// Element item = view.Parent;
public static MainPage GetMainPage(ContentPage view) => GetParentElement<MainPage>(view);
// while (true)
// {
// if (item == null) break;
// if (item is App) break;
public static T GetParentElement<T>(ContentPage view)
// item = item.Parent;
// }
// return (item as App);
//}
public static Application GetApplication(ContentPage view) => (Application)GetParentElement<Application>(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<MainPage>(view);
public static object GetParentElement<T>(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);

View File

@ -6,13 +6,43 @@ namespace MobileApp1.Views
public class ContentViewHelper
{
public static App GetApp(ContentView view) => GetParentElement<App>(view);
public static App GetApp(ContentView view) => (App)GetParentElement<App>(view);
//{
// if (view == null) return null;
public static Application GetApplication(ContentView view) => GetParentElement<Application>(view);
// Element item = view.Parent;
public static ContentPage GetContentPage(ContentView view) => GetParentElement<ContentPage>(view);
// while (true)
// {
// if (item == null) break;
// if (item is App) break;
public static T GetParentElement<T>(ContentView view)
// item = item.Parent;
// }
// return (item as App);
//}
public static Application GetApplication(ContentView view) => (Application)GetParentElement<Application>(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<ContentPage>(view);
public static object GetParentElement<T>(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)

View File

@ -35,14 +35,20 @@
</ItemGroup>
<ItemGroup>
<Compile Update="Views\AboutPage.xaml.cs">
<DependentUpon>AboutPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\Content\LoadingIndicatorView.xaml.cs">
<DependentUpon>LoadingIndicatorView.xaml</DependentUpon>
</Compile>
<Compile Update="Views\Content\CompanyHeaderView.xaml.cs">
<DependentUpon>CompanyHeaderView.xaml</DependentUpon>
</Compile>
<Compile Update="Views\BlankPage.xaml.cs">
<DependentUpon>BlankPage.xaml</DependentUpon>
<Compile Update="Views\LogoutPage.xaml.cs">
<DependentUpon>LogoutPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\MyStuffPage.xaml.cs">
<DependentUpon>MyStuffPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\PasswordResetPage.xaml.cs">
<DependentUpon>PasswordResetPage.xaml</DependentUpon>

View File

@ -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;

View File

@ -24,7 +24,7 @@ namespace MobileApp1.Views
{
isBusy = value;
LoginPage cp = ContentViewHelper.GetParentElement<LoginPage>(this);
LoginPage cp = (LoginPage)ContentViewHelper.GetParentElement<LoginPage>(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());
});
}

View File

@ -24,7 +24,7 @@ namespace MobileApp1.Views
{
isBusy = value;
LoginPage cp = ContentViewHelper.GetParentElement<LoginPage>(this);
LoginPage cp = (LoginPage)ContentViewHelper.GetParentElement<LoginPage>(this);
if (cp != null)
{
cp.IsBusy = value;

View File

@ -26,10 +26,10 @@
<ListView.ItemsSource>
<x:Array Type="{x:Type models:FlyoutPageItem}">
<models:FlyoutPageItem Title="Home" Icon="home.png" TargetType="{x:Type views:HomePage}" />
<models:FlyoutPageItem Title="My Stuff" Icon="layers.png" TargetType="{x:Type views:BlankPage}" />
<models:FlyoutPageItem Title="My Stuff" Icon="layers.png" TargetType="{x:Type views:MyStuffPage}" />
<models:FlyoutPageItem Title="Settings" Icon="settings.png" TargetType="{x:Type views:SettingsPage}" />
<models:FlyoutPageItem Title="About" Icon="help.png" TargetType="{x:Type views:AboutPage}" />
<models:FlyoutPageItem Title="Logout" Icon="logout.png" TargetType="{x:Type views:BlankPage}" />
<models:FlyoutPageItem Title="Logout" Icon="logout.png" TargetType="{x:Type views:LogoutPage}" />
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>

View File

@ -17,7 +17,7 @@ namespace MobileApp1.Views
{
await Task.Run(() =>
{
ContentPageHelper.SetDetailPage(this, typeof(BlankPage));
ContentPageHelper.SetDetailPage(this, typeof(MyStuffPage));
});
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:views="clr-namespace:MobileApp1.Views"
x:Class="MobileApp1.Views.LogoutPage"
Title="Logout">
<ContentPage.Content>
<ScrollView>
<StackLayout Margin="20">
<views:CompanyHeaderView />
<Label Text="Logout" FontSize="Body" />
<Label Text="Logout of your account." />
<Label Text="" Margin="0, 0, 0, 10" />
<Button x:Name="button1" Margin="0, 20, 0, 0" Text="Logout" Clicked="button1_Clicked" ></Button>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>

View File

@ -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());
});
}
}
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MobileApp1.Views.BlankPage"
x:Class="MobileApp1.Views.MyStuffPage"
xmlns:views="clr-namespace:MobileApp1.Views"
Title="Blank">
Title="My Stuff">
<ContentPage.Content>
<StackLayout Margin="20">
<views:CompanyHeaderView />

View File

@ -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();

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:views="clr-namespace:MobileApp1.Views"
x:Class="MobileApp1.Views.SettingsPage"
Title="Settings">
<ContentPage.Content>
@ -14,19 +14,31 @@
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="CheckBox Setting" VerticalTextAlignment="Center" />
<CheckBox Grid.Row="0" Grid.Column="1" HorizontalOptions="EndAndExpand" />
<CheckBox Grid.Row="0" Grid.Column="1" x:Name="checkBox1" HorizontalOptions="EndAndExpand" />
<Label Grid.Row="1" Grid.Column="0" Text="Switch Setting" VerticalTextAlignment="Center" />
<Switch Grid.Row="1" Grid.Column="1" HorizontalOptions="EndAndExpand" />
<Switch Grid.Row="1" Grid.Column="1" x:Name="switchBox1" HorizontalOptions="EndAndExpand" />
<Label Grid.Row="2" Grid.Column="0" Text="Date Setting" VerticalTextAlignment="Center" />
<DatePicker Grid.Row="2" Grid.Column="1" HorizontalOptions="EndAndExpand" Format="dd/MM/yyyy" />
<DatePicker Grid.Row="2" Grid.Column="1" x:Name="datePicker1" HorizontalOptions="EndAndExpand" Format="dd/MM/yyyy" />
<Label Grid.Row="3" Grid.Column="0" Text="TextBox Setting" VerticalTextAlignment="Center" />
<Entry Grid.Row="3" Grid.Column="1" HorizontalOptions="EndAndExpand" />
<Label Grid.Row="3" Grid.Column="0" Text="Time Setting" VerticalTextAlignment="Center" />
<TimePicker Grid.Row="3" Grid.Column="1" x:Name="timePicker1" HorizontalOptions="EndAndExpand" Format="HH:mm" />
<!--<Label Grid.Row="3" Grid.Column="0" Text="TextBox Setting" VerticalTextAlignment="Center" />
<Entry Grid.Row="3" Grid.Column="1" HorizontalOptions="EndAndExpand" />-->
<Label Grid.Row="4" Grid.Column="0" Text="Picker Setting" VerticalTextAlignment="Center" />
<Picker Grid.Row="4" Grid.Column="1" HorizontalOptions="EndAndExpand" MinimumWidthRequest="100" />
<Picker Grid.Row="5" Grid.Column="1" x:Name="pickerBox1" HorizontalOptions="FillAndExpand">
<Picker.Items>
<x:String></x:String>
<x:String>One</x:String>
<x:String>Two</x:String>
<x:String>Three</x:String>
<x:String>Four</x:String>
<x:String>Five</x:String>
</Picker.Items>
</Picker>
<!--<Label Grid.Row="5" Grid.Column="0" Text="TextBox Setting" VerticalTextAlignment="Center" />
<RadioButton Grid.Row="5" Grid.Column="1" HorizontalOptions="EndAndExpand" />-->
@ -37,13 +49,11 @@
<!--<Label Grid.Row="5" Grid.Column="0" Text="TextBox Setting" VerticalTextAlignment="Center" />
<Stepper Grid.Row="5" Grid.Column="1" HorizontalOptions="EndAndExpand" MinimumWidthRequest="100" />-->
<Label Grid.Row="5" Grid.Column="0" Text="Time Setting" VerticalTextAlignment="Center" />
<TimePicker Grid.Row="5" Grid.Column="1" HorizontalOptions="EndAndExpand" Format="HH:mm" />
</Grid>
<Button x:Name="button1" Margin="0, 20, 0, 0" Text="Save" Clicked="button1_Clicked"></Button>
<Button x:Name="button1" Margin="0, 20, 0, 0" Text="Save"></Button>
<views:LoadingIndicatorView x:Name="loadingIndicatorView1" Margin="0, 20, 0, 0" />
</StackLayout>
</ScrollView>

View File

@ -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)
//{
//}
});
}
}
}