diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml new file mode 100644 index 00000000000..5922a5fe235 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml.cs new file mode 100644 index 00000000000..43b4093ff8f --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7671.xaml.cs @@ -0,0 +1,21 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +using Xamarin.Forms.Xaml; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.None, 7671, "Check ControlTemplate rendering", PlatformAffected.WPF)] +#if APP + [XamlCompilation(XamlCompilationOptions.Compile)] +#endif + public partial class Issue7671 : ContentPage + { + public Issue7671() + { +#if APP + InitializeComponent(); +#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 1f2b93d033b..3197fc6f76d 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 @@ -287,6 +287,10 @@ Code + + Issue7671.xaml + Code + Code @@ -2714,6 +2718,12 @@ MSBuild:UpdateDesignTimeXaml + + + Designer + MSBuild:Compile + + Designer diff --git a/Xamarin.Forms.Platform.WPF/Controls/FormsContentPage.cs b/Xamarin.Forms.Platform.WPF/Controls/FormsContentPage.cs index be680e3d9c8..4e3c372aab4 100644 --- a/Xamarin.Forms.Platform.WPF/Controls/FormsContentPage.cs +++ b/Xamarin.Forms.Platform.WPF/Controls/FormsContentPage.cs @@ -1,10 +1,18 @@ -namespace Xamarin.Forms.Platform.WPF.Controls +using System; +using System.Windows; +using WpfSize = System.Windows.Size; + +namespace Xamarin.Forms.Platform.WPF.Controls { public class FormsContentPage : FormsPage { public FormsContentPage() { - this.DefaultStyleKey = typeof(FormsContentPage); + } + + static FormsContentPage() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(FormsContentPage), new FrameworkPropertyMetadata(typeof(FormsContentPage))); } } } diff --git a/Xamarin.Forms.Platform.WPF/FormsContentLoader.cs b/Xamarin.Forms.Platform.WPF/FormsContentLoader.cs index 3a27a89718d..a6627305dc3 100644 --- a/Xamarin.Forms.Platform.WPF/FormsContentLoader.cs +++ b/Xamarin.Forms.Platform.WPF/FormsContentLoader.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -57,7 +58,7 @@ private object CreateOrResizeContent(FrameworkElement parent, VisualElement visu var contentPage = visualElement as ContentPage; if (contentPage?.ControlTemplate != null) { - contentPage.Content?.Layout(actualRect); + (contentPage.LogicalChildren.OfType().FirstOrDefault() ?? contentPage.Content)?.Layout(actualRect); } else { diff --git a/Xamarin.Forms.Platform.WPF/Renderers/PageRenderer.cs b/Xamarin.Forms.Platform.WPF/Renderers/PageRenderer.cs index 47f1583df99..bb44e0ef02d 100644 --- a/Xamarin.Forms.Platform.WPF/Renderers/PageRenderer.cs +++ b/Xamarin.Forms.Platform.WPF/Renderers/PageRenderer.cs @@ -1,49 +1,49 @@ using System.ComponentModel; +using System.Linq; using Xamarin.Forms.Platform.WPF.Controls; namespace Xamarin.Forms.Platform.WPF { public class PageRenderer : VisualPageRenderer { - VisualElement _currentView; + VisualElement _currentView = null; protected override void OnElementChanged(ElementChangedEventArgs e) { if (e.NewElement != null) { - if (Control == null) // construct and SetNativeControl and suscribe control event - { + if (Control == null) SetNativeControl(new FormsContentPage()); - } - // Update control property UpdateContent(); } - base.OnElementChanged(e); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); - - if (e.PropertyName == ContentPage.ContentProperty.PropertyName) + + if (e.PropertyName == ContentPage.ContentProperty.PropertyName + || e.PropertyName == TemplatedPage.ControlTemplateProperty.PropertyName) + { UpdateContent(); + } } void UpdateContent() { - ContentPage page = Element as ContentPage; - if (page != null) + if (Element is ContentPage page) { - if (_currentView != null) + if (_currentView != null) // destroy current view { - _currentView.Cleanup(); // cleanup old view + _currentView?.Cleanup(); + _currentView = null; } - _currentView = page.Content; + _currentView = page.LogicalChildren.OfType().FirstOrDefault() ?? page.Content; Control.Content = _currentView != null ? Platform.GetOrCreateRenderer(_currentView).GetNativeElement() : null; } } } -} +} \ No newline at end of file