Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
Unloaded="RichTextPreview_Unloaded"
mc:Ignorable="d">

<Border>
Expand Down
15 changes: 14 additions & 1 deletion src/Files.App/UserControls/FilePreviews/RichTextPreview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ public RichTextPreview(RichTextPreviewViewModel viewModel)

private void TextPreviewControl_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
TextPreviewControl.Document.LoadFromStream(Microsoft.UI.Text.TextSetOptions.FormatRtf, ViewModel.Stream);
// Defensive check to prevent loading if stream is null or disposed
if (ViewModel.Stream is not null)
{
TextPreviewControl.Document.LoadFromStream(Microsoft.UI.Text.TextSetOptions.FormatRtf, ViewModel.Stream);
}
}

private void RichTextPreview_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
// Clear the document to release resources
TextPreviewControl.Document.SetText(Microsoft.UI.Text.TextSetOptions.None, string.Empty);

// Call the ViewModel's unload handler to dispose the stream
ViewModel.PreviewControlBase_Unloaded(sender, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Files.App.ViewModels.Properties;
using Microsoft.UI.Xaml;
using Windows.Storage.Streams;

namespace Files.App.ViewModels.Previews
Expand All @@ -21,5 +22,13 @@ public async override Task<List<FileProperty>> LoadPreviewAndDetailsAsync()

return [];
}

public override void PreviewControlBase_Unloaded(object sender, RoutedEventArgs e)
{
Stream?.Dispose();
Stream = null;

base.PreviewControlBase_Unloaded(sender, e);
}
}
}
Loading