From 0d2e8c96ebe66eb720d1b47dc9a2ab4c54a4fb4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20K=C3=B6nig?= Date: Sun, 3 Dec 2023 19:22:14 +0100 Subject: [PATCH 1/2] Added ViewServiceHostUserControl --- src/Directory.Build.props | 3 +- .../Mvvm/MvvmUserControlTests.cs | 2 +- .../ViewServiceHostUserControlTests.cs | 60 +++++++++++++++++++ .../Mvvm/Markup/ViewServiceHostUserControl.cs | 27 +++++++++ .../ViewServices/DefaultViewServices.cs | 2 - .../ViewServices/IOpenDirectoryViewService.cs | 7 +-- 6 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/RolandK.AvaloniaExtensions.Tests/ViewServices/ViewServiceHostUserControlTests.cs create mode 100644 src/RolandK.AvaloniaExtensions/Mvvm/Markup/ViewServiceHostUserControl.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 96d0d7c..5be51f9 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,8 @@ - 2.0.0 + 2.0.1 + preview-1 diff --git a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs index 7d5eb14..d474a38 100644 --- a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs +++ b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs @@ -7,7 +7,7 @@ using RolandK.AvaloniaExtensions.ViewServices; using RolandK.AvaloniaExtensions.ViewServices.Base; -namespace RolandK.AvaloniaExtensions.Tests.Views; +namespace RolandK.AvaloniaExtensions.Tests.Mvvm; [Collection(nameof(ApplicationTestCollection))] public class MvvmUserControlTests diff --git a/src/RolandK.AvaloniaExtensions.Tests/ViewServices/ViewServiceHostUserControlTests.cs b/src/RolandK.AvaloniaExtensions.Tests/ViewServices/ViewServiceHostUserControlTests.cs new file mode 100644 index 0000000..c27c26c --- /dev/null +++ b/src/RolandK.AvaloniaExtensions.Tests/ViewServices/ViewServiceHostUserControlTests.cs @@ -0,0 +1,60 @@ +using RolandK.AvaloniaExtensions.Mvvm.Markup; +using RolandK.AvaloniaExtensions.Tests.Util; +using RolandK.AvaloniaExtensions.ViewServices; +using RolandK.AvaloniaExtensions.ViewServices.Base; + +namespace RolandK.AvaloniaExtensions.Tests.ViewServices; + +[Collection(nameof(ApplicationTestCollection))] +public class ViewServiceHostUserControlTests +{ + [Fact] + public Task DefaultViewService_from_ViewServiceHostUserControl() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testViewServiceUserControl = new ViewServiceHostUserControl(); + + var hostWindow = new TestRootWindow(); + hostWindow.Content = testViewServiceUserControl; + + // Act + var srvOpenFileDialog = testViewServiceUserControl.TryFindViewService(); + + // Assert + Assert.NotNull(srvOpenFileDialog); + + GC.KeepAlive(hostWindow); + }); + } + + [Fact] + public Task Find_ViewService_From_ParentContainer() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testViewServiceUserControl = new ViewServiceHostUserControl(); + + var parentViewServiceUserControl = new ViewServiceHostUserControl(); + parentViewServiceUserControl.Content = testViewServiceUserControl; + parentViewServiceUserControl.ViewServices.Add(new DummyViewService()); + + var hostWindow = new TestRootWindow(); + hostWindow.Content = parentViewServiceUserControl; + + // Act + var srvOpenFileDialog = testViewServiceUserControl.TryFindViewService(); + + // Assert + Assert.NotNull(srvOpenFileDialog); + + GC.KeepAlive(hostWindow); + }); + } + + private interface IDummyViewService : IViewService { } + + private class DummyViewService : ViewServiceBase, IDummyViewService { } +} diff --git a/src/RolandK.AvaloniaExtensions/Mvvm/Markup/ViewServiceHostUserControl.cs b/src/RolandK.AvaloniaExtensions/Mvvm/Markup/ViewServiceHostUserControl.cs new file mode 100644 index 0000000..4bf146c --- /dev/null +++ b/src/RolandK.AvaloniaExtensions/Mvvm/Markup/ViewServiceHostUserControl.cs @@ -0,0 +1,27 @@ +using Avalonia.Controls; +using RolandK.AvaloniaExtensions.ViewServices; +using RolandK.AvaloniaExtensions.ViewServices.Base; + +namespace RolandK.AvaloniaExtensions.Mvvm.Markup; + +public class ViewServiceHostUserControl : UserControl, IViewServiceHost +{ + private ViewServiceContainer _viewServiceContainer; + + /// + public ICollection ViewServices => _viewServiceContainer.ViewServices; + + /// + public IViewServiceHost? ParentViewServiceHost => this.TryGetParentViewServiceHost(); + + public ViewServiceHostUserControl() + { + _viewServiceContainer = new ViewServiceContainer(this); + } + + /// + public object? TryGetDefaultViewService(Type viewServiceType) + { + return DefaultViewServices.TryGetDefaultViewService(this, viewServiceType); + } +} diff --git a/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs b/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs index 2f49b1f..8695a11 100644 --- a/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs +++ b/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs @@ -1,7 +1,5 @@ -using System; using Avalonia.Controls; using Avalonia.LogicalTree; -using Avalonia.VisualTree; using RolandK.AvaloniaExtensions.Views; using RolandK.AvaloniaExtensions.ViewServices.FileDialogs; using RolandK.AvaloniaExtensions.ViewServices.MessageBox; diff --git a/src/RolandK.AvaloniaExtensions/ViewServices/IOpenDirectoryViewService.cs b/src/RolandK.AvaloniaExtensions/ViewServices/IOpenDirectoryViewService.cs index 01be941..c796f09 100644 --- a/src/RolandK.AvaloniaExtensions/ViewServices/IOpenDirectoryViewService.cs +++ b/src/RolandK.AvaloniaExtensions/ViewServices/IOpenDirectoryViewService.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using RolandK.AvaloniaExtensions.ViewServices.Base; +using RolandK.AvaloniaExtensions.ViewServices.Base; namespace RolandK.AvaloniaExtensions.ViewServices; From b443f9c65b90fd5d07c109319260d7976845382d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20K=C3=B6nig?= Date: Sun, 3 Dec 2023 19:28:20 +0100 Subject: [PATCH 2/2] Updated version information --- src/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 5be51f9..6fd3604 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -3,7 +3,7 @@ 2.0.1 - preview-1 + preview1