try to use IEventAggregator and IHandle

pull/1/head
Sascha Woitschetzki 2021-07-09 14:21:02 +07:00
parent 0e55a547a8
commit c588e0b648
5 changed files with 98 additions and 10 deletions

@ -4,13 +4,21 @@ namespace Gremlin.MVVM
{ {
public class AppBootstrapper : BootstrapperBase public class AppBootstrapper : BootstrapperBase
{ {
private readonly SimpleContainer _container = new();
public AppBootstrapper() public AppBootstrapper()
{ {
Initialize(); Initialize();
} }
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{ {
//LogManager.GetLog = type => new DebugLog(type); //DEBUGGING
DisplayRootViewFor<ShellViewModel>(); DisplayRootViewFor<ShellViewModel>();
} }
} }

@ -7,17 +7,32 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Gremlin.MVVM namespace Gremlin.MVVM
{ {
public class AllContactsViewModel : PropertyChangedBase public class AllContactsViewModel : PropertyChangedBase, IHandle<ShellViewModel>
{ {
private ObservableCollection<ContactViewModel> _allContactsVM; private ObservableCollection<ContactViewModel> _allContactsVM;
private IEventAggregator _eventAggregator;
public ObservableCollection<ContactViewModel> AllContactsVM { get => _allContactsVM; internal set { _allContactsVM = value; NotifyOfPropertyChange(() => AllContactsVM); } } public ObservableCollection<ContactViewModel> AllContactsVM { get => _allContactsVM; internal set { _allContactsVM = value; NotifyOfPropertyChange(() => AllContactsVM); } }
public AllContactsViewModel() //public AllContactsViewModel()
//{
// LoadContacts();
//}
public AllContactsViewModel(IEventAggregator eventAggregator)
{ {
_eventAggregator = eventAggregator;
LoadContacts(); LoadContacts();
_eventAggregator.PublishOnUIThreadAsync($"{AllContactsVM.Count} Contacts loaded.");
}
public AllContactsViewModel()
{
} }
public void LoadContacts() public void LoadContacts()
@ -50,5 +65,10 @@ namespace Gremlin.MVVM
throw; throw;
} }
} }
public Task HandleAsync(ShellViewModel message, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
} }
} }

@ -3,10 +3,12 @@ using System.Text;
using System.Windows.Data; using System.Windows.Data;
using Gremlin.GremlinData.EntityClasses; using Gremlin.GremlinData.EntityClasses;
using Caliburn.Micro; using Caliburn.Micro;
using System.Threading.Tasks;
using System.Threading;
namespace Gremlin.MVVM namespace Gremlin.MVVM
{ {
public class ContactViewModel : PropertyChangedBase public class ContactViewModel : PropertyChangedBase, IHandle<ShellViewModel>
{ {
private byte _gender; private byte _gender;
private string _firstName; private string _firstName;
@ -16,6 +18,7 @@ namespace Gremlin.MVVM
private string _accountStreet; private string _accountStreet;
private uint _accountZIP; private uint _accountZIP;
private string _accountCity; private string _accountCity;
private IEventAggregator _eventAggregator;
public byte Gender { get => _gender; internal set { _gender = value; NotifyOfPropertyChange(() => Gender); } } public byte Gender { get => _gender; internal set { _gender = value; NotifyOfPropertyChange(() => Gender); } }
public string FirstName { get => _firstName; internal set { _firstName = value; NotifyOfPropertyChange(() => FirstName); } } public string FirstName { get => _firstName; internal set { _firstName = value; NotifyOfPropertyChange(() => FirstName); } }
@ -26,7 +29,10 @@ namespace Gremlin.MVVM
public uint AccountZIP { get => _accountZIP; internal set { _accountZIP = value; NotifyOfPropertyChange(() => AccountZIP); } } public uint AccountZIP { get => _accountZIP; internal set { _accountZIP = value; NotifyOfPropertyChange(() => AccountZIP); } }
public string AccountCity { get => _accountCity; internal set { _accountCity = value; NotifyOfPropertyChange(() => AccountCity); } } public string AccountCity { get => _accountCity; internal set { _accountCity = value; NotifyOfPropertyChange(() => AccountCity); } }
public ContactViewModel() { } public ContactViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public ContactViewModel(byte gender, string firstName, string lastName, string eMail, string accountName, string accountStreet, uint accountZIP, string accountCity) public ContactViewModel(byte gender, string firstName, string lastName, string eMail, string accountName, string accountStreet, uint accountZIP, string accountCity)
{ {
@ -40,6 +46,10 @@ namespace Gremlin.MVVM
AccountCity = accountCity ?? throw new ArgumentNullException(nameof(accountCity)); AccountCity = accountCity ?? throw new ArgumentNullException(nameof(accountCity));
} }
public ContactViewModel()
{
}
internal static ContactViewModel ConvertObjectToContactVM(object selectedItem) internal static ContactViewModel ConvertObjectToContactVM(object selectedItem)
{ {
ContactViewModel selectedContact = new(); ContactViewModel selectedContact = new();
@ -178,5 +188,9 @@ namespace Gremlin.MVVM
return briefkopf; return briefkopf;
} }
public Task HandleAsync(ShellViewModel message, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
} }
} }

@ -2,10 +2,12 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Gremlin.MVVM namespace Gremlin.MVVM
{ {
public class QuoteViewModel : PropertyChangedBase public class QuoteViewModel : PropertyChangedBase, IHandle<ShellViewModel>
{ {
private string _quoteType = "ein Analysegerät"; private string _quoteType = "ein Analysegerät";
private string _quotePath; private string _quotePath;
@ -27,6 +29,7 @@ namespace Gremlin.MVVM
private bool _quoteContains3PP; private bool _quoteContains3PP;
private bool _quoteContainsRB; private bool _quoteContainsRB;
private bool? _showDiscounts = true; private bool? _showDiscounts = true;
private IEventAggregator _eventAggregator;
public string QuoteType { get => _quoteType; internal set { _quoteType = value; NotifyOfPropertyChange(() => QuoteType); } } public string QuoteType { get => _quoteType; internal set { _quoteType = value; NotifyOfPropertyChange(() => QuoteType); } }
public string QuotePath { get => _quotePath; internal set { _quotePath = value; NotifyOfPropertyChange(() => QuotePath); } } public string QuotePath { get => _quotePath; internal set { _quotePath = value; NotifyOfPropertyChange(() => QuotePath); } }
@ -62,7 +65,7 @@ namespace Gremlin.MVVM
}; };
} }
public QuoteViewModel() { } //public QuoteViewModel() { }
private QuoteViewModel(string quoteNumber, string quoteType, ContactViewModel recipient, ContactViewModel salesRep, bool? brutto, float vAT, int warranty, int validity, string quotePath, bool? singlePrices, bool? brochures, bool? dataSheets, bool? mailTemplate, bool? showDiscounts) private QuoteViewModel(string quoteNumber, string quoteType, ContactViewModel recipient, ContactViewModel salesRep, bool? brutto, float vAT, int warranty, int validity, string quotePath, bool? singlePrices, bool? brochures, bool? dataSheets, bool? mailTemplate, bool? showDiscounts)
{ {
@ -82,6 +85,15 @@ namespace Gremlin.MVVM
ShowDiscounts = showDiscounts; ShowDiscounts = showDiscounts;
} }
public QuoteViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public QuoteViewModel()
{
}
internal static QuoteViewModel CreateQuote(string quoteNumber, ContactViewModel recipient, ContactViewModel salesRep, float vAT = 19f, string quoteType = "ein Analysegerät", bool? brutto = true, int warranty = 12, bool? singlePrices = true, bool? brochures = true, bool? dataSheets = true, bool? mailTemplate = true, string quotePath = "", int validity = 60, bool? showDiscounts = true) internal static QuoteViewModel CreateQuote(string quoteNumber, ContactViewModel recipient, ContactViewModel salesRep, float vAT = 19f, string quoteType = "ein Analysegerät", bool? brutto = true, int warranty = 12, bool? singlePrices = true, bool? brochures = true, bool? dataSheets = true, bool? mailTemplate = true, string quotePath = "", int validity = 60, bool? showDiscounts = true)
{ {
decimal totalListprice = 0, totalDiscount = 0, calcTotalNet = 0; decimal totalListprice = 0, totalDiscount = 0, calcTotalNet = 0;
@ -149,5 +161,10 @@ namespace Gremlin.MVVM
{ {
LineItemsViewModel = LineItemViewModel.ReadLineItemsFromClipboard(); LineItemsViewModel = LineItemViewModel.ReadLineItemsFromClipboard();
} }
public Task HandleAsync(ShellViewModel message, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
} }
} }

@ -1,14 +1,43 @@
namespace Gremlin.MVVM using Caliburn.Micro;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Gremlin.MVVM
{ {
public class ShellViewModel public class ShellViewModel : PropertyChangedBase, IHandle<QuoteViewModel> ,IHandle<AllContactsViewModel>
{ {
public QuoteViewModel QuoteVM { get; set; } private AllContactsViewModel allContactsVM;
public AllContactsViewModel AllContactsVM { get; set; } private QuoteViewModel quoteVM;
private readonly IEventAggregator _eventAggregator;
public QuoteViewModel QuoteVM { get => quoteVM; set => quoteVM = value; }
public AllContactsViewModel AllContactsVM { get => allContactsVM; set => allContactsVM = value; }
public ShellViewModel() public ShellViewModel()
{ {
QuoteVM = new(); QuoteVM = new();
AllContactsVM = new(); AllContactsVM = new();
} }
public ShellViewModel(IEventAggregator eventAggregator)
{
QuoteVM = new QuoteViewModel(eventAggregator);
AllContactsVM = new AllContactsViewModel(eventAggregator);
_eventAggregator = eventAggregator;
_eventAggregator.PublishOnUIThreadAsync($"{AllContactsVM.AllContactsVM.Count} Contacts loaded.");
_eventAggregator.PublishOnUIThreadAsync(12);
}
public Task HandleAsync(AllContactsViewModel message, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task HandleAsync(QuoteViewModel message, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
} }
} }