Skip to content

Commit 06489eb

Browse files
authored
Fix: Fixed crash that would occur when using an invalid background image (#17083)
1 parent b14b559 commit 06489eb

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/Files.App/ViewModels/MainPageViewModel.cs

+23-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.UI.Xaml;
5+
using Microsoft.UI.Xaml.Controls;
56
using Microsoft.UI.Xaml.Input;
67
using Microsoft.UI.Xaml.Media;
78
using Microsoft.UI.Xaml.Media.Imaging;
89
using Microsoft.UI.Xaml.Navigation;
910
using System.Windows.Input;
1011
using Windows.System;
11-
using Microsoft.UI.Xaml.Controls;
1212

1313
namespace Files.App.ViewModels
1414
{
@@ -88,10 +88,27 @@ public Stretch AppThemeBackgroundImageFit
8888
public float AppThemeBackgroundImageOpacity
8989
=> AppearanceSettingsService.AppThemeBackgroundImageOpacity;
9090

91-
public ImageSource? AppThemeBackgroundImageSource =>
92-
string.IsNullOrEmpty(AppearanceSettingsService.AppThemeBackgroundImageSource)
93-
? null
94-
: new BitmapImage(new Uri(AppearanceSettingsService.AppThemeBackgroundImageSource, UriKind.RelativeOrAbsolute));
91+
public ImageSource? AppThemeBackgroundImageSource
92+
{
93+
get
94+
{
95+
if (string.IsNullOrWhiteSpace(AppearanceSettingsService.AppThemeBackgroundImageSource))
96+
return null;
97+
98+
if (!Uri.TryCreate(AppearanceSettingsService.AppThemeBackgroundImageSource, UriKind.RelativeOrAbsolute, out Uri? validUri))
99+
return null;
100+
101+
try
102+
{
103+
return new BitmapImage(validUri);
104+
}
105+
catch (Exception)
106+
{
107+
// Catch potential errors
108+
return null;
109+
}
110+
}
111+
}
95112

96113
public VerticalAlignment AppThemeBackgroundImageVerticalAlignment
97114
=> AppearanceSettingsService.AppThemeBackgroundImageVerticalAlignment;
@@ -104,7 +121,7 @@ public HorizontalAlignment AppThemeBackgroundImageHorizontalAlignment
104121
context.PageType is not ContentPageTypes.Home &&
105122
context.PageType is not ContentPageTypes.ReleaseNotes &&
106123
context.PageType is not ContentPageTypes.Settings;
107-
124+
108125
public bool ShowStatusBar =>
109126
context.PageType is not ContentPageTypes.Home &&
110127
context.PageType is not ContentPageTypes.ReleaseNotes &&

0 commit comments

Comments
 (0)