diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index 0723bc313e09..c2910b6140be 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -341,7 +341,8 @@ public static Task LaunchNewWindowAsync() public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, bool openViaApplicationPicker = false) { // Don't open files and folders inside recycle bin - if (associatedInstance.ShellViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || + if (associatedInstance.ShellViewModel is null || + associatedInstance.ShellViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || associatedInstance.SlimContentPage?.SelectedItems is null) { return; @@ -378,7 +379,8 @@ public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, b public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInstance, IEnumerable items, string executablePath) { // Don't open files and folders inside recycle bin - if (associatedInstance.ShellViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || + if (associatedInstance.ShellViewModel is null || + associatedInstance.ShellViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || associatedInstance.SlimContentPage is null) return; @@ -398,6 +400,9 @@ public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInsta /// Open folders in a new tab regardless of the "OpenFoldersInNewTab" option public static async Task OpenPath(string path, IShellPage associatedInstance, FilesystemItemType? itemType = null, bool openSilent = false, bool openViaApplicationPicker = false, IEnumerable? selectItems = null, string? args = default, bool forceOpenInNewTab = false) { + if (associatedInstance.ShellViewModel is null) + return false; + string previousDir = associatedInstance.ShellViewModel.WorkingDirectory; bool isHiddenItem = Win32Helper.HasFileAttribute(path, System.IO.FileAttributes.Hidden); bool isDirectory = Win32Helper.HasFileAttribute(path, System.IO.FileAttributes.Directory); @@ -550,13 +555,16 @@ private static async Task OpenDirectory(string path, IShellPag } else { - opened = await associatedInstance.ShellViewModel.GetFolderWithPathFromPathAsync(path) - .OnSuccess((childFolder) => - { - // Add location to Recent Items List - if (childFolder.Item is SystemStorageFolder) - WindowsRecentItemsService.Add(childFolder.Path); - }); + if (associatedInstance.ShellViewModel is not null) + { + opened = await associatedInstance.ShellViewModel.GetFolderWithPathFromPathAsync(path) + .OnSuccess((childFolder) => + { + // Add location to Recent Items List + if (childFolder.Item is SystemStorageFolder) + WindowsRecentItemsService.Add(childFolder.Path); + }); + } if (!opened) opened = (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path); @@ -582,7 +590,7 @@ private static async Task OpenFile(string path, IShellPage ass } else { - if (!FileExtensionHelpers.IsWebLinkFile(path)) + if (!FileExtensionHelpers.IsWebLinkFile(path) && associatedInstance.ShellViewModel is not null) { StorageFileWithPath childFile = await associatedInstance.ShellViewModel.GetFileWithPathFromPathAsync(shortcutInfo.TargetPath); // Add location to Recent Items List @@ -599,9 +607,11 @@ private static async Task OpenFile(string path, IShellPage ass } else { - opened = await associatedInstance.ShellViewModel.GetFileWithPathFromPathAsync(path) - .OnSuccess(async childFile => - { + if (associatedInstance.ShellViewModel is not null) + { + opened = await associatedInstance.ShellViewModel.GetFileWithPathFromPathAsync(path) + .OnSuccess(async childFile => + { // Add location to Recent Items List if (childFile.Item is SystemStorageFile) WindowsRecentItemsService.Add(childFile.Path); @@ -627,7 +637,9 @@ private static async Task OpenFile(string path, IShellPage ass bool launchSuccess = false; BaseStorageFileQueryResult? fileQueryResult = null; //Get folder to create a file query (to pass to apps like Photos, Movies & TV..., needed to scroll through the folder like what Windows Explorer does) - BaseStorageFolder currentFolder = await associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(path)); + BaseStorageFolder? currentFolder = associatedInstance.ShellViewModel is not null + ? await associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(path)) + : null; if (currentFolder is not null) { QueryOptions queryOptions = new(CommonFileQuery.DefaultQuery, null); @@ -692,6 +704,7 @@ private static async Task OpenFile(string path, IShellPage ass } } }); + } } return opened; }