All
我有一个问题。现在我正在使用MVVM框架来开发Windows Phone 8应用程序。我只想在按下按钮后开始录制一些东西,松开按钮时停止录制,我使用 InvokeCommandAction 在 ViewModel 中绑定命令,这是代码如下
Xaml:
<Button x:Name="BtnRecord" Height="50" Width="180" Background="#D43637" Content="Record" Margin="20,0,0,0" Style="{StaticResource BasicButtonStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding StartRecordCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding EndRecordCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
模型视图:
public ICommand StartRecordCommand
{
get
{
return new DelegateCommand(StartRecord);
}
}
public ICommand EndRecordCommand
{
get
{
return new DelegateCommand(EndRecord);
}
}
private void StartRecord(object parameter){}
private void EndRecord(object parameter){}
当我调试应用程序时,我发现它既没有触发鼠标左按钮向下也没有触发鼠标左按钮打开事件,所以我注册了两个事件处理程序,如下所示:
BtnRecord.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Button_MouseLeftButtonDown), true);
BtnRecord.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Button_MouseLeftButtonUp), true);
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}
好吧,继续,但下一个问题来了,它没有在ViewModel中触发ICommand,它叫Button_MouseLeftButtonDown,哦,上帝,我疯了
有人知道如何在ViewModel中调用ICommand吗?还是另一种实现方式?
ICommand.Execute
.因此,您的处理程序应该是
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
StartRecordCommand.Execute(null);
}
按钮的 IsPressed 属性与 ViewModel 中名为 IsRecording 的 TwoWay 绑定绑定,并根据新的布尔值从资源库内部启动/停止记录逻辑(true 表示开始)。让我知道它是否有效。