microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

TabView throw a 'System.ArgumentException' while removing an item from it's TabItemsSource

Open Guillermo-Santos opened this issue 3 years ago • 0 comments

Describe the bug

I created a WinUI3 app using the template selector and added a tabview to work with files in the main page, i did this by making the tabitemsource a ObservableCollection of <Document>, the thing is that if i navigate to other page and then return to the main page, if i decide to open two or more tabs and then close one of the unselected tabs, the app Exception thrown: 'System.ArgumentException' in WinRT.Runtime.dll WinRT information: The parameter is incorrect.

I think this is a bug, since i can open and close as many tabs as i want when i am on the main page (selected tabs or not) without issue, but if i navigate to other page an then return, closing new opened tabs or changing the selected tab and then closing it, throw this error if unselected.

Steps to reproduce the bug

  1. Open app.
  2. Open a directory.
  3. Click settings icon to go to settings page.
  4. Click settings icon to go back to main page.
  5. Open some documents
  6. Close unselected document.

Expected behavior

The document should close successfully but for some reason it does not.

Screenshots

  1. Open app.
  2. Open a directory. Screenshot 2022-08-15 190135
  3. Click settings icon to go to settings page. Screenshot 2022-08-15 191137
  4. Click settings icon to go back to main page. Screenshot (52)
  5. Open some documents Screenshot (55)
  6. Close unselected document. Screenshot 2022-08-15 183135

NuGet package version

WinUI 3 - Windows App SDK 1.1.4

Windows app type

  • [ ] UWP
  • [ ] Win32

Device form factor

Desktop

Windows version

Windows 11 (22H2): Build 22000

Additional context

Code:

TabView Control

<TabView x:Name="codeTabs"
         IsAddTabButtonVisible="False"
         CloseButtonOverlayMode="OnPointerOver"
         TabCloseRequested="TabView_TabCloseRequested"
         HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch"
         TabItemsSource="{x:Bind ViewModel.Files}"
         SelectedItem="{x:Bind ViewModel.SelectedDocument, Mode=TwoWay}">
    <TabView.TabItemTemplate>
        <DataTemplate x:DataType="models:Document">
            <TabViewItem Header="{x:Bind FileName}">
                <TabViewItem.Content>
                    <local:FilePage FileName="{x:Bind Path}" Text="{x:Bind Text}"/>
                </TabViewItem.Content>
            </TabViewItem>
        </DataTemplate>
    </TabView.TabItemTemplate>
</TabView>

TabView_TabCloseRequested

    private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
    {
        SparkFileService.Instance.CloseFile(args.Item as Document);
    }

CloseFile

    public void CloseFile(Document document)
    {
        var openDocument = OpenDocuments.Where(d => d.Path == document.Path).FirstOrDefault();
        if (openDocument != null)
        {
            OpenDocuments.Remove(openDocument);
        }
    }

Fix

    private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
    {
        var document = args.Item as Document;
        var selectedDocument = ViewModel.SelectedDocument;
        ViewModel.SelectedDocument = document;
        SparkFileService.Instance.CloseFile(document);
        ViewModel.SelectedDocument = selectedDocument;
    }

This is a fix to the problem, since this bug only reproduce with unselected tabs, the solution i found was making the tab to close the selected tab and then closing it.

Guillermo-Santos avatar Aug 15 '22 23:08 Guillermo-Santos