I am writing an app using C++/winrt, on Windows 11, and using the new WinUI 3.0. The app has a TabView control, and each tab represents a document. The TabView control works well, but when you try to delete the tab that is selected... the program crashes. All the other non-selected tabs can be deleted easily. I have tried everything to unselect the selected tab, before deleting it, but nothing works. Has anyone else encountered this issue?
I tried unselecting the TabViewItem that represents the tab, but I can't unselect it.
void MainWindow::TabView_TabCloseRequested(::winrt::Microsoft::UI::Xaml::Controls::TabView const& sender, ::winrt::Microsoft::UI::Xaml::Controls::TabViewTabCloseRequestedEventArgs const& args)
{
uint32_t indexOf;
if (sender.TabItems().IndexOf(args.Tab(), indexOf))
{
sender.TabItems().RemoveAt(indexOf);
}
}
Here's the Xaml page...
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
<!-- Licensed under the MIT License. -->
<Window
x:Class="Vectraset.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Vectraset"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d">
<RelativePanel>
<StackPanel x:Name="menu" Orientation="Horizontal" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignTopWithPanel="True">
<MenuBar>
<MenuBarItem Title="File">
<MenuFlyoutItem Text="New"/>
<MenuFlyoutItem Text="Open..."/>
<MenuFlyoutItem Text="Save"/>
<MenuFlyoutItem Text="Exit"/>
</MenuBarItem>
<MenuBarItem Title="Edit">
<MenuFlyoutItem Text="Undo"/>
<MenuFlyoutItem Text="Cut"/>
<MenuFlyoutItem Text="Copy"/>
<MenuFlyoutItem Text="Paste"/>
</MenuBarItem>
<MenuBarItem Title="Help">
<MenuFlyoutItem Text="About"/>
</MenuBarItem>
</MenuBar>
</StackPanel>
<StackPanel x:Name="stack2" Orientation="Vertical" RelativePanel.AlignLeftWithPanel="True" RelativePanel.Below="menu">
<TabView x:Name="myTab" SelectionChanged="TabView_SelectionChanged" AddTabButtonClick="TabView_AddButtonClick" TabCloseRequested="TabView_TabCloseRequested" Loaded="TabView_Loaded"/>
<Canvas x:Name="myStack" Width="2500" Height="1250" HorizontalAlignment="Left" VerticalAlignment="Top">
<canvas:CanvasControl x:Name="D2D_CanvasControl" Canvas.Left="0" Canvas.Top="0" Width="2500" Height="1250" HorizontalAlignment="Right" VerticalAlignment="Bottom" Draw="CanvasControl_Draw" ClearColor="CornflowerBlue">
</canvas:CanvasControl>
<local:PanelPart x:Name="myPanelPart" Canvas.Left="100" Canvas.Top="100" PointerPressed="PanelPart_PointerPressed" PointerReleased="PanelPart_PointerReleased" PointerMoved="PanelPart_PointerMoved" Width="75" Height="200" Background="Blue" Label="PanelPart"/>
</Canvas>
</StackPanel>
<AppBar x:Name="bottomAppBar" Padding="0,0,0,0" MinWidth="100" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" AppBar.IsSticky="True">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<AppBarButton Icon="Add" Label="Add Part" Click="AddPartButton_Click"/>
<AppBarButton AutomationProperties.Name="Sample Button"
AutomationProperties.AutomationId="SampleAppBarButton"
Click="AppBarButton_Click"/>
</StackPanel>
</AppBar>
</RelativePanel>
</Window>
Here is the code I've been using to add the TabViewItem...
// CPP
void MainWindow::TabView_AddButtonClick(::winrt::Microsoft::UI::Xaml::Controls::TabView const& sender, ::winrt::Windows::Foundation::IInspectable const& e)
{
myTab().TabItems().Append(CreateNewTab(L"Default Title"));
}
Microsoft::UI::Xaml::Controls::TabViewItem MainWindow::CreateNewTab(hstring title)
{
TabViewItem newItem;
newItem.Header(box_value(title));
return newItem;
}