Skip to content

Commit 223ecce

Browse files
Fix: Handle exceptions when opening files from IFileActivatedEventArgs (#17356)
1 parent bc97433 commit 223ecce

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/Files.App/MainWindow.xaml.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,39 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
160160
break;
161161

162162
case IFileActivatedEventArgs fileArgs:
163-
var index = 0;
164-
if (rootFrame.Content is null || rootFrame.Content is SplashScreenPage || !MainPageViewModel.AppInstances.Any())
163+
try
165164
{
166-
// When the navigation stack isn't restored navigate to the first page,
167-
// configuring the new page by passing required information as a navigation parameter
168-
rootFrame.Navigate(typeof(MainPage), fileArgs.Files.First().Path, new SuppressNavigationTransitionInfo());
169-
index = 1;
170-
}
171-
else
172-
{
173-
// Bring to foreground (#14730)
174-
Win32Helper.BringToForegroundEx(new(WindowHandle));
175-
}
165+
if (fileArgs.Files is null || fileArgs.Files.Count == 0)
166+
{
167+
break;
168+
}
169+
170+
var index = 0;
171+
if (rootFrame.Content is null || rootFrame.Content is SplashScreenPage || !MainPageViewModel.AppInstances.Any())
172+
{
173+
// When the navigation stack isn't restored navigate to the first page,
174+
// configuring the new page by passing required information as a navigation parameter
175+
rootFrame.Navigate(typeof(MainPage), fileArgs.Files.First().Path, new SuppressNavigationTransitionInfo());
176+
index = 1;
177+
}
178+
else
179+
{
180+
// Bring to foreground (#14730)
181+
Win32Helper.BringToForegroundEx(new(WindowHandle));
182+
}
176183

177-
for (; index < fileArgs.Files.Count; index++)
184+
for (; index < fileArgs.Files.Count; index++)
185+
{
186+
await NavigationHelpers.AddNewTabByPathAsync(typeof(ShellPanesPage), fileArgs.Files[index].Path, true);
187+
}
188+
}
189+
catch (Exception ex)
178190
{
179-
await NavigationHelpers.AddNewTabByPathAsync(typeof(ShellPanesPage), fileArgs.Files[index].Path, true);
191+
App.Logger.LogWarning(ex, "Failed to open files.");
192+
if (rootFrame.Content is null || rootFrame.Content is SplashScreenPage || !MainPageViewModel.AppInstances.Any())
193+
rootFrame.Navigate(typeof(MainPage), null, new SuppressNavigationTransitionInfo());
194+
else
195+
Win32Helper.BringToForegroundEx(new(WindowHandle));
180196
}
181197
break;
182198

0 commit comments

Comments
 (0)