C# 将MVVM命令绑定到UWP中的时间选择器控制
我在XAML页面上使用C# 将MVVM命令绑定到UWP中的时间选择器控制,c#,xaml,mvvm,win-universal-app,template10,C#,Xaml,Mvvm,Win Universal App,Template10,我在XAML页面上使用TimePicker控件,并尝试对相应的ViewModel中的更改做出反应。我在我的页面中添加了Xaml.Interactivity和Xaml.Interactivity.Core命名空间,以便在调用TimeChanged事件后使用Behaviors SDK触发命令 <!-- XAML Namespaces --> xmlns:interact="using:Microsoft.Xaml.Interactivity" xmlns:interactcore="us
TimePicker
控件,并尝试对相应的ViewModel
中的更改做出反应。我在我的页面中添加了Xaml.Interactivity
和Xaml.Interactivity.Core
命名空间,以便在调用TimeChanged
事件后使用Behaviors SDK
触发命令
<!-- 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>
初始化页面后,将抛出带有以下消息的InvalidOperationException
Adding or removing event handlers dynamically is not supported on WinRT events.
感谢您的讨论@Archana。我找到了解决问题的办法。 我现在使用编译绑定
x:Bind
直接将事件与ViewModel
中的处理程序连接,而不是使用Binding
与ViewModel
中的命令连接
我更新了时间选择器
控件
<TimePicker x:Name="TestTimePicker" ClockIdentifier="24HourClock" Time="{x:Bind ViewModel.TestTime, Mode=TwoWay}" TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}" />
感谢您的讨论@Archana。我找到了解决问题的办法。
我现在使用编译绑定x:Bind
直接将事件与ViewModel
中的处理程序连接,而不是使用Binding
与ViewModel
中的命令连接
我更新了时间选择器
控件
<TimePicker x:Name="TestTimePicker" ClockIdentifier="24HourClock" Time="{x:Bind ViewModel.TestTime, Mode=TwoWay}" TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}" />
参考此
EventTriggerBehavior不支持TimeChanged事件。支持的事件包括
Tapped
PointerPressed
Loaded
DataContextChanged
Click
Checked
Unchecked
SelectionChanged
TextChanged
Toggled
NavigationCompleted
您必须实现自定义行为以支持TimeChanged事件
这里有一个实现自定义行为的链接参见此
EventTriggerBehavior不支持TimeChanged事件。支持的事件包括
Tapped
PointerPressed
Loaded
DataContextChanged
Click
Checked
Unchecked
SelectionChanged
TextChanged
Toggled
NavigationCompleted
您必须实现自定义行为以支持TimeChanged事件
这里有一个实现自定义行为的链接您现在能告诉我问题出在哪里吗?对不起!问题更新是的,这来自模板10。让我们参考此链接。EventTriggerBehavior不支持TimeChanged事件您能告诉我现在出现了什么问题吗?抱歉!问题更新是的,这来自模板10。让我们参考此链接。EventTriggerBehavior不支持TimeChanged Event很奇怪,这很难实现MVVM。很奇怪,这很难实现MVVM。