Skip to content

Commit

Permalink
Merge pull request #5 from RolandKoenig/3-create-viewservicehostuserc…
Browse files Browse the repository at this point in the history
…ontrol-base-class

3 create viewservicehostusercontrol base class
  • Loading branch information
RolandKoenig authored Dec 30, 2023
2 parents b899434 + b443f9c commit ae67864
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<!-- Version -->
<PropertyGroup>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionSuffix>preview1</VersionSuffix>
</PropertyGroup>

<!-- Nuget -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IOpenFileViewService>();

// 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<IDummyViewService>();

// Assert
Assert.NotNull(srvOpenFileDialog);

GC.KeepAlive(hostWindow);
});
}

private interface IDummyViewService : IViewService { }

private class DummyViewService : ViewServiceBase, IDummyViewService { }
}
Original file line number Diff line number Diff line change
@@ -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;

/// <inheritdoc />
public ICollection<IViewService> ViewServices => _viewServiceContainer.ViewServices;

/// <inheritdoc />
public IViewServiceHost? ParentViewServiceHost => this.TryGetParentViewServiceHost();

public ViewServiceHostUserControl()
{
_viewServiceContainer = new ViewServiceContainer(this);
}

/// <inheritdoc />
public object? TryGetDefaultViewService(Type viewServiceType)
{
return DefaultViewServices.TryGetDefaultViewService(this, viewServiceType);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit ae67864

Please sign in to comment.