(這不是BCB的RX)
MSDN Reactive Extension 介紹
textBox的文字在TextChanged事件發生時同步
var res = from evt in Observable.FromEventPattern(textBox1, "TextChanged")
select ((TextBox) evt.Sender).Text;
res.Subscribe(text => textBox2.Text = text);
在Winform表單上繪圖
將滑鼠事件轉成Observable集合後,籍由Subscribe訂閱事件發生時想要的操作,完成一般要分散在三個滑鼠事件中撰寫的工作。
private Point _startPoint;
private void Form1_Load(object sender, EventArgs e)
{
WireUpEvents();
}
private void WireUpEvents()
{
var mouseDown = Observable.FromEventPattern<MouseEventArgs>(this, "MouseDown")
.Select(arg => arg.EventArgs.Location);
var mouseMove = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
.Select(arg => arg.EventArgs.Location);
var mouseUp = Observable.FromEventPattern<MouseEventArgs>(this, "MouseUp")
.Select(arg => arg.EventArgs.Location);
var mouseDrag = mouseMove
.SkipUntil(mouseDown)
.TakeUntil(mouseUp)
.Repeat();
// remember the point when mouse down
mouseDown.Subscribe(pos => _startPoint = pos);
// show dragging position
mouseDrag.Subscribe(pos => label1.Text = pos.ToString());
mouseDrag.Subscribe(currentPos =>
{
using (var g = this.CreateGraphics())
{
if (_startPoint == currentPos)
return;
g.DrawLine(new Pen(Color.Green), _startPoint, currentPos);
_startPoint = currentPos;
}
});
}
另有UI整合的Framework,也是很有趣的一個應用
ReactiveUI(MVVM with Rx)
沒有留言:
張貼留言