1

我在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我使用ViewModelBaseDelegateCommandfrom中的代码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.
4

2 回答 2

3

感谢您的讨论@Archana。我找到了解决我的问题的方法。我现在使用编译绑定直接Binding将事件与Command.ViewModelx:BindViewModel

我更新了TimePicker控件

<TimePicker x:Name="TestTimePicker" ClockIdentifier="24HourClock" Time="{x:Bind ViewModel.TestTime, Mode=TwoWay}" TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}" />

和代码中ViewModel

public void TestTimeChangedEvent (object sender, TimePickerValueChangedEventArgs e)
{
    TestTime = e.NewTime;
}
于 2016-04-05T10:08:30.110 回答
1

参考这个链接

EventTriggerBehavior 不支持 TimeChanged 事件。支持的事件是

    Tapped
    PointerPressed  
    Loaded
    DataContextChanged
    Click
    Checked
    Unchecked
    SelectionChanged
    TextChanged
    Toggled
NavigationCompleted

您必须实现自定义行为以支持 TimeChanged 事件

这是实现自定义行为的链接implement_custom_behaviour

于 2016-04-05T10:09:43.567 回答