Skip to content

Commit 24931fa

Browse files
Refactor services and update SDK version
- Updated `IDialogService` registration in `App.xaml.cs` to use a factory method with a `Window` parameter. - Upgraded `Microsoft.WindowsAppSDK` version in `BooksApp.csproj` from `1.6.250108002` to `1.7.250310001`. - Modified `WinUIDialogService` constructor to accept a `Window` parameter for better dialog handling. - Refactored `MainWindowViewModel` to simplify constructor and remove unnecessary private fields. - Streamlined `NavigationMessage` class by adopting primary constructor syntax. - Corrected dialog message in `BookDetailViewModel` and improved error logging format.
1 parent 4f746ad commit 24931fa

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

4_Apps/Patterns/BooksApp/BooksApp/App.xaml.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ private void RegisterServices()
4242
.AddScoped<BookDetailViewModel>()
4343
.AddScoped<MainWindowViewModel>()
4444
.AddSingleton<IItemsService<Book>, BooksService>()
45-
.AddSingleton<IDialogService, WinUIDialogService>()
45+
.AddSingleton<IDialogService>(provider =>
46+
{
47+
var window = _window ?? throw new InvalidOperationException("_window not set");
48+
return new WinUIDialogService(window);
49+
})
4650
.AddSingleton<INavigationService, WinUINavigationService>()
4751
.AddSingleton<WinUIInitializeNavigationService>()
4852
.AddLogging(builder =>

4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
34-
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" />
34+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250310001" />
3535
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
3636
<Manifest Include="$(ApplicationManifest)" />
3737
</ItemGroup>

4_Apps/Patterns/BooksApp/BooksApp/Services/WinUIDialogService.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace BooksApp.Services;
22

3-
public class WinUIDialogService : IDialogService
3+
public class WinUIDialogService(Window window) : IDialogService
44
{
55
public async Task ShowMessageAsync(string message)
66
{
77
ContentDialog dlg = new()
88
{
99
Title = "Message",
1010
Content = message,
11-
PrimaryButtonText = "OK",
11+
PrimaryButtonText = "OK",
12+
XamlRoot = window.Content.XamlRoot,
1213
};
1314
await dlg.ShowAsync();
1415
}

4_Apps/Patterns/BooksApp/BooksApp/ViewModels/MainWindowViewModel.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
namespace BooksApp.ViewModels;
22

3-
public class MainWindowViewModel : ViewModelBase
3+
public class MainWindowViewModel(INavigationService navigationService, WinUIInitializeNavigationService initializeNavigationService) : ViewModelBase
44
{
55
private readonly Dictionary<string, Type> _pages = new()
66
{
77
[PageNames.BooksPage] = typeof(BooksPage),
88
[PageNames.BookDetailPage] = typeof(BookDetailPage)
99
};
1010

11-
private readonly INavigationService _navigationService;
12-
private readonly WinUIInitializeNavigationService _initializeNavigationService;
13-
public MainWindowViewModel(INavigationService navigationService, WinUIInitializeNavigationService initializeNavigationService)
14-
{
15-
_navigationService = navigationService;
16-
_initializeNavigationService = initializeNavigationService;
17-
}
18-
19-
public void SetNavigationFrame(Frame frame) => _initializeNavigationService.Initialize(frame, _pages);
11+
public void SetNavigationFrame(Frame frame) => initializeNavigationService.Initialize(frame, _pages);
2012

2113
public void UseNavigation(bool navigation)
2214
{
23-
_navigationService.UseNavigation = navigation;
15+
navigationService.UseNavigation = navigation;
2416
}
2517

2618
public void OnNavigationSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
@@ -30,7 +22,7 @@ public void OnNavigationSelectionChanged(NavigationView sender, NavigationViewSe
3022
switch (navigationItem.Tag)
3123
{
3224
case "books":
33-
_navigationService.NavigateToAsync(PageNames.BooksPage);
25+
navigationService.NavigateToAsync(PageNames.BooksPage);
3426
break;
3527
default:
3628
break;

4_Apps/Patterns/BooksLib/Events/NavigationMessage.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public class NavigationInfo
66
{
77
public bool UseNavigation { get; set; }
88
}
9-
public class NavigationMessage : ValueChangedMessage<NavigationInfo>
9+
public class NavigationMessage(NavigationInfo navigationInfo) :
10+
ValueChangedMessage<NavigationInfo>(navigationInfo)
1011
{
11-
public NavigationMessage(NavigationInfo navigationInfo)
12-
: base(navigationInfo) { }
1312
}

4_Apps/Patterns/BooksLib/ViewModels/BookDetailViewModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public override async Task OnSaveAsync()
4747
{
4848
if (EditItem is null) throw new InvalidOperationException();
4949

50-
await _dialogService.ShowMessageAsync("TEst save");
50+
await _dialogService.ShowMessageAsync("Test save");
5151

5252
await _itemsService.AddOrUpdateAsync(EditItem);
5353
}
5454
catch (Exception ex)
5555
{
56-
_logger.LogError("error {0} in {1}", ex.Message, nameof(OnSaveAsync));
56+
_logger.LogError("error {message} in {method}", ex.Message, nameof(OnSaveAsync));
5757
await _dialogService.ShowMessageAsync("Error saving the data");
5858
}
5959
}

0 commit comments

Comments
 (0)