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
{
private readonly SimpleContainer _container = new();
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
//LogManager.GetLog = type => new DebugLog(type); //DEBUGGING
DisplayRootViewFor<ShellViewModel>();
}
}

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

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

@ -2,10 +2,12 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Gremlin.MVVM
{
public class QuoteViewModel : PropertyChangedBase
public class QuoteViewModel : PropertyChangedBase, IHandle<ShellViewModel>
{
private string _quoteType = "ein Analysegerät";
private string _quotePath;
@ -27,6 +29,7 @@ namespace Gremlin.MVVM
private bool _quoteContains3PP;
private bool _quoteContainsRB;
private bool? _showDiscounts = true;
private IEventAggregator _eventAggregator;
public string QuoteType { get => _quoteType; internal set { _quoteType = value; NotifyOfPropertyChange(() => QuoteType); } }
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)
{
@ -82,6 +85,15 @@ namespace Gremlin.MVVM
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)
{
decimal totalListprice = 0, totalDiscount = 0, calcTotalNet = 0;
@ -149,5 +161,10 @@ namespace Gremlin.MVVM
{
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; }
public AllContactsViewModel AllContactsVM { get; set; }
private AllContactsViewModel allContactsVM;
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()
{
QuoteVM = 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();
}
}
}