Bubble events in viewmodels using Caliburn.Micro - c#

I have a screen viewmodel with a search field and some results, composed of multiple, smaller viewmodels: a control for the search field, and a control instance for each result.
My "container" viewmodel (with the search and results) looks like this:
[Export(typeof(ShippingViewModel))]
public class ShippingViewModel : Screen, IHandle<SearchReferenceEvent>
{
private readonly IEventAggregator events;
[ImportingConstructor]
public ShippingViewModel(IEventAggregator events)
{
this.events = events;
this.Search = new QuickSearchViewModel(this.events);
}
public QuickSearchViewModel Search { get; set; }
public void Handle(SearchReferenceEvent message)
{
System.Windows.MessageBox.Show(message.Reference);
}
}
And the "quick search" (with the search fields) viewmodel:
[Export(typeof(QuickSearchViewModel))]
public class QuickSearchViewModel : PropertyChangedBase
{
private readonly IEventAggregator events;
private string currentSearch;
[ImportingConstructor]
public QuickSearchViewModel(IEventAggregator events)
{
this.events = events;
}
public string CurrentSearch // bound to the search field
{
get
{
return this.currentSearch;
}
set
{
this.currentSearch = value;
this.NotifyOfPropertyChange(() => this.CurrentSearch);
}
}
public void SearchReference(string reference) // bound to the search button
{
this.events.Publish(new SearchReferenceEvent(reference));
}
}
The view is displayed as expected, and the search button triggers the SearchReference method.
But the event doesn't seem to be bubbled up to the ShippingViewModel, the Handle is never triggered.
Thanks!

Looks like you're never subscribing to the events in your ShippingViewModel. Try modifying your constructor like so:
[ImportingConstructor]
public ShippingViewModel(IEventAggregator events)
{
this.events = events;
this.events.Subscribe(this); // <= register to receive events
this.Search = new QuickSearchViewModel(this.events);
}

Related

How to pass values between ViewModels using Caliburn.micro EventAggregator MVVM

I am trying to understand and implement EventAggregator using Caliburn micro. I am a new student of MVVM.
My goal: I want to create a window/popup that will be used to update customers. I want to make it possible to update customers from different usercontrols using the same window. I could be completely off the mark here, so please forgive my lack of knowledge.
My Event Class:
private CustomerModel _selectedCustomer;
public SelectedCustomerEvent(CustomerModel selectedCustomer)
{
_selectedCustomer = selectedCustomer;
}
public CustomerModel SelectedCustomer { get { return _selectedCustomer; } }
My Popup/Window used to update a customer:
private IEventAggregator _events;
public CustomerUpdateViewModel(IEventAggregator events)
{
_events = events;
_events.Subscribe(this);
}
public void Handle(SelectedCustomerEvent message)
{
Customer = message.SelectedCustomer;
}
public CustomerModel Customer { get; private set; }
One of the UserControls that will open the window to update a customer:
private IEventAggregator _events;
public CustomerViewModel(IEventAggregator events)
{
_events = events;
_events.Subscribe(this);
}
..........
private CustomerModel _selectedCustomer;
public CustomerModel SelectedCustomer
{
get { return _selectedCustomer; }
set
{
_selectedCustomer = value;
NotifyOfPropertyChange(() => SelectedCustomer);
_events.PublishOnUIThread(new SelectedCustomerEvent(SelectedCustomer));
}
}
public void UpdateCustomer()
{
WindowManager wm = new WindowManager();
CustomerUpdateViewModel cn = new CustomerUpdateViewModel();
wm.ShowWindow(cn);
}
My issue right now is that I get an error on "new CustomerUpdateViewModel()" that says: There is no argument given that corresponds to the required formal parameter 'events' of CustomerUpdateViewModel(IEventAggregator).
I figured this out. It wasnt a problem with the eventaggregator, but with the navigation. I needed to put the WindowManager and the ViewModel in the constructor like so:
public CustomerViewModel(IEventAggregator events, IWindowManager windowManager, CustomerUpdateViewModel customerUpdateVM)
{
_windowManager = windowManager;
_customerUpdateVM = customerUpdateVM;
_events = events;
_events.Subscribe(this);
}
and change the method to open the window:
public void UpdateCustomer() => _windowManager.ShowDialog(_customerUpdateVM);
As the error describes, Constructor for CustomerUpdateViewModel requires a parameter of type IEventAggregator. This is missing in the code.
Constructor of CustomerUpdateViewModel:
public CustomerUpdateViewModel(IEventAggregator events)
{
_events = events;
_events.Subscribe(this);
}
Call to constructor in OP, with argument missing:
public void UpdateCustomer()
{
WindowManager wm = new WindowManager();
CustomerUpdateViewModel cn = new CustomerUpdateViewModel(); // Argument missing
wm.ShowWindow(cn);
}
The way you have fixed it in your own answer was by injecting an instance of CustomerUpdateViewModel into the CustomerViewModel. This might be an unnecessary step, if for example, you do not navigate to CustomerUpdateViewModel every time you display CustomerViewModel.
Instead, you could you still use your initial approach and initialize an instance of CustomerUpdateViewModel only when you actually need it. You only need to pass an instance of IEventAggregator to it.
For example:
public void UpdateCustomer()
{
WindowManager wm = new WindowManager();
CustomerUpdateViewModel cn = new CustomerUpdateViewModel(_events);
wm.ShowWindow(cn);
}

Prism. Closing a dialog created with IDialogService

I am trying to use a new IDialogService which was discussed in github issue 1666. A New IDialogService for WPF. I like this new feature but I can't find a solution for one case of using IDialogService in compare with InteractionRequest.
There is a button, pressing on which non-modal dialog is opened. If user press the same button one more time, while dialog still open, dialog close. How this behavior should be implemented in a proper way?
MainWindowViewModel
public class MainWindowViewModel : BindableBase
{
private readonly IDialogService _dialogService;
public DelegateCommand CustomPopupCommand { get; }
public MainWindowViewModel(IDialogService dialogService)
{
_dialogService = dialogService;
CustomPopupCommand = new DelegateCommand(OpenClosePopup);
}
private void OpenClosePopup()
{
// It looks like some additional logic should be implemented here.
// How to save previously opened IDialogAware instance and close it if needed?
_dialogService.Show("CustomPopupView", new DialogParameters("Title=Good Title"), result => { });
}
}
CustomPopupViewModel
public class CustomPopupViewModel : BindableBase, IDialogAware
{
private string _title;
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
public DelegateCommand<object> CloseCommand { get; }
public CustomPopupViewModel()
{
CloseCommand = new DelegateCommand<object>(CloseDialog);
}
public event Action<IDialogResult> RequestClose;
public void OnDialogOpened(IDialogParameters parameters)
{
Title = parameters.GetValue<string>(nameof(Title));
}
public void OnDialogClosed()
{
}
public bool CanCloseDialog()
{
return true;
}
public void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose?.Invoke(dialogResult);
}
private void CloseDialog(object button)
{
RaiseRequestClose(
new DialogResult(button is ButtonResult buttonResult ? buttonResult : ButtonResult.Cancel));
}
}
I have no idea how can it be implemented in proper way because method IDialogService.Show() fully decoupled from knowing about ViewModel and View. Of course except the name of View.
You can always send an event through the event aggregator, probably you have to pass some id in the dialog parameters to close the right dialog if there's more than one open at a time.
But this feels really clunky, I'd prefer to get an IDisposable from Show/ShowDialog that closes the dialog on Dispose.
public CustomPopupViewModel(IEventAggregator eventAggregator)
{
eventAggregator.GetEvent<CloseDialogEvent>().Subscribe( id => { if (id == _id) CloseMe(); } );
}
public void OnDialogOpened(IDialogParameters parameters)
{
_id = parameters.GetValue<string>("id");
}
_dialogService.Show("CustomPopupView", new DialogParameters("id=12345"), result => { });
_eventAggregator.GetEvent<CloseDialogEvent>().Publish("12345");
I find it simplest to use Prism implementation of the subscriber pattern
I use a class that will be used in the pattern and is communicated:
public class DialogStatus
{
public bool DialogResult { get; set; }
}
In my sample, I show you how I do this using a Login Dialog in WPF using Prism 8.0.0.1909
in the App.cs
protected override void OnInitialized()
{
var login = Container.Resolve<LoginDialog>();
var result = login.ShowDialog();
if (result.HasValue && result.Value)
{
base.OnInitialized();
}
else
{
Application.Current.Shutdown();
}
}
in LoginDialog.cs in my Dialogs folder
public partial class LoginDialog : Window
{
public LoginDialog(IEventAggregator eventAggregator)
{
InitializeComponent();
eventAggregator.GetEvent<CloseDialogWindowEvent>().Subscribe(OnCloseWindow);
}
private void OnCloseWindow(DialogStatus obj)
{
base.DialogResult = obj.DialogResult;
}
}
now anywhere in my code, in a ViewModel of view a custom control's view model, the only thing I need to do is pass the IEventAggregator in in the constructor and save it in a field.
private readonly IEventAggregator _eventAggregator;
public LoginControlViewModel(IAuthenticationService authenticationService
, IConnectFileImporterService connectFileImporterService
, IDialogService dialogService
, IEventAggregator eventAggregator)
{
_eventAggregator= eventAggregator;
// the other code
}
I can now close my dialog, and in this sample return true to falls to my OnInitalize in my App.cs from anywhere by calling
_eventAggregator.GetEvent<CloseDialogWindowEvent>().Publish(new CloseDialogWindowEvent() { DialogResult = true });
or
_eventAggregator.GetEvent<CloseDialogWindowEvent>().Publish(new CloseDialogWindowEvent() { DialogResult = false});
If i understand correctly, you want to close the dailog window programmatically instead of clicking the windows's close button, right? If It is true, maybe I can provide you with a solution. Although this method is not very elegant, it is very simple.
My project use mahapps styles, I want use metrowindow as the dailoghost window. Following prism documentation, I register dialoghost window and usercontrol like this:
containerRegistry.RegisterDialogWindow<DialogHost>(nameof(DialogHost));
containerRegistry.RegisterDialog<UserEdit, UserEditViewModel>(nameof(UserEdit));
The UserEidt is a usercontrol, I place a confirm button and a cancel button in UserEidt, and both button binding DelegateCommand in UserEditViewModel. The question is, how can i close dailogwindow by clicking the cancel button?
Here is my solution, firstly define a IDailogViewModel interface:
public interface IDialogViewModel
{
Action CloseDialogWindow { get; set; }
}
Then UserEditViewModel implement this interface:
public class UserEditViewModel : BindableBase, IDialogAware,IDialogViewModel
{
public DelegateCommand CancelCmd { get; private set; }
public Action CloseDialogWindow { get; set; }
public UserEditViewModel()
{
CancelCmd = new DelegateCommand(CloseDialogWindow)
}
private void CloseDialogWindow()
{
CloseDialogWindow.Invoke();
}
}
Infact, when the dialog window popup, the UserEdit will be dialogWindow's content. So in the dialogwindow's loaded event handler, i can get the UserEdit object by using Window.Content, here is the code:
public partial class DialogHost : MetroWindow, IDialogWindow
{
public DialogHost()
{
InitializeComponent();
}
public IDialogResult Result { get; set; }
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
var dialogVM = (IDialogViewModel)((UserControl)Content).DataContext;
dialogVM.CloseDialogWindow += CloseDialogWindow;
}
void CloseDialogWindow()
{
Close();
}
}
Now,after clicking the cancel button, the dialogwindow will be close.

Caliburn Micro publish / subscribe

I'm working through the following tutorial http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/
and I'm currently stuck at the publish / subscribe part.
I have everything set up, so that it should actually publish the events but the subscribing viewmodel doesn't get the message.
I've done the following:
Publishing ViewModel:
[Export(typeof(ColorViewModel))]
public class ColorViewModel : PropertyChangedBase
{
private readonly IEventAggregator events;
[ImportingConstructor]
public ColorViewModel(IEventAggregator events)
{
this.events = events;
}
public void Red()
{
this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Red)));
}
public void Green()
{
this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Green)));
}
public void Blue()
{
this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Blue)));
}
}
Subscribing ViewModel:
[Export(typeof(AppViewModel))]
public class AppViewModel : PropertyChangedBase, IAppViewModel, IHandle<ColorEvent>
{
private IEventAggregator events;
[ImportingConstructor]
public AppViewModel(ColorViewModel colorViewModel, IEventAggregator events)
{
this.ColorViewModel = colorViewModel;
this.events = events;
this.events.Subscribe(this);
}
public ColorViewModel ColorViewModel { get; private set; }
private SolidColorBrush color;
public SolidColorBrush Color
{
get
{
return this.color;
}
set
{
this.color = value;
this.NotifyOfPropertyChange(() => this.Color);
}
}
public void Handle(ColorEvent message)
{
this.Color = message.Color;
}
}
There are 3 radio buttons on the ColorView which I can click and I do get into the Red(), Green(), Blue() methods so that the PublishOnUIThread is called.
But I never reach the Handle(ColorEvent) method of the AppViewModel.
Am I missing something or why doesn't my handle method gets called after publishing the ColorEvents?
Thanks in advance
Where is the event aggregator coming from? Is it the same instance shared between AppViewModel and ColorViewModel?
Make sure the event aggregator is registered as a singleton in the dependency injector.

Caliburn micro ViewModel is not picking up a message from another ViewModel

Following my learning of MVVM using Caliburn.micro framework... I'm trying to communicate two viewModels sending data through the EventAggregator like this (code with "no sense", just for test):
MainWindowViewModel.cs
namespace TOP
{
[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
readonly IWindowManager windowManager;
private readonly IEventAggregator events;
private bool _Logged;
[ImportingConstructor]
public MainWindowViewModel(IWindowManager windowManager, IEventAggregator events)
{
DisplayName = "TOP";
this.events = events;
events.Subscribe(this);
this.windowManager = windowManager;
windowManager.ShowDialog(new LoginViewModel(events));
}
public bool Logged
{
get { return _Logged; }
set
{
_Logged = value;
if(_Logged== true)
InitiateApp();
}
}
public void Handle(LoginEvent message)
{
Logged = message.Logged;
}
private void InitiateApp() {
ActivateItem(new TwoWindowViewModel());
}
}
}
LoginViewModel.cs
namespace TOP{
[Export(typeof(IScreen))]
public class LoginViewModel : Screen
{
private readonly IEventAggregator _events;
[ImportingConstructor]
public LoginViewModel(IEventAggregator events)
{
DisplayName = "Login";
_events = events;
Login();
}
public void Login()
{
_events.Publish(new LoginEvent(true));
}
}
}
LoginEvent.cs
namespace TOP
{
public class LoginEvent
{
public LoginEvent(bool logged)
{
Logged = logged;
}
public bool Logged { get; private set; }
}
}
Why the Handle method of MainWindowViewModel is not picking up the published message from LoginViewModel?
Thank you for your responses.
Your MainWindowViewModel needs to implement IHandle<LoginEvent>. You already defined the method with the correct signature, so you only are missing the part where you actually tell the compiler that you implement the interface:
public class MainWindowViewModel
: Conductor<IScreen>.Collection.OneActive, IHandle<LoginEvent>
More info can be found in the documentation.

Return value of a Property from child ViewModel to parent ViewModel

In my WPF MVVM app, using Caliburn.Micro, I have a ViewModel, CreateServiceViewModel that, on a button click, opens a GridView in a seperate window for the User to chose a Row from.
I created another ViewModel for this, MemberSearchViewModel which has two properties:
private Member selectedMember;
public Member SelectedMember
{
get { return selectedMember; }
set { selectedMember = value; }
}
private IList<Member> members;
public IList<Member> Members
{
get { return members; }
set { members = value; }
}
How do I get that SelectedMember value back to the calling ViewModel? That ViewModel has a property of Service.SelectedMember.
EventAggregator is what you could use... One of many solutions I am sure.
public class MessageNotifier{
public object Content{get;set;}
public string Message {get;set;}
}
//MEF bits here
public class HelloWorldViewModel: Screen, IHandle<MessageNotifier>{
private readonly IEventAggregator _eventAggregator
//MEF constructor bits
public YourViewModel(IEventAggregator eventAggregator){
_eventAggregator = eventAggregator;
}
public override OnActivate(){
_eventAggregator.Subscribe(this);
}
public override OnDeactivate(){
_eventAggregator.UnSubscribe(this);
}
//I Handle all messages with this signature and if the message applies to me do something
//
public void Handle(MesssageNotifier _notifier){
if(_notifier.Message == "NewSelectedItem"){
//do something with the content of the selectedItem
var x = _notifier.Content
}
}
}
//MEF attrs
public class HelloWorld2ViewModel: Screen{
private readonly IEventAggregator _eventAggregator
//MEF attrs
public HelloWorld2ViewModel(IEventAggregator eventAggregator){
_eventAggregator = eventAggregator;
}
public someobject SelectedItem{
get{ return _someobject ;}
set{ _someobject = value;
NotifyOfPropertyChange(()=>SelectedItem);
_eventAggregator.Publish(new MessageNotifier(){ Content = SelectedItem, Message="NewSelectedItem"});
}
}
One option is to utilize NotifyPropertyChanged. Since you are working with ViewModels, they most likely implement INotifyPropertyChanged, which you can make use of just as the framework does.
When your CreateServiceViewModel creates the MemberSearchViewModel, it would just subscribe to the PropertyChanged event:
//This goes wherever you create your child view model
var memberSearchViewModel = new MemberSearchViewModel(); //Or using a service locator, if applicable
memberSearchViewModel.PropertyChanged += OnMemberSearchPropertyChanged;
private void OnMemberSearchPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == "SelectedMember")
{
//Code to respond to a change in the Member
}
}
And then in your MemberSearchViewModel, you simply raise the NotifyPropertyChanged event when the user has selected a member from the grid.
EDIT:
As #DNH correctly notes in the comments, using event handlers like this can lead to memory leaks if not properly cleaned up. So when you are finished with the MemberSearchViewModel, make sure to unsubscribe to the PropertyChanged event. So for example, if you only need it until the user selects a member, you could put it inside the Property Changed Handler itself (I've switched it to use a class-level variable to hold the ViewModel):
private void OnMemberSearchPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == "SelectedMember")
{
//Code to respond to a change in the Member
//Unsubscribe so the view model can be garbage collected
_memberSearchViewModel.PropertyChanged -= OnMemberSearchPropertyChanged;
_memberSearchViewModel = null;
}
}
One option would be to store MemberSearchViewModel as a field of CreateServiceViewModel and define CreateServiceViewModel.SelectedMember property as follows:
public Member SelectedMember
{
get
{
return _memberSearchViewModel.SelectedMember;
}
set
{
_memberSearchViewModel.SelectedMember = value;
}
}
How about?
public interface INotifyMe<T>
{
T ResultToNotify { get; set; }
}
public class CreateServiceViewModel : ViewModelBase, INotifyMe<Member>
{
// implement the interface as you like...
}
public class MemberSearchViewModel : ViewModelBase
{
public MemberSearchViewModel(INotifyMe<Member> toBeNotified)
{
// initialize field and so on...
}
}
Now you could let listen CreateServiceViewModel to its own property and you won't have to think about the removal of the event listener.
Well of course to do the more classical way you could alternatively use an interface like this.
public interface INotifyMe<T>
{
void Notify(T result);
}
As a follow-up to my comment, here's an example using Prism - I've never used Caliburn.
Create an event - the event's payload will be your SelectedMember:
public class YourEvent:CompositePresentationEvent<YourEventPayload>{}
Publish the event:
EventAggregator.GetEvent<YourEvent>().Publish(YourEventPayload);
Subscribe to the event:
EventAggregator.GetEvent<YourEvent>().Subscribe((i) => ...);

Categories