Files
DP44/Common/DTS.CommonCore/Base/ViewModel/BaseViewModel.cs
2026-04-17 14:55:32 -04:00

155 lines
5.1 KiB
C#

using System;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Microsoft.Practices.Unity;
using System.Threading.Tasks;
using System.Windows.Threading;
using DTS.Common.Events;
// ReSharper disable once CheckNamespace
namespace DTS.Common.Base
{
public abstract class BaseViewModel<TModel> : BasePropertyChanged, IBaseViewModel
where TModel : class
{
protected IEventAggregator Aggregator { get; private set; }
protected IUnityContainer Container { get; private set; }
protected IRegionManager _regionManager { get; private set; }
public TModel Model { get; set; }
protected BaseViewModel()
{
}
protected BaseViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
{
Aggregator = eventAggregator;
Container = unityContainer;
_regionManager = regionManager;
CreateCommands();
}
protected virtual void CreateCommands()
{
ConfirmationRequest = new InteractionRequest<Confirmation>();
CloseCommand = new DelegateCommand<object>(CloseMethod);
}
#region Commands
/// <summary>
/// The interaction request to display the confirmation.
/// </summary>
public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
/// <summary>
/// The delegate command to close object.
/// </summary>
public DelegateCommand<object> CloseCommand { get; private set; }
/// <summary>
/// This is an execute method for the <see cref="CloseCommand">CloseCommand</see>.
/// </summary>
/// <param name="parameter">The parameter to pass to the <see cref="CloseCommand">CloseCommand</see>.</param>
protected virtual void CloseMethod(object parameter)
{
CleanupAtClose();
}
private void CleanupAtClose()
{
Cleanup();
}
#endregion Commands
#region Bases Methods
/// <summary>
/// Executes after the viewmodel activated.
/// </summary>
public virtual void Activated()
{
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
public virtual void Initialize()
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
public virtual void Initialize(object parameter)
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
}
public virtual void Initialize(object parameter, object model)
{
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
public virtual async Task InitializeAsync()
{
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
});
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
public virtual async Task InitializeAsync(object parameter)
{
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
});
}
#endregion Bases Methods
public new event PropertyChangedEventHandler PropertyChanged;
public bool IsMenuIncluded { get; set; }
public bool IsNavigationIncluded { get; set; }
public int Percentage { get; set; }
public string IsBusyMessage { get; set; }
public bool IsBusy { get; set; }
public bool IsDirty { get; set; }
/// <summary>
/// Sets the Model to null.
/// </summary>
public virtual void Cleanup()
{
Model = default(TModel);
}
public Task CleanupAsync()
{
throw new NotImplementedException();
}
}
}