Skip to content

Commit a3eb151

Browse files
authored
Fix app crash on Properties window closing (#1646)
1 parent 890bfbc commit a3eb151

File tree

8 files changed

+149
-80
lines changed

8 files changed

+149
-80
lines changed

Files/Interacts/Interaction.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
using Windows.UI;
3535
using Windows.UI.Core;
3636
using Windows.UI.Popups;
37-
using Windows.UI.WindowManagement;
38-
using Windows.UI.WindowManagement.Preview;
37+
using Windows.UI.ViewManagement;
3938
using Windows.UI.Xaml;
4039
using Windows.UI.Xaml.Controls;
4140
using Windows.UI.Xaml.Hosting;
@@ -527,9 +526,6 @@ public void ShareItem_Click(object sender, RoutedEventArgs e)
527526
DataTransferManager.ShowShareUI();
528527
}
529528

530-
public static Dictionary<UIContext, AppWindow> AppWindows { get; set; }
531-
= new Dictionary<UIContext, AppWindow>();
532-
533529
private async void ShowProperties()
534530
{
535531
if (App.CurrentInstance.ContentPage.IsItemSelected)
@@ -562,27 +558,28 @@ private async Task OpenPropertiesWindow(object item)
562558
{
563559
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
564560
{
565-
AppWindow appWindow = await AppWindow.TryCreateAsync();
566-
Frame frame = new Frame();
567-
appWindow.TitleBar.ExtendsContentIntoTitleBar = true;
568-
frame.Navigate(typeof(Properties), item, new SuppressNavigationTransitionInfo());
569-
570-
WindowManagementPreview.SetPreferredMinSize(appWindow, new Size(400, 550));
571-
572-
appWindow.RequestSize(new Size(400, 550));
573-
appWindow.Title = ResourceController.GetTranslation("PropertiesTitle");
561+
CoreApplicationView newWindow = CoreApplication.CreateNewView();
562+
ApplicationView newView = null;
574563

575-
ElementCompositionPreview.SetAppWindowContent(appWindow, frame);
576-
AppWindows.Add(frame.UIContext, appWindow);
577-
578-
appWindow.Closed += delegate
564+
await newWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
579565
{
580-
AppWindows.Remove(frame.UIContext);
581-
frame.Content = null;
582-
appWindow = null;
583-
};
584-
585-
await appWindow.TryShowAsync();
566+
Frame frame = new Frame();
567+
frame.Navigate(typeof(Properties), item, new SuppressNavigationTransitionInfo());
568+
Window.Current.Content = frame;
569+
Window.Current.Activate();
570+
571+
newView = ApplicationView.GetForCurrentView();
572+
newWindow.TitleBar.ExtendViewIntoTitleBar = true;
573+
newView.Title = ResourceController.GetTranslation("PropertiesTitle");
574+
newView.SetPreferredMinSize(new Size(400, 550));
575+
newView.Consolidated += delegate
576+
{
577+
Window.Current.Close();
578+
};
579+
});
580+
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newView.Id);
581+
// Set window size again here as sometimes it's not resized in the page Loaded event
582+
newView.TryResizeView(new Size(400, 550));
586583
}
587584
else
588585
{

Files/View Models/Properties/FileProperties.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Windows.Foundation.Collections;
99
using Windows.Security.Cryptography.Core;
1010
using Windows.Storage.FileProperties;
11+
using Windows.UI.Core;
1112
using Windows.UI.Xaml;
1213
using Windows.UI.Xaml.Media.Imaging;
1314

@@ -19,11 +20,12 @@ internal class FileProperties : BaseProperties
1920

2021
public ListedItem Item { get; }
2122

22-
public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, ProgressBar progressBar, ListedItem item)
23+
public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, CoreDispatcher coreDispatcher, ProgressBar progressBar, ListedItem item)
2324
{
2425
ViewModel = viewModel;
2526
TokenSource = tokenSource;
2627
ProgressBar = progressBar;
28+
Dispatcher = coreDispatcher;
2729
Item = item;
2830

2931
GetBaseProperties();
@@ -38,7 +40,7 @@ public override void GetBaseProperties()
3840
ViewModel.ItemType = Item.ItemType;
3941
ViewModel.ItemPath = Path.IsPathRooted(Item.ItemPath) ? Path.GetDirectoryName(Item.ItemPath) : Item.ItemPath;
4042
ViewModel.ItemModifiedTimestamp = Item.ItemDateModified;
41-
ViewModel.FileIconSource = Item.FileImage;
43+
//ViewModel.FileIconSource = Item.FileImage;
4244
ViewModel.LoadFolderGlyph = Item.LoadFolderGlyph;
4345
ViewModel.LoadUnknownTypeGlyph = Item.LoadUnknownTypeGlyph;
4446
ViewModel.LoadFileIcon = Item.LoadFileIcon;

Files/View Models/Properties/FolderProperties.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using ByteSizeLib;
22
using Files.Filesystem;
33
using Files.Helpers;
4+
using Microsoft.Toolkit.Uwp.Helpers;
45
using System;
56
using System.IO;
67
using System.Threading;
78
using System.Threading.Tasks;
9+
using Windows.ApplicationModel.Core;
810
using Windows.Foundation.Collections;
911
using Windows.Storage;
1012
using Windows.UI.Core;
@@ -35,7 +37,7 @@ public override void GetBaseProperties()
3537
ViewModel.ItemType = Item.ItemType;
3638
ViewModel.ItemPath = Path.IsPathRooted(Item.ItemPath) ? Path.GetDirectoryName(Item.ItemPath) : Item.ItemPath;
3739
ViewModel.ItemModifiedTimestamp = Item.ItemDateModified;
38-
ViewModel.FileIconSource = Item.FileImage;
40+
//ViewModel.FileIconSource = Item.FileImage;
3941
ViewModel.LoadFolderGlyph = Item.LoadFolderGlyph;
4042
ViewModel.LoadUnknownTypeGlyph = Item.LoadUnknownTypeGlyph;
4143
ViewModel.LoadFileIcon = Item.LoadFileIcon;
@@ -76,7 +78,8 @@ public async override void GetSpecialProperties()
7678
}
7779

7880
StorageFolder storageFolder;
79-
if (App.CurrentInstance.ContentPage.IsItemSelected)
81+
var isItemSelected = await CoreApplication.MainView.ExecuteOnUIThreadAsync(() => App.CurrentInstance.ContentPage.IsItemSelected);
82+
if (isItemSelected)
8083
{
8184
storageFolder = await ItemViewModel.GetFolderFromPathAsync(Item.ItemPath);
8285
ViewModel.ItemCreatedTimestamp = ListedItem.GetFriendlyDate(storageFolder.DateCreated);

Files/View Models/SelectedItemsPropertiesViewModel.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
using Files.Helpers;
44
using GalaSoft.MvvmLight;
55
using GalaSoft.MvvmLight.Command;
6+
using Microsoft.Toolkit.Uwp.Helpers;
67
using System;
78
using System.Collections.Generic;
89
using System.IO;
910
using System.Linq;
11+
using System.Threading.Tasks;
12+
using Windows.ApplicationModel.Core;
13+
using Windows.UI.Core;
1014
using Windows.UI.Xaml;
1115
using Windows.UI.Xaml.Media;
1216

@@ -509,11 +513,10 @@ public bool IsSelectedItemShortcut
509513
set => Set(ref _IsSelectedItemShortcut, value);
510514
}
511515

512-
public void CheckFileExtension()
516+
public async void CheckFileExtension()
513517
{
514518
//check if the selected item is an image file
515-
string ItemExtension = App.CurrentInstance.ContentPage.SelectedItem.FileExtension;
516-
519+
string ItemExtension = await CoreApplication.MainView.ExecuteOnUIThreadAsync(() => App.CurrentInstance.ContentPage.SelectedItem.FileExtension);
517520
if (!string.IsNullOrEmpty(ItemExtension) && SelectedItemsCount == "1 " + ResourceController.GetTranslation("ItemSelected/Text"))
518521
{
519522
if (ItemExtension.Equals(".png", StringComparison.OrdinalIgnoreCase)

Files/Views/Pages/Properties.xaml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@
55
xmlns:Windows10version1903="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 8)"
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:local="using:Files"
8-
xmlns:local1="using:Files.Helpers"
98
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
109
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
1110
d:DesignHeight="700"
1211
d:DesignWidth="400"
1312
KeyDown="Page_KeyDown"
1413
Loaded="Properties_Loaded"
15-
RequestedTheme="{x:Bind local1:ThemeHelper.RootTheme}"
1614
Unloaded="Properties_Unloaded"
1715
mc:Ignorable="d">
18-
<Page.Background>
19-
<AcrylicBrush
20-
Windows10version1903:TintLuminosityOpacity="0.9"
21-
AlwaysUseFallback="{x:Bind AppSettings.AcrylicEnabled, Mode=OneWay}"
22-
BackgroundSource="HostBackdrop"
23-
FallbackColor="{x:Bind AppSettings.AcrylicTheme.FallbackColor, Mode=OneWay}"
24-
TintColor="{x:Bind AppSettings.AcrylicTheme.TintColor, Mode=OneWay}"
25-
TintOpacity="{x:Bind AppSettings.AcrylicTheme.TintOpacity, Mode=OneWay}" />
26-
</Page.Background>
2716

2817
<Grid>
2918
<Grid.RowDefinitions>
@@ -42,6 +31,7 @@
4231
x:Name="NavigationView"
4332
Grid.Row="1"
4433
AllowDrop="False"
34+
IsPaneOpen="False"
4535
IsBackButtonVisible="Collapsed"
4636
IsPaneToggleButtonVisible="False"
4737
IsSettingsVisible="False"

0 commit comments

Comments
 (0)