Version: .NET Core 3
Windows 10 x64
It works in .NET Framework 4.8
I have a WPF user control deriving from ContentControl. It's working fine in .NET Framework 4.8. In .NET Core 3, however, OnApplyTemplate never gets called. (MediaPlayerWpf inherits from ContentControl)
public class MpvMediaPlayer : MediaPlayerWpf
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// This never gets called.
}
}
Works for me in both 3.0 GA (3.0.0-rc1-19456-20) and an older preview 8 I had lying around (3.0.0-preview8-28376-27). I'm using this code and can put a breakpoint into OnApplyTemplate which gets hit correctly. Can you provide a reproduction example?
working code
```C#
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class MediaPlayerWpf : ContentControl
{
}
public class MpvMediaPlayer : MediaPlayerWpf
{
public override void OnApplyTemplate()
{
// breakpoint gets hit (contrary to issue description)
base.OnApplyTemplate();
}
}
}
```XAML
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:MpvMediaPlayer />
</Grid>
</Window>
Reproduced the problem. The line causing OnApplyTemplate to never trigger is DefaultStyleKeyProperty. Comment that line and it gets triggered
namespace EmergenceGuardian.MpvPlayerUI
{
public class MpvTest : Control
{
static MpvTest()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MpvTest), new FrameworkPropertyMetadata(typeof(MpvTest)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
}
}
It looks like it's somehow not finding or not binding the template. I put this in Themes\Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EmergenceGuardian.MpvPlayerUI">
<Style TargetType="{x:Type local:MpvTest}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MpvMediaPlayerHost}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Thats probably a duplicate of #1699 - Desktop Framework had Properties\AssemblyInfo.cs in the project template which contained a [assembly: ThemeInfo(...)] attribute which is required for generic.xaml to work. The .NET Core template is missing this, you have to add that attribute manually for now.
The updated application templates in .NET Core 3.1 and .NET Core 5.0 should now address this.
Most helpful comment
Thats probably a duplicate of #1699 - Desktop Framework had
Properties\AssemblyInfo.csin the project template which contained a[assembly: ThemeInfo(...)]attribute which is required forgeneric.xamlto work. The .NET Core template is missing this, you have to add that attribute manually for now.