【发布时间】:2013-02-10 04:55:28
【问题描述】:
我缺乏经验,尤其是在 MVVM 方面,但我尝试使用 ReactiveUI,但我不理解我发现的演示 ReactiveCommand 的示例。我以前用过 ICommand / DelegateCommand 一次,但这次不一样,我没听懂。
我想要做的非常简单。单击视图中的一个按钮,并让它在视图模型中执行一个方法。我发现的示例都涉及 IObservable,但我不明白,因为它们没有针对我这个完全菜鸟的解释。
基本上,我正在尝试将此作为一种学习体验,而我最理想的做法是将 xaml 中按钮的 Command 属性绑定到一个命令(但我不知道它是否有效),即导致方法执行。没有集合,我只传递一个 int 变量。
感谢您的帮助。我真的很感激。
编辑 - 下面是使用 Paul Betts 建议的代码:
C#
public ReactiveCommand AddToDailyUsed { get; protected set; }
public MainPageVM()
{
Initialize();
AddToDailyUsed = new ReactiveCommand();
AddToDailyUsed.Subscribe(AddToTodayUsedAction => this.AddToDailyUsedExecuted());
}
private object AddToDailyUsedExecuted()
{
MessageBox.Show("AddToDailyUsedAction");
return null;
}
private void AddToDailyUsedAction(object obj)
{
MessageBox.Show("AddToDailyUsedAction");
}
XAML
<Button Content="{Binding Strings.add, Source={StaticResource LocalStrings}}"
Command="{Binding AddToTodayUsed}"
Margin="-5,-10, -10,-10"
Grid.Row="3"
Grid.Column="2" />
显然我错过了一些东西。我在 AddToDailyUsedExecuted 和 AddToDailyUsedAction 方法处插入了断点,但它们从未到达。
编辑视图后面代码的构造函数:
MainPageVM mainPageVM = new MainPageVM();
public MainPage()
{
InitializeComponent();
Speech.Initialize();
DataContext = mainPageVM;
ApplicationBar = new ApplicationBar();
TaskRegistration.RegisterScheduledTask();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
//Shows the rate reminder message, according to the settings of the RateReminder.
(App.Current as App).rateReminder.Notify();
}
【问题讨论】:
-
这看起来正确 - 你能告诉我你的 CodeBehind 的构造函数吗?
标签: mvvm windows-phone-8 windows-phone reactiveui