Adding a PivotItem-derived class to a Pivot throws an exception

0

UWP, C#. I have a Pivot control. I'm trying to add a PivotItem-derived object to the Items collection. It throws a COMException:

No installed components were detected.

Cannot apply a Style with TargetType 'Windows.UI.Xaml.Controls.PivotItem' to an object of type 'Windows.UI.Xaml.Controls.ContentControl'.

The HRESULT is 0x800f1000, if that matters.

Reproduces on a clean example. This works:

MyPivot.Items.Insert(1, new PivotItem() { Header = "Boo" });

And this exceptions:

MyPivot.Items.Insert(1, new BooItem() { Header = "Boo" });

Where BooItem is defined as

public class BooItem : PivotItem
{
    public BooItem(){}
}

If specified via XAML, the PivotItem-derived classes work in a Pivot. My other items in the app are all like that.

The whole codebase is a port from Windows Phone Silverlight (targeting WP8.0), it worked over there. Now, UWP is not quite the same, but still, you'd expect...

EDIT: you can add the item as invisible. The header will still appear, but the contents will be, well, invisible. Trying to make it visible causes the same exception.

There's an interesting line in the Pivot docs:

Any item you add to the Pivot that is not explicitly a PivotItem is implicitly wrapped in a PivotItem.

Do objects that derive PivotItem count?


EDIT2: reproduced on a blank project. The main page XAML goes:

<Page
    x:Class="UWTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Pivot x:Name="ThePivot">
        <PivotItem Header="Foo">
            <StackPanel>
                <Button Content="Go" Click="OnGo"/>
            </StackPanel>
        </PivotItem>
        
    </Pivot>
</Page>

The code behind goes:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace UWTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void OnGo(object sender, RoutedEventArgs e)
        {
            ThePivot.Items.Insert(1, new BooItem() { Header = "Boo" });
        }
    }
    public class BooItem : PivotItem
    {
        public BooItem() { }
    }
}

Visual Studio 2015 update 3, target version 14393, min version the same.

EDIT: reproduced on another machine. Visual Studio 2015, brand new Unversal Windows C# project. Windows 10 Pro, v 1803 (build 17134.648).

windows-runtime
pivot
win-universal-app
asked on Stack Overflow Mar 7, 2019 by Seva Alekseyev • edited Jun 20, 2020 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0