TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dwayne Barsotta
NA
13
5.4k
Caliburn Micro - Event Aggregator
Jan 28 2018 10:26 PM
I am trying to learn how I can pass data from a SecondWindowViewModel back to the ShellViewModel. It looks like using messaging through event aggregator is what I want. I have tried to get this to work on my primary project located on git hub:
I could not get it to work. I have created a much simpler program. T
The test program in here:
https://github.com/Dev-Mech/CaliburnMicro.git
Can anyone offer a good tutorial on how to accomplish this, before I start messing around with the code again (add screwing it all up again)? Please see the code in "ScheduleReview" for my first royal screw up. My OCD is going crazy ATM.
FYI - I deleted useless information from the original post.
---------------- Edited ---------------------------
This morning I rebuilt a simple application with two ViewModels. Both have a text box and a button. In the starting viewmodel (ShellViewModel), when you click the "Click Me" button the second window opens. Here the user types any text and clicks send.
I created a POCO to hold the data I want to send back (I know I could use just string, but my primary application requires sending more than one string, so I figured best to have it all stored in one object).
I followed the directions from this page - right from the Calibrn.Micro authors:
https://caliburnmicro.com/documentation/event-aggregator
The first time I ran the program VS errored with "no parameterless constructor for ShellViewModel" and stopped in the AppBootstrapper:
DisplayRootViewFor
();
So I created a parameterless constructor, the program runs normally until I hit send on the second window. The program just breaks. I think it is because I had to add the parameterless constructor in ShellViewModel and I am actually sending a "null" "_eventAggregator" to the second window.
I confirmed all my bindings are working.
My code is below as well as on GitHub:
https://github.com/Dev-Mech/CaliburnMicro.git
AppBootstrapper:
namespace
CaliburnMicro
{
class
AppBootstrapper : BootstrapperBase
{
private
readonly
SimpleContainer _container =
new
SimpleContainer();
public
AppBootstrapper()
{
Initialize();
}
protected
override
void
OnStartup(
object
sender, StartupEventArgs e)
{
DisplayRootViewFor
();
}
protected
override
void
Configure()
{
_container.Singleton
();
}
}
}
ShellViewModel:
namespace
CaliburnMicro.ViewModels
{
class
ShellViewModel : Screen, IHandle
{
private
string
_messageBox;
private
readonly
IEventAggregator _eventAggregator;
public
string
MessageBox
{
get
{
return
_messageBox; }
set
{
_messageBox = value;
NotifyOfPropertyChange(() => MessageBox);
}
}
public
ShellViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(
this
);
}
public
ShellViewModel()
{
}
public
void
OpenWindow()
{
WindowManager wm =
new
WindowManager();
SecondWindowViewModel swm =
new
SecondWindowViewModel(_eventAggregator);
wm.ShowWindow(swm);
}
public
void
Handle(EventMessage message)
{
MessageBox = message.Text;
}
}
}
SecondWindowViewModel:
(BTW I attempted to change "PublishonUIThread" to whats in this code)
namespace
CaliburnMicro.ViewModels
{
class
SecondWindowViewModel: Screen
{
private
string
_secondTextBox;
private
readonly
IEventAggregator _eventAggregator;
public
EventMessage Tosend =
new
EventMessage();
public
string
SecondTextBox
{
get
{
return
_secondTextBox; }
set
{
_secondTextBox = value;
NotifyOfPropertyChange(() => SecondTextBox);
}
}
public
SecondWindowViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public
void
SendBack()
{
Tosend.Text = SecondTextBox;
_eventAggregator.PublishOnCurrentThread(Tosend);
Thread.Sleep(1000);
//I wanted the app to wait a second before closing
TryClose();
}
}
}
Attachment:
SceheduleReview.zip
Reply
Answers (
0
)
Hosting a Windows Control in WPF Application
WPF application with MVVM