Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentDialog: 'System.ObjectDisposedException' occurred in WinRT.Runtime.dll but was not handled in user code Cannot access a disposed object. #9173

Open
GSerjo opened this issue Dec 18, 2023 · 1 comment
Labels
area-Dialogs bug Something isn't working team-Reach Issue for the Reach team

Comments

@GSerjo
Copy link

GSerjo commented Dec 18, 2023

Describe the bug

The frame object has been disposed after opening/closing the ContentDialog several times.

<?xml version="1.0" encoding="utf-8"?>
<ContentDialog
    x:Class="Client.Dialogs.DocumentDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Client.Dialogs"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <NavigationView
            IsBackEnabled="{x:Bind ContentFrame.CanGoBack, Mode=OneWay}"
            BackRequested="NavigationView_OnBackRequested"
            PaneDisplayMode="LeftMinimal"
            IsPaneToggleButtonVisible="False"
            IsSettingsVisible="False"
            IsTabStop="False"
            IsTitleBarAutoPaddingEnabled="True">
            <ScrollViewer>
                <Frame x:Name="ContentFrame" /> // <-- Disposed after several open/close
            </ScrollViewer>
        </NavigationView>
        <StackPanel Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal" Spacing="20">
            <Button  Width="100"
                     Content="Ok"
                     Margin="0, 24, 0, 0"
                     Visibility="{x:Bind ContentFrame.CanGoBack, Mode=OneWay}"
                     Style="{StaticResource AccentButtonStyle}"
                     Click="Ok_OnClick"/>
            <Button  Width="100" Content="Cancel" Margin="0, 24, 0, 0" Click="Cancel_OnClick"/>
        </StackPanel>
    </Grid>
</ContentDialog>
    public sealed partial class DocumentDialog : ContentDialog
    {

        private readonly DispatcherQueue _dispatcherQueue;

        public DocumentDialog()
        {
            this.InitializeComponent();

            DataContext = new AddDocumentDialogViewModel();
            _dispatcherQueue = DispatcherQueue.GetForCurrentThread();

            Navigate(typeof(AddDocumentTypeSelectorPage));
        }

        public void Navigate(Type pageType, object? targetPageArguments = null)
        {
            var args = new NavigationAddDocumentPageArgs
            {
                Parameter = targetPageArguments
            };
            ContentFrame.Navigate(pageType, args, new SuppressNavigationTransitionInfo()); // <-- happens here
        }

        private void Cancel_OnClick(object sender, RoutedEventArgs e)
        {
            Hide();
        }

        private void Ok_OnClick(object sender, RoutedEventArgs e)
        {
            var page = ContentFrame.Content as AddDocumentTypePage;
            DocumentViewModel? viewModel = page?.ViewModel;
            viewModel?.Save();
            Hide();
        }

}

Steps to reproduce the bug

  1. Create a custom dialog with NavigationView and two pages
  2. Open/close navigate between pages
  3. wait 1-2 min
  4. Open dialog
  5. Navigate to the second page
  6. ObjectDisposedException

Expected behavior

No ObjectDisposedException

Screenshots

image

NuGet package version

None

Windows version

No response

Additional context

System.ObjectDisposedException
  HResult=0x80131622
  Message=Cannot access a disposed object.
Object name: 'ObjectReference'.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.IObjectReference.ThrowIfDisposed()
   at ABI.Microsoft.UI.Xaml.Controls.IFrameMethods.Navigate(IObjectReference _obj, Type sourcePageType, Object parameter, NavigationTransitionInfo infoOverride)
   at Client.Dialogs.AddDocumentDialog.Navigate(Type pageType, Object targetPageArguments) in DocumentDialog.xaml.cs:line 37
   at Client.Dialogs.AddDocumentDialog.<>c__DisplayClass4_0.<OnDocumentTypeSelected>b__0() in DocumentDialog.xaml.cs:line 55
   at ABI.Microsoft.UI.Dispatching.DispatcherQueueHandler.Do_Abi_Invoke(IntPtr thisPtr)
@GSerjo GSerjo added the bug Something isn't working label Dec 18, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Dec 18, 2023
Copy link

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@bpulliam bpulliam added area-Dialogs team-Reach Issue for the Reach team and removed needs-triage Issue needs to be triaged by the area owners labels Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Dialogs bug Something isn't working team-Reach Issue for the Reach team
Projects
None yet
Development

No branches or pull requests

2 participants