Made the TPageEnumeration a value type so the multi page view model can start out with 'null' and switch to the initial page after the main window loads
This commit is contained in:
parent
5175af250e
commit
7d98ba1e0d
3 changed files with 17 additions and 10 deletions
|
|
@ -27,7 +27,7 @@ using System.Runtime.InteropServices;
|
||||||
[assembly: AssemblyProduct("Nuclex.Avalonia")]
|
[assembly: AssemblyProduct("Nuclex.Avalonia")]
|
||||||
[assembly: AssemblyDescription("Lean and elegant MVVM library with extras for Avalonia")]
|
[assembly: AssemblyDescription("Lean and elegant MVVM library with extras for Avalonia")]
|
||||||
[assembly: AssemblyCompany("Nuclex Development Labs")]
|
[assembly: AssemblyCompany("Nuclex Development Labs")]
|
||||||
[assembly: AssemblyCopyright("Copyright © Markus Ewald / Nuclex Development Labs 2002-2025")]
|
[assembly: AssemblyCopyright("Copyright © Markus Ewald / Nuclex Development Labs 2002-2026")]
|
||||||
[assembly: AssemblyTrademark("")]
|
[assembly: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
|
@ -46,4 +46,4 @@ using System.Runtime.InteropServices;
|
||||||
// Build Number
|
// Build Number
|
||||||
// Revision
|
// Revision
|
||||||
//
|
//
|
||||||
[assembly: AssemblyVersion("1.4.0")]
|
[assembly: AssemblyVersion("1.5.0")]
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,11 @@ using Nuclex.Avalonia.Commands;
|
||||||
namespace Nuclex.Avalonia.ViewModels {
|
namespace Nuclex.Avalonia.ViewModels {
|
||||||
|
|
||||||
/// <summary>Interface for view models that can switch between different pages</summary>
|
/// <summary>Interface for view models that can switch between different pages</summary>
|
||||||
/// <typeparam name="TPageEnumeration">Enum type by which pages can be indicated</typeparam>
|
/// <typeparam name="TPageEnumeration">
|
||||||
public interface IMultiPageViewModel<TPageEnumeration> {
|
/// Type by which pages can be indicated (typically an enum)
|
||||||
|
/// </typeparam>
|
||||||
|
public interface IMultiPageViewModel<TPageEnumeration>
|
||||||
|
where TPageEnumeration : struct {
|
||||||
|
|
||||||
/// <summary>Command to switch the active tool page</summary>
|
/// <summary>Command to switch the active tool page</summary>
|
||||||
IAsyncCommand<TPageEnumeration> SwitchPageCommand { get; }
|
IAsyncCommand<TPageEnumeration> SwitchPageCommand { get; }
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ limitations under the License.
|
||||||
#endregion // Apache License 2.0
|
#endregion // Apache License 2.0
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -27,9 +28,12 @@ using Nuclex.Support;
|
||||||
namespace Nuclex.Avalonia.ViewModels {
|
namespace Nuclex.Avalonia.ViewModels {
|
||||||
|
|
||||||
/// <summary>Base class for view models that have multiple child view models</summary>
|
/// <summary>Base class for view models that have multiple child view models</summary>
|
||||||
/// <typeparam name="TPageEnumeration">Enum type by which pages can be indicated</typeparam>
|
/// <typeparam name="TPageEnumeration">
|
||||||
|
/// Type by which pages can be indicated (typically an enum)
|
||||||
|
/// </typeparam>
|
||||||
public abstract class MultiPageViewModel<TPageEnumeration> :
|
public abstract class MultiPageViewModel<TPageEnumeration> :
|
||||||
Observable, IMultiPageViewModel<TPageEnumeration>, IDisposable {
|
Observable, IMultiPageViewModel<TPageEnumeration>, IDisposable
|
||||||
|
where TPageEnumeration : struct {
|
||||||
|
|
||||||
/// <summary>Initializes a new multi-page view model</summary>
|
/// <summary>Initializes a new multi-page view model</summary>
|
||||||
/// <param name="windowManager">
|
/// <param name="windowManager">
|
||||||
|
|
@ -98,8 +102,8 @@ namespace Nuclex.Avalonia.ViewModels {
|
||||||
/// <summary>Switches to another page</summary>
|
/// <summary>Switches to another page</summary>
|
||||||
/// <param name="newPage">New page to switch to</param>
|
/// <param name="newPage">New page to switch to</param>
|
||||||
/// <returns>A task that will finish when the new page has been switched to</returns>
|
/// <returns>A task that will finish when the new page has been switched to</returns>
|
||||||
private Task switchPageAsync(TPageEnumeration? newPage) {
|
private Task switchPageAsync(TPageEnumeration newPage) {
|
||||||
if(newPage.Equals(this.activePage)) {
|
if(Comparer.Default.Compare(this.activePage, newPage) == 0) {
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,7 +120,7 @@ namespace Nuclex.Avalonia.ViewModels {
|
||||||
|
|
||||||
// Double-checked locking to avoid creating a view model for the same page
|
// Double-checked locking to avoid creating a view model for the same page
|
||||||
// multiple times if the construction takes time
|
// multiple times if the construction takes time
|
||||||
object newPageViewModel;
|
object? newPageViewModel;
|
||||||
if(!this.cachedViewModels.TryGetValue(newPage, out newPageViewModel)) {
|
if(!this.cachedViewModels.TryGetValue(newPage, out newPageViewModel)) {
|
||||||
lock(this.cachedViewModels) {
|
lock(this.cachedViewModels) {
|
||||||
if(!this.cachedViewModels.TryGetValue(newPage, out newPageViewModel)) {
|
if(!this.cachedViewModels.TryGetValue(newPage, out newPageViewModel)) {
|
||||||
|
|
@ -155,7 +159,7 @@ namespace Nuclex.Avalonia.ViewModels {
|
||||||
private readonly ConcurrentDictionary<TPageEnumeration, object>? cachedViewModels;
|
private readonly ConcurrentDictionary<TPageEnumeration, object>? cachedViewModels;
|
||||||
|
|
||||||
/// <summary>Page that is currently active in the multi-page view model</summary>
|
/// <summary>Page that is currently active in the multi-page view model</summary>
|
||||||
private TPageEnumeration activePage;
|
private TPageEnumeration? activePage;
|
||||||
/// <summary>View model for the active page</summary>
|
/// <summary>View model for the active page</summary>
|
||||||
private object? activePageViewModel;
|
private object? activePageViewModel;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue