我有一个带有命令绑定和使用Prism库的文本块。
这是XAML的一部分:
<TextBlock Margin="0,10,0,0">SocialSecurityNumer:</TextBlock>
<TextBox Name="SSNText" GotFocus="TextBox_GotFocus" Text="{Binding SSN, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0"/>
这是后面的ViewModel:
public FindViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
}
public DelegateCommand FindCommand { get; set; }
private string ssn;
public string SSN
{
get { return ssn; }
set
{
if (ssn== value)
return;
ssn = value;
RaisePropertyChanged(() => SSN);
FindCommand.RaiseCanExecuteChanged();
}
}
这是GridViewModel,它监听此事件触发器并使用SSN作为参数启动函数
public class GridViewModel : NotificationObject
{
public GridViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
eventAggregator.GetEvent<SSNChangedEvent>().Subscribe(GetData);
}
public ObservableCollection<Investment> Investments { get; set; }
private void GetData(string ssn)
{
var list = GeniusConnection.GetDataFromWebService(ssn);
Investments = new ObservableCollection<Investment>(list);
RaisePropertyChanged(() => Investment);
}
}
如何添加其他参数,例如datetime参数,让我困惑的部分是:
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
这个Publish功能只需要一个参数,因此我不知道如何轻松添加多个参数。??
答案 0 :(得分:5)
您应该创建一个包含您要发布的所有必要参数的类。
public class SSNChangedEventParams
{
public string SSN{get;set;}
public DateTime Dt{get;set;}
...
}
然后发布此类的实例:
eventAggregator.GetEvent<SSNChangedEvent>().Publish(new SSNChangedEventParams(){SSN=SSN, Dt = DateTime.Now})