Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] ContentPage.ControlTemplate gets not rendered in Wpf (#7671) #7728

Merged
merged 4 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Xamarin.Forms.Controls.Issues.Issue7671">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
<ContentPage.ControlTemplate>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition />
<RowDefinition Height="60" />
</Grid.RowDefinitions>

<Frame Grid.Row="0" BackgroundColor="Green">
<Label Text="I am the template." />
</Frame>
<ContentPresenter Grid.Row="1" />
<Frame Grid.Row="2" BackgroundColor="Red">
<Label Text="I am the template." />
</Frame>
</Grid>
</ControlTemplate>
</ContentPage.ControlTemplate>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue7519Xaml.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue7671.xaml.cs">
<DependentUpon>Issue7671.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue7700.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7758.xaml.cs">
<SubType>Code</SubType>
Expand Down Expand Up @@ -2714,6 +2718,12 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7671.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7048.xaml">
<SubType>Designer</SubType>
Expand Down
12 changes: 10 additions & 2 deletions Xamarin.Forms.Platform.WPF/Controls/FormsContentPage.cs
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using the FrameworkPropertyMetadata?

}

static FormsContentPage()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FormsContentPage), new FrameworkPropertyMetadata(typeof(FormsContentPage)));
}
}
}
3 changes: 2 additions & 1 deletion Xamarin.Forms.Platform.WPF/FormsContentLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -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<VisualElement>().FirstOrDefault() ?? contentPage.Content)?.Layout(actualRect);
}
else
{
Expand Down
28 changes: 14 additions & 14 deletions Xamarin.Forms.Platform.WPF/Renderers/PageRenderer.cs
Original file line number Diff line number Diff line change
@@ -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<Page, FormsContentPage>
{
VisualElement _currentView;
VisualElement _currentView = null;

protected override void OnElementChanged(ElementChangedEventArgs<Page> 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<VisualElement>().FirstOrDefault() ?? page.Content;
Control.Content = _currentView != null ? Platform.GetOrCreateRenderer(_currentView).GetNativeElement() : null;
}
}
}
}
}