Browse Source

Add type parameter overload for NavigateTo

pull/324/head
Ionite 1 year ago
parent
commit
7a978be6ca
No known key found for this signature in database
  1. 12
      StabilityMatrix.Avalonia/Services/INavigationService.cs
  2. 50
      StabilityMatrix.Avalonia/Services/NavigationService.cs

12
StabilityMatrix.Avalonia/Services/INavigationService.cs

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Media.Animation;
using StabilityMatrix.Avalonia.Models;
@ -6,7 +7,7 @@ using StabilityMatrix.Avalonia.ViewModels.Base;
namespace StabilityMatrix.Avalonia.Services;
public interface INavigationService<T>
public interface INavigationService<[SuppressMessage("ReSharper", "UnusedTypeParameter")] T>
{
event EventHandler<TypedNavigationEventArgs>? TypedNavigation;
@ -24,6 +25,15 @@ public interface INavigationService<T>
)
where TViewModel : ViewModelBase;
/// <summary>
/// Navigate to the view of the given view model type.
/// </summary>
void NavigateTo(
Type viewModelType,
NavigationTransitionInfo? transitionInfo = null,
object? param = null
);
/// <summary>
/// Navigate to the view of the given view model.
/// </summary>

50
StabilityMatrix.Avalonia/Services/NavigationService.cs

@ -76,6 +76,56 @@ public class NavigationService<T> : INavigationService<T>
);
}
/// <inheritdoc />
public void NavigateTo(
Type viewModelType,
NavigationTransitionInfo? transitionInfo = null,
object? param = null
)
{
if (!viewModelType.IsAssignableFrom(typeof(ViewModelBase)))
{
// ReSharper disable once LocalizableElement
throw new ArgumentException("Type must be a ViewModelBase.", nameof(viewModelType));
}
if (_frame is null)
{
throw new InvalidOperationException("SetFrame was not called before NavigateTo.");
}
if (App.Services.GetService(typeof(ISettingsManager)) is ISettingsManager settingsManager)
{
// Handle animation scale
switch (transitionInfo)
{
// If the transition info is null or animation scale is 0, suppress the transition
case null:
case BaseTransitionInfo when settingsManager.Settings.AnimationScale == 0f:
transitionInfo = new SuppressNavigationTransitionInfo();
break;
case BaseTransitionInfo baseTransitionInfo:
baseTransitionInfo.Duration *= settingsManager.Settings.AnimationScale;
break;
}
}
_frame.NavigateToType(
viewModelType,
param,
new FrameNavigationOptions
{
IsNavigationStackEnabled = true,
TransitionInfoOverride = transitionInfo ?? new SuppressNavigationTransitionInfo()
}
);
TypedNavigation?.Invoke(
this,
new TypedNavigationEventArgs { ViewModelType = viewModelType }
);
}
/// <inheritdoc />
public void NavigateTo(ViewModelBase viewModel, NavigationTransitionInfo? transitionInfo = null)
{

Loading…
Cancel
Save