我是RX框架的新手。我玩弄它試圖更新Silverlight文本框更新屬性使用INotifyPropertyChanged。我的XAML看起來像這樣:使用Silverlight的反應框架和INotifyPropertyChanged
<Canvas x:Name="LayoutRoot" Background="White">
<Border BorderThickness="2" CornerRadius="5" BorderBrush="Blue" Canvas.Left="12" Width="371">
<TextBlock Height="135" HorizontalAlignment="Left" Margin="12,50,0,0" Name="textBlock1"
Text=""
VerticalAlignment="Top" Width="367" />
</Border>
<Button Content="Post!" Height="23" HorizontalAlignment="Left" Margin="12,210,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="41"
HorizontalAlignment="Left"
Margin="12,247,0,0"
Name="textBox1"
VerticalAlignment="Top"
Width="376"
/>
<sdk:Label Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="Post Window" FontWeight="Bold" FontSize="12" Canvas.Left="14" Canvas.Top="0" />
</Canvas>
在背後說我的代碼,我有:
public partial class MainPage : UserControl
{
Messanger messanger;
public MainPage()
{
InitializeComponent();
}
void messanger_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var textBoxInput = from evt in Observable.FromEvent<PropertyChangedEventArgs>(messanger, "PropertyChanged")
select evt.EventArgs.PropertyName.Equals("PropertyName");
textBoxInput.Subscribe(value => textBlock1.Text = value.ToString());
}
private void button1_Click(object sender, RoutedEventArgs e)
{
messanger = new Messanger();
messanger.PropertyChanged += new PropertyChangedEventHandler(messanger_PropertyChanged);
messanger.Message = textBox1.Text;
}
}
,我創建了另一個類來處理的屬性更改事件
public class Messanger : INotifyPropertyChanged
{
private string MessageValue;
public string Message
{
get { return MessageValue; }
set
{
MessageValue = value;
NotifyPropertyChanged("Message");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我的問題是,我沒有從文本框中的文本中獲得輸出。也讓我知道,如果我使用它不正確...
任何想法?提前致謝!
不會失去使用Rx框架和Observable的目的嗎? – Mage 2011-04-09 05:39:38
我不這麼認爲,你應該做正確的綁定以便可視化正確的數據 – 2011-04-09 06:13:56
你正在做的只是將文本框綁定到文本塊。這很好,但不是我想要做的。我希望Oberserver將數據推送給我(意思是Textblock) – Mage 2011-04-09 17:36:24