72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Wino.Core.Domain.Interfaces;
|
|
using Wino.Core.Domain.Models;
|
|
using Wino.Core.Domain.Models.Navigation;
|
|
|
|
namespace Wino.Core.ViewModels;
|
|
|
|
public class CoreBaseViewModel : ObservableRecipient, INavigationAware
|
|
{
|
|
private IDispatcher _dispatcher;
|
|
public IDispatcher Dispatcher
|
|
{
|
|
get
|
|
{
|
|
return _dispatcher;
|
|
}
|
|
set
|
|
{
|
|
_dispatcher = value;
|
|
|
|
if (value != null)
|
|
{
|
|
OnDispatcherAssigned();
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void OnNavigatedTo(NavigationMode mode, object parameters)
|
|
{
|
|
UnregisterRecipients();
|
|
RegisterRecipients();
|
|
}
|
|
|
|
public virtual void OnNavigatedFrom(NavigationMode mode, object parameters)
|
|
{
|
|
UnregisterRecipients();
|
|
}
|
|
|
|
public virtual void OnPageLoaded() { }
|
|
|
|
public virtual Task KeyboardShortcutHook(KeyboardShortcutTriggerDetails args) => Task.CompletedTask;
|
|
|
|
public Task ExecuteUIThread(Action action)
|
|
{
|
|
if (action == null) return Task.CompletedTask;
|
|
|
|
if (Dispatcher == null)
|
|
{
|
|
action();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
return Dispatcher.ExecuteOnUIThread(action);
|
|
}
|
|
public void ReportUIChange<TMessage>(TMessage message) where TMessage : class, IUIMessage => Messenger.Send(message);
|
|
|
|
protected virtual void OnDispatcherAssigned() { }
|
|
|
|
/// <summary>
|
|
/// Register message recipients for this view model. Override to register specific message types.
|
|
/// </summary>
|
|
protected virtual void RegisterRecipients() { }
|
|
|
|
/// <summary>
|
|
/// Unregister message recipients for this view model. Override to unregister specific message types.
|
|
/// </summary>
|
|
protected virtual void UnregisterRecipients() { }
|
|
}
|