Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

[Caliburn.Micro] Passing Control Event to ShellView

0.00/5 (No votes)
6 Sep 2012 1  
How to pass Control Event to ShellView (Main Page) using Caliburn.Micro?

Introduction

In Caliburn.Micro, we have a series of supporting services for building presentation tiers. Among them is the EventAggregator, a service which supports in-process publish/subscribe. Here I explain how to pass (Publish) event from Control to Main Page (ShellView) and How to subscribe event in ShellviewModel.

Let’s start by having a look at the ItemClickEvent class:

First add ItemClickEvent class and write there:

public string ToDoItemDescription { get; set; }

public ItemClickEvent(string toDoItemDescription)
{
	ToDoItemDescription = toDoItemDescription;
}

After creating ItemClassEvent class, add new Item PageOneView which contains a Simple Button and add PageOneViewModel which contains Button click like:

public class PageOneViewModel : Conductor<screen>.Collection.OneActive
{
	public void ClickMe()
       {
       	IoC.Get<ieventaggregator>().Publish(new ItemClickEvent("Button Click"));
		// Here you can pass your custom value and variable in ItemClassEvent().
       }
} 

Finally ShellViewModel looks like:

namespace EventPassingToShellView
{
    [Export(typeof(IShell))]
    public class ShellViewModel : Conductor<screen>
    .Collection.OneActive, IShell, IHandle<itemclickevent>
    {
        public ShellViewModel()
        {
            ShowPageOne();
            IoC.Get<ieventaggregator>().Subscribe(this);

        }
        public void ShowPageOne()
        {
            ActivateItem(new PageOneViewModel());
        }
        
        #region IHandle<itemclickevent> Members

        public void Handle(ItemClickEvent message)
        {
		// Got message from  PageOneViewModel what ever you pass it.
           MessageBox.Show(message.ToDoItemDescription);
        }

        #endregion
    }
}   

Finally Project Structure looks like:

Project Structure

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here