我使用Reactive Extensions制作了一个示例应用程序。
我正在尝试根据文本框的内容获得不断更新的建议单词列表。但是,没有结果发送给我的观察员。
我的代码出了什么问题?
var ts = Observable.FromEventPattern<EventArgs>(textBox1, "TextChanged");
string dicWord = string.Empty;
var input = (from es in ts
select ((TextBox)es.Sender).Text)
.DistinctUntilChanged()
.Throttle(TimeSpan.FromSeconds(1));
input.ObserveOn(SynchronizationContext.Current)
.Subscribe(x =>
{
label1.Text = x.ToString();
dicWord = x.ToString();
});
var pex = new ServiceReference1.DictServiceSoapClient("DictServiceSoap");
var match = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
( pex.BeginMatchInDict, pex.EndMatchInDict );
var lookup = new Func<string, IObservable<DictionaryWord[]>>(
word => match("wn", label1.Text, "prefix"));
var res = from term in input
from words in lookup(term)
select words;
using (res.ObserveOn(SynchronizationContext.Current).Subscribe(words =>
{
listBox1.Items.Clear();
listBox1.Items.AddRange((from word in words select word.Word).ToArray());
}))
答案 0 :(得分:3)
您的使用块会立即处理您的订阅。 Rx工作正常; - )
答案 1 :(得分:0)
我建议您尝试在http://download.microsoft.com/download/C/5/D/C5D669F9-01DF-4FAF-BBA9-29C096C462DB/Rx%20HOL%20.NET.pdf的官方Rx Hands on labs中使用相同的教程。
它是在Rx 1.0时间框架中编写的,但你应该能够在Rx 2.1中相对轻松地工作,希望它能为你填补空白。