我在TimePickerXAML 页面上使用控件并尝试对相应的ViewModel. 我将Xaml.InteractivityandXaml.Interactivity.Core命名空间添加到我的页面,以便在调用事件后使用Behaviors SDK触发命令TimeChanged。
<!-- XAML Namespaces -->
xmlns:interact="using:Microsoft.Xaml.Interactivity"
xmlns:interactcore="using:Microsoft.Xaml.Interactions.Core"
<TimePicker x:Name="TestTimePicker" ClockIdentifier="24HourClock" Time="0">
<interact:Interaction.Behaviors>
<interactcore:EventTriggerBehavior EventName="TimeChanged">
<interactcore:InvokeCommandAction Command="{Binding DataContext.TimeChangedCommand}" CommandParameter="{Binding ElementName=TestTimePicker, Path=Time}" />
</interactcore:EventTriggerBehavior>
</interact:Interaction.Behaviors>
</TimePicker>
对于ViewModel我使用ViewModelBase和DelegateCommandfrom中的代码Template10。
public DelegateCommand<TimeSpan> StartTimeChangedCommand { get; set; }
public TestViewModel()
{
...
TimeChangedCommand = new DelegateCommand<TimeSpan>(TimeChangedAction);
...
}
private void TimeChangedAction(TimeSpan obj)
{
//do something with obj
}
一旦页面被初始化InvalidOperationException,就会抛出以下消息。
Adding or removing event handlers dynamically is not supported on WinRT events.