Composite UI 中发布/订阅如何使用?
我想使用发布/订阅实现系统间(同一进程内)不同窗体之间的通信,但发现它不能正常工作。
之后,我做了一个简单的测试程序:
创建一个 windows application 项目,
在系统自动创建的Form1(类)上添加CompositeUI中事件代理的引用
标记发布、订阅。
Form1的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.Utility;
namespace PS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OnTextChanged(tbPub.Text);
}
[EventSubscription("topic://EventBrokerQuickStart/TextChanged", Thread = ThreadOption.UserInterface)]
public void OnSensingTextChanged(object sender, DataEventArgs<string> e)
{
tbSub.Text = "sssss" + e.Data;
}
[EventPublication("topic://EventBrokerQuickStart/TextChanged", PublicationScope.Global)]
public event EventHandler<DataEventArgs<string>> TextChangedEvent;
private void OnTextChanged(string strText)
{
if (TextChangedEvent != null)
{
TextChangedEvent(this, new DataEventArgs<string>(strText));
}
}
}
}
在调试时,发现TextChangedEvent始终为null。
而MS 或者其他人提供的例子中对应物则 不是 null。
不知道,使用发布/订阅还需要有什么条件。
请各位高手赐教。