您的位置:首页 > 其它

Send Custom Event to Host (Windows Form) from a WPF Control -Part 2

2013-06-17 23:08 513 查看
Send Custom event to Host (Windows Form) from a WPF Control - Part 2



In the previous articles we have seen how to host a
WPF control into the Windows Form and send the
standard events to Windows form from WPF control. In this article we would see how custom event from WPF control could be passed to hosting windows form.



There are 3 steps involve to do achieve this.



Step 1: Create the custom Event and its argument:

To create any custom argument for the event it has to be derived from EventArgs. These arguments have to be available to the source and receivers so it has to be declared in the library project.



Create the Class Library Project and name it CustomEvents.
Rename the class1 file to Controller
Add the CustomArgs class and derive it from EventArgs as shown here.

public class CustomArgs : EventArgs
    {
        private string _userName;
        public CustomArgs(string userName)
        {
            this._userName = userName;
        }

        // This is a straightforward implementation for
        // declaring a public read only field
        public string UserName
        {
            get
            {
                return this._userName;
            }
        }
 }


Add the Custom delegate and its Event

///<summary>

/// CustomMsgEventHandler Delegate type defining the prototype of the callback method that receivers must implement.

///</summary>

public delegate void CustomMsgEventHandler(

            Object sender, CustomArgs args);

 

///<summary>

/// Event EventCustomMsg, which the subscriber would listen and source would raise this event

///</summary>

public event CustomMsgEventHandler EventCustomMsg;


Add the Public method used by the source who would raise the event

public void NewCustomMsg(Object sender, CustomArgs e)
        {
            // Has any objects registered interest with our event?
            if (EventCustomMsg != null)
            {
                // Yes, notify all the objects
                EventCustomMsg(sender, e);
            }
     }




Step2. Create the WPF control project to raise the custom event to windows Form Application:

Create WpfUserControlLib project in Visual Studio 2008 described in the use
WPF control in Windows Form.
Add the reference of the CustomEvents project created in the first step.
Create the class member of the Controller class

private WPFCustomEvents.Controller _controller;


Add the constructor and initialize the controller class object. Same Controller object has to shared by all source and target classes. Its better create the singleton object of Controller class.

public UserControl1(WPFCustomEvents.Controller controller)
{
     InitializeComponent();
     _controller = controller;
}




Add the Event Handler for the btnOk and btnCancl button in the WPF Library

private void btnOK_Click(object sender, RoutedEventArgs e)
{

}


private void btnCancel_Click(object sender, RoutedEventArgs e)
{

}




Add the public property in the UserControl1.xaml.cs file to share the value of the textbox with the host

public string UserName
{
       get { return txtName.Text; }
       set { txtName.Text = value; }
}




Declare the Cancel Button, which can be subscribed by Windows Form. For the Ok button events already declared in the controller class

public event EventHandler CancelClick;


Now add the code to the event handler so that we can raise the event to host also.

In the OK button event create the custom argument and fills all the members and raise the custom event.

private void btnOK_Click(object sender, RoutedEventArgs e)
{
WPFCustomEvents.Controller.CustomArgs eCustomArg = new WPFCustomEvents.Controller.CustomArgs(txtName.Text);
_controller.NewCustomMsg(this, eCustomArg);
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
            if (CancelClick != null)
                CancelClick(this, e);
}




Step 3: Handle the WPF Control’s Custom Event in Windows Form:

Create WinFormWPFHostApp Windows Form project in Visual Studio 2008 as described in the article to
use the WPF control in windows Form.

Add the reference of the WpfUserControlLib and CustomEvents
projects.
In the constructor create the object of the Controller class and pass it to WpfUserControlLib’s WPF control.

public Form1()
{
   InitializeComponent();
   _controller = new WPFCustomEvents.Controller();
  elementHost1.Dock = DockStyle.Fill;
   _WPFUserControl = new WpfUserControlLib.UserControl1(_controller);
   _controller.EventCustomMsg += new WPFCustomEvents.Controller.CustomMsgEventHandler(OnCustomMsg);
   _WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);
   elementHost1.Child = _WPFUserControl;
}


Add Handler to OKClick and Cancel click Event just after creating the instance of the user control.

_WPFUserControl.OkClick += new EventHandler(OnOkHandler);
  _WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);


Write code in the handler method. If you see in OnCustomMsg I am getting all the information along with Custom event and argument.

protected void OnCustomMsg(object sender, WPFCustomEvents.Controller.CustomArgs e)
{
MessageBox.Show("Hello: " + e.UserName + " you clicked Ok Button");
}

protected void OnCancelHandler(object sender, EventArgs e)
{
            MessageBox.Show("you clicked Cancel Button");
}



Compile and Run the Windows Application you would see the WPF control is hosted in the Windows Control, Enter your name in the Text box and click OK and Cancel Button to see you are getting back the event from WPF control including the custom event and custom
arument on ok button.



Summary: We can use the power of the WPF Control’s visual elements into the Windows Form application by hosting very easily. You can share the data and send the event (both custom and existing) to host from the WPF control.

转自【http://www.a2zdotnet.com/View.aspx?Id=94#.Ub8hTJyvy3I】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐