0
<!--MainWindow.xaml-->

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
// MainWindow.xaml.cs

namespace TestApp
{
    internal sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
// App.xaml.cs

namespace TestApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            var mainWindow = new MainWindow();
            mainWindow.Content = new SubControl();
            mainWindow.Activate();
        }
    }
}
// SubControl.cs

namespace TestApp.Controls
{
    internal class SubControl : NavigationView
    {
    }
}

The above code causes exception when SubControl is loaded.

e.Exception: {"No installed components were detected. (0x800F1000)"}, System.Exception {System.Runtime.InteropServices.COMException}

e.Message: "Cannot apply a Style with TargetType 'Microsoft.UI.Xaml.Controls.NavigationView' to an object of type 'Microsoft.UI.Xaml.Controls.ContentControl'."

If SubControl is created from xaml or inherits Button, TextBox, CheckBox, etc., it will function normally.

What I'm trying to do is create a custom control that inherits NavigationView. In order to implement the part that I want to customize, I think it would be better to do subclassing than applying Behavior.

An exception is thrown when applying breakpoints to "mainWindow.Content = new SubControl();".

If you do not apply breakpoints, there are no exceptions, but the control is not displayed.

  • Can't reproduce this. Can you check if some detail is missing from your question? – Andrew KeepCoding May 21 '23 at 05:11
  • @AndrewKeepCoding I apologize for my mistake. I've added more content, so please check it out. – appletree5641 May 21 '23 at 06:21
  • I still cannot reproduce it except my custom control is `public sealed class`. It is adapted from [Build XAML controls with C#](https://learn.microsoft.com/en-us/windows/apps/winui/winui3/xaml-templated-controls-csharp-winui-3). – YangXiaoPo-MSFT May 24 '23 at 08:53
  • @YangXiaoPo-MSFT I've tried again now and it seems like the controls are not showing, but exceptions only happen once or twice out of 10 attempts. It's a very strange thing. – appletree5641 May 24 '23 at 12:23

2 Answers2

1

You get the same behavior with another controls that doesn't have a Default____Style (e.g. DefaultButtonStyle).

The only workaround that I can come up for the moment is to bring the style from the generic.xaml and change the TargetType to SubControl.

<Style TargetType="local:SubControl">
    <Setter Property="PaneToggleButtonStyle" Value="{StaticResource PaneToggleButtonStyle}" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="CompactPaneLength" Value="{ThemeResource NavigationViewCompactPaneLength}" />
    <Setter Property="CornerRadius" Value="{ThemeResource OverlayCornerRadius}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:SubControl">
:
:
:
</Style>
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
1

@AndrewKeepCoding was trying to resolve Control.DefaultStyleKey. With the simplest case, you can use the NavigationView style. Just add a new custom control and replace the following code.

// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace App2CSharp
{
    public sealed class CustomControl1 : NavigationView
    {
        public CustomControl1()
        {
            this.DefaultStyleKey = typeof(NavigationView);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }
}

enter image description here

YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22
  • If I modify the file Themes/Generic.xaml as below and comment `` out, it will not work. ``` ``` If I change the DefaultStyleKey, the Style in Generic.xaml doesn't seem to play any role, can you tell me why the difference is happening? – appletree5641 May 25 '23 at 04:54
  • @appletree5641 the following is my assumption. It seems each control must have its own style because WindowsAPPSDK will check it although the style is not in use. You can override OnApplyTemplate to verify. – YangXiaoPo-MSFT May 25 '23 at 06:59