From dfa2606a302eb1c30134daf2b362fe71131ff6d4 Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Wed, 27 Jun 2018 19:01:26 +0100 Subject: [PATCH] [Android] Add native ctor to PageContainer (#3107) fixes #2740 * [Controls] Add repo and uitest for issue 2740 * [Android] Add native ctor to PageContainer --- .../Issue2740.cs | 78 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../Renderers/PageContainer.cs | 6 ++ 3 files changed, 85 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2740.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2740.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2740.cs new file mode 100644 index 000000000..62bfabb3e --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2740.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 2740, "System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.PageContainer from native handle", PlatformAffected.Android)] + public class Issue2740 : TestMasterDetailPage // or TestMasterDetailPage, etc ... + { + protected override void Init() + { + var page = new AddressListView(); + + // Initialize ui here instead of ctor + Master = new ContentPage + { + Content = new Label + { + Text = "Click a item on the left then the toolbar item switch" + }, + Title = "2740" + }; + Detail = new NavigationPage(page); + } + + public partial class AddressListView : ContentPage + { + + public AddressListView() + { + var listview = new ListView(); + listview.ItemsSource = new List { "1", "2" }; + listview.ItemTapped += OnItemTapped; + Content = listview; + Title = "Unit List"; + } + + public async void OnItemTapped(object sender, ItemTappedEventArgs e) + { + var p = new UnitViolationView(); + await Navigation.PushAsync(p); + } + } + + public partial class UnitViolationView : ContentPage + { + public UnitViolationView() + { + ToolbarItems.Add(new ToolbarItem("Switch", null, MapAddressSwitch) { AutomationId = "Switch" }); + } + + async void MapAddressSwitch() + { + await Navigation.PopAsync(false); + (Application.Current.MainPage as MasterDetailPage).Detail = new NavigationPage(new AddressListView()); + } + } + +#if UITEST + [Test] + public void Issue2740Test () + { + RunningApp.WaitForElement (q => q.Marked ("1")); + RunningApp.Tap (q => q.Marked ("1")); + RunningApp.WaitForElement (q => q.Marked ("Switch")); + RunningApp.Tap (q => q.Marked ("Switch")); + RunningApp.WaitForElement (q => q.Marked ("1")); + } +#endif + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index d0b7a3652..9ae8cfa20 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -461,6 +461,7 @@ + diff --git a/Xamarin.Forms.Platform.Android/Renderers/PageContainer.cs b/Xamarin.Forms.Platform.Android/Renderers/PageContainer.cs index 435ee7cfb..083a34c94 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/PageContainer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/PageContainer.cs @@ -1,5 +1,7 @@ using Android.Content; +using Android.Runtime; using Android.Views; +using System; namespace Xamarin.Forms.Platform.Android { @@ -17,6 +19,10 @@ namespace Xamarin.Forms.Platform.Android public bool IsInFragment { get; set; } + protected PageContainer(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) + { + } + protected override void OnLayout(bool changed, int l, int t, int r, int b) { Child.UpdateLayout();