From f62d319fc9d5aeb6aef8bb175342355e31c5c8c3 Mon Sep 17 00:00:00 2001 From: wenfengcheng Date: Sat, 25 Apr 2020 18:45:01 +0800 Subject: [PATCH 1/3] redesign with shell --- src/TravelMonkey/AppShell.xaml | 23 +++++++++++++++++++ src/TravelMonkey/AppShell.xaml.cs | 14 +++++++++++ src/TravelMonkey/Views/AddPicturePage.xaml | 11 ++++++++- src/TravelMonkey/Views/AddPicturePage.xaml.cs | 5 ++-- src/TravelMonkey/Views/MainPage.xaml | 11 ++++++++- src/TravelMonkey/Views/MainPage.xaml.cs | 7 +++--- src/TravelMonkey/Views/SplashScreen.xaml.cs | 3 ++- .../Views/TranslationResultPage.xaml | 12 +++++++++- .../Views/TranslationResultPage.xaml.cs | 3 ++- 9 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 src/TravelMonkey/AppShell.xaml create mode 100644 src/TravelMonkey/AppShell.xaml.cs diff --git a/src/TravelMonkey/AppShell.xaml b/src/TravelMonkey/AppShell.xaml new file mode 100644 index 0000000..18cb98c --- /dev/null +++ b/src/TravelMonkey/AppShell.xaml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/src/TravelMonkey/AppShell.xaml.cs b/src/TravelMonkey/AppShell.xaml.cs new file mode 100644 index 0000000..d3a874d --- /dev/null +++ b/src/TravelMonkey/AppShell.xaml.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using Xamarin.Forms; + +namespace TravelMonkey +{ + public partial class AppShell : Shell + { + public AppShell() + { + InitializeComponent(); + } + } +} diff --git a/src/TravelMonkey/Views/AddPicturePage.xaml b/src/TravelMonkey/Views/AddPicturePage.xaml index 8a6f98a..10ab08e 100644 --- a/src/TravelMonkey/Views/AddPicturePage.xaml +++ b/src/TravelMonkey/Views/AddPicturePage.xaml @@ -1,5 +1,14 @@  - + diff --git a/src/TravelMonkey/Views/AddPicturePage.xaml.cs b/src/TravelMonkey/Views/AddPicturePage.xaml.cs index 3ea8d36..c18689c 100644 --- a/src/TravelMonkey/Views/AddPicturePage.xaml.cs +++ b/src/TravelMonkey/Views/AddPicturePage.xaml.cs @@ -17,9 +17,10 @@ public AddPicturePage() MessagingCenter.Subscribe(this, Constants.PictureFailedMessage, async (vm) => await DisplayAlert("Uh-oh!", "Can you hand me my glasses? Something went wrong while analyzing this image", "OK")); } - private void Button_Clicked(object sender, EventArgs e) + private async void Button_Clicked(object sender, EventArgs e) { - Navigation.PopModalAsync(); + //Navigation.PopModalAsync(); + await Shell.Current.GoToAsync("//main"); } } } \ No newline at end of file diff --git a/src/TravelMonkey/Views/MainPage.xaml b/src/TravelMonkey/Views/MainPage.xaml index ee8eb69..6515523 100644 --- a/src/TravelMonkey/Views/MainPage.xaml +++ b/src/TravelMonkey/Views/MainPage.xaml @@ -1,5 +1,14 @@  - + diff --git a/src/TravelMonkey/Views/MainPage.xaml.cs b/src/TravelMonkey/Views/MainPage.xaml.cs index 8b2b41f..5c823f7 100644 --- a/src/TravelMonkey/Views/MainPage.xaml.cs +++ b/src/TravelMonkey/Views/MainPage.xaml.cs @@ -33,7 +33,8 @@ protected override void OnDisappearing() private async void AddNewPicture_Tapped(object sender, EventArgs e) { - await Navigation.PushModalAsync(new AddPicturePage()); + //await Navigation.PushModalAsync(new AddPicturePage()); + await Shell.Current.GoToAsync("//picture"); } private async void Entry_Completed(object sender, EventArgs e) @@ -43,8 +44,8 @@ private async void Entry_Completed(object sender, EventArgs e) await DisplayAlert("No text entered", "You didn't enter any text!", "OK"); return; } - - await Navigation.PushModalAsync(new TranslationResultPage(TranslateTextEntry.Text)); + await Shell.Current.GoToAsync("//translation"); + //await Navigation.PushModalAsync(new TranslationResultPage(TranslateTextEntry.Text)); TranslateTextEntry.Text = ""; } } diff --git a/src/TravelMonkey/Views/SplashScreen.xaml.cs b/src/TravelMonkey/Views/SplashScreen.xaml.cs index dcb71c3..488df00 100644 --- a/src/TravelMonkey/Views/SplashScreen.xaml.cs +++ b/src/TravelMonkey/Views/SplashScreen.xaml.cs @@ -61,7 +61,8 @@ private void NavigateToMainPage() { MainThread.BeginInvokeOnMainThread(() => { - Application.Current.MainPage = new MainPage(); + Application.Current.MainPage = new AppShell(); + Shell.Current.GoToAsync("//main"); }); } } diff --git a/src/TravelMonkey/Views/TranslationResultPage.xaml b/src/TravelMonkey/Views/TranslationResultPage.xaml index 0fcf116..81128a8 100644 --- a/src/TravelMonkey/Views/TranslationResultPage.xaml +++ b/src/TravelMonkey/Views/TranslationResultPage.xaml @@ -1,5 +1,15 @@  - + diff --git a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs index bf8553c..a4ce656 100644 --- a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs +++ b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs @@ -27,7 +27,8 @@ public TranslationResultPage(string inputText) private async void Button_Clicked(object sender, EventArgs e) { - await Navigation.PopModalAsync(); + //await Navigation.PopModalAsync(); + await Shell.Current.GoToAsync("//main"); } } } \ No newline at end of file From 1149d4722015b05b2ef0121284058f7497e8938a Mon Sep 17 00:00:00 2001 From: wenfengcheng Date: Sat, 25 Apr 2020 19:17:32 +0800 Subject: [PATCH 2/3] fix compile error and safe area bug --- .../Effects/SafeAreaPaddingEffect.cs | 38 +++++++++++++------ .../Effects/SafeAreaPaddingEffect.cs | 2 + src/TravelMonkey/Views/MainPage.xaml | 6 ++- src/TravelMonkey/Views/MainPage.xaml.cs | 2 +- .../Views/TranslationResultPage.xaml.cs | 16 +++++--- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/TravelMonkey.iOS/Effects/SafeAreaPaddingEffect.cs b/src/TravelMonkey.iOS/Effects/SafeAreaPaddingEffect.cs index c668630..8b2bc60 100644 --- a/src/TravelMonkey.iOS/Effects/SafeAreaPaddingEffect.cs +++ b/src/TravelMonkey.iOS/Effects/SafeAreaPaddingEffect.cs @@ -1,4 +1,6 @@ -using TravelMonkey.iOS.Effects; +using System.Linq; +using TravelMonkey.Effects; +using TravelMonkey.iOS.Effects; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; @@ -12,22 +14,36 @@ public class SafeAreaPaddingEffect_iOS : PlatformEffect Thickness _padding; protected override void OnAttached() { - if (Element is Layout element) + try { - if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) + var effect = (SafeAreaPaddingEffect)Element.Effects.FirstOrDefault(e => e is SafeAreaPaddingEffect); + if (effect != null && Element is Layout element) { - _padding = element.Padding; - var insets = UIApplication.SharedApplication.Windows[0].SafeAreaInsets; - - if (insets.Top > 0) + if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) { - element.Padding = new Thickness(_padding.Left + insets.Left, _padding.Top + insets.Top, _padding.Right + insets.Right, _padding.Bottom + insets.Bottom); - return; + _padding = element.Padding; + var insets = UIApplication.SharedApplication.Windows[0].SafeAreaInsets; + + if (insets.Top > 0) + { + if (!effect.Revert) + element.Padding = new Thickness(_padding.Left + insets.Left, _padding.Top + insets.Top, _padding.Right + insets.Right, _padding.Bottom + insets.Bottom); + else + element.Padding = new Thickness(_padding.Left - insets.Left, _padding.Top - insets.Top, _padding.Right - insets.Right, _padding.Bottom - insets.Bottom); + return; + } } + if (!effect.Revert) + element.Padding = new Thickness(_padding.Left, _padding.Top + 20, _padding.Right, _padding.Bottom + 20); + else + element.Padding = new Thickness(_padding.Left, _padding.Top - 20, _padding.Right, _padding.Bottom - 20); } - - element.Padding = new Thickness(_padding.Left, _padding.Top + 20, _padding.Right, _padding.Bottom + 20); } + catch (System.Exception ex) + { + System.Console.WriteLine("Cannot set property on attached control. Error: ", ex.Message); + } + } protected override void OnDetached() diff --git a/src/TravelMonkey/Effects/SafeAreaPaddingEffect.cs b/src/TravelMonkey/Effects/SafeAreaPaddingEffect.cs index 4a37075..e2a5fd7 100644 --- a/src/TravelMonkey/Effects/SafeAreaPaddingEffect.cs +++ b/src/TravelMonkey/Effects/SafeAreaPaddingEffect.cs @@ -4,6 +4,8 @@ namespace TravelMonkey.Effects { public class SafeAreaPaddingEffect : RoutingEffect { + public bool Revert { get; set; } + public SafeAreaPaddingEffect() : base("TravelMonkey.SafeAreaPaddingEffect") { } diff --git a/src/TravelMonkey/Views/MainPage.xaml b/src/TravelMonkey/Views/MainPage.xaml index 6515523..41e98f1 100644 --- a/src/TravelMonkey/Views/MainPage.xaml +++ b/src/TravelMonkey/Views/MainPage.xaml @@ -4,12 +4,16 @@ xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView" + xmlns:effect="clr-namespace:TravelMonkey.Effects" xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" mc:Ignorable="d" Shell.NavBarIsVisible="False" ios:Page.UseSafeArea="False" x:Class="TravelMonkey.Views.MainPage"> + + + @@ -31,7 +35,7 @@ - + diff --git a/src/TravelMonkey/Views/MainPage.xaml.cs b/src/TravelMonkey/Views/MainPage.xaml.cs index 5c823f7..f6c143c 100644 --- a/src/TravelMonkey/Views/MainPage.xaml.cs +++ b/src/TravelMonkey/Views/MainPage.xaml.cs @@ -44,7 +44,7 @@ private async void Entry_Completed(object sender, EventArgs e) await DisplayAlert("No text entered", "You didn't enter any text!", "OK"); return; } - await Shell.Current.GoToAsync("//translation"); + await Shell.Current.GoToAsync($"//translation?inputText={TranslateTextEntry.Text}"); //await Navigation.PushModalAsync(new TranslationResultPage(TranslateTextEntry.Text)); TranslateTextEntry.Text = ""; } diff --git a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs index a4ce656..5cc0fa2 100644 --- a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs +++ b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs @@ -4,12 +4,22 @@ namespace TravelMonkey.Views { + [QueryProperty("InputText", "inputText")] public partial class TranslationResultPage : ContentPage { private readonly TranslateResultPageViewModel _translateResultPageViewModel = new TranslateResultPageViewModel(); - public TranslationResultPage(string inputText) + public string InputText + { + set + { + _translateResultPageViewModel.InputText = value; + BindingContext = _translateResultPageViewModel; + } + } + + public TranslationResultPage() { InitializeComponent(); @@ -19,10 +29,6 @@ public TranslationResultPage(string inputText) { await DisplayAlert("Whoops!", "We lost our dictionary, something went wrong while translating", "OK"); }); - - _translateResultPageViewModel.InputText = inputText; - - BindingContext = _translateResultPageViewModel; } private async void Button_Clicked(object sender, EventArgs e) From fe9be2e081f36c6332a7b2499b3e56eaea1e0ded Mon Sep 17 00:00:00 2001 From: wenfengcheng Date: Sat, 25 Apr 2020 19:58:20 +0800 Subject: [PATCH 3/3] optimize code --- src/TravelMonkey/Views/AddPicturePage.xaml | 2 -- src/TravelMonkey/Views/AddPicturePage.xaml.cs | 4 ++-- src/TravelMonkey/Views/MainPage.xaml.cs | 2 +- .../Views/TranslationResultPage.xaml.cs | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/TravelMonkey/Views/AddPicturePage.xaml b/src/TravelMonkey/Views/AddPicturePage.xaml index 10ab08e..971f965 100644 --- a/src/TravelMonkey/Views/AddPicturePage.xaml +++ b/src/TravelMonkey/Views/AddPicturePage.xaml @@ -3,11 +3,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TravelMonkey.Views.AddPicturePage" xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView" - xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" xmlns:effect="clr-namespace:TravelMonkey.Effects" Shell.PresentationMode="ModalAnimated" Shell.NavBarIsVisible="False" - ios:Page.UseSafeArea="False" Title="Add Picture"> diff --git a/src/TravelMonkey/Views/AddPicturePage.xaml.cs b/src/TravelMonkey/Views/AddPicturePage.xaml.cs index c18689c..0222d10 100644 --- a/src/TravelMonkey/Views/AddPicturePage.xaml.cs +++ b/src/TravelMonkey/Views/AddPicturePage.xaml.cs @@ -12,14 +12,14 @@ public AddPicturePage() BindingContext = new AddPicturePageViewModel(); - MessagingCenter.Subscribe(this, Constants.PictureAddedMessage, async (vm) => await Navigation.PopModalAsync(true)); + MessagingCenter.Subscribe(this, Constants.PictureAddedMessage, async (vm) => await Shell.Current.GoToAsync("//main")); MessagingCenter.Subscribe(this, Constants.PictureFailedMessage, async (vm) => await DisplayAlert("Uh-oh!", "Can you hand me my glasses? Something went wrong while analyzing this image", "OK")); } private async void Button_Clicked(object sender, EventArgs e) { - //Navigation.PopModalAsync(); + //await Shell.Current.Navigation.PopModalAsync(); await Shell.Current.GoToAsync("//main"); } } diff --git a/src/TravelMonkey/Views/MainPage.xaml.cs b/src/TravelMonkey/Views/MainPage.xaml.cs index f6c143c..594c8e3 100644 --- a/src/TravelMonkey/Views/MainPage.xaml.cs +++ b/src/TravelMonkey/Views/MainPage.xaml.cs @@ -45,7 +45,7 @@ private async void Entry_Completed(object sender, EventArgs e) return; } await Shell.Current.GoToAsync($"//translation?inputText={TranslateTextEntry.Text}"); - //await Navigation.PushModalAsync(new TranslationResultPage(TranslateTextEntry.Text)); + //await Shell.Current.Navigation.PushModalAsync(new TranslationResultPage(TranslateTextEntry.Text)); TranslateTextEntry.Text = ""; } } diff --git a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs index 5cc0fa2..b19d99e 100644 --- a/src/TravelMonkey/Views/TranslationResultPage.xaml.cs +++ b/src/TravelMonkey/Views/TranslationResultPage.xaml.cs @@ -31,6 +31,24 @@ public TranslationResultPage() }); } + public TranslationResultPage(string inputText) + { + InitializeComponent(); + + MessagingCenter.Subscribe(this, + Constants.TranslationFailedMessage, + async (s) => + { + await DisplayAlert("Whoops!", "We lost our dictionary, something went wrong while translating", "OK"); + }); + + + _translateResultPageViewModel.InputText = inputText; + + BindingContext = _translateResultPageViewModel; + + } + private async void Button_Clicked(object sender, EventArgs e) { //await Navigation.PopModalAsync();