绑定滑块控件的事件
在视图(AudioView.xaml
)中,我编写了以下代码
<Slider
Name="AudioSlider"
Width="200"
Height="23"
Grid.Column="0"
IsSelectionRangeEnabled="True"
IsSnapToTickEnabled="True"
Maximum="{Binding Path=TotalAudioPlayingSeconds, Mode=OneTime}"
Minimum="0"
Style="{StaticResource CustomStyleForSlider}"
Thumb.DragCompleted="{Binding AudioSliderChangedCommand}"
TickFrequency="1"
Value="{Binding Path=AudioPosition}"/>
注意:还有文件AudioView.xaml.cs
。
在视图模型类(AudioViewModel.cs
)中,我定义了以下属性
public event DragCompletedEventHandler AudioSliderChangedCommand;
,在视图模型类(AudioViewModel.cs)的构造函数中,
this.AudioSliderChangedCommand = new DragCompletedEventHandler(OnAudioSliderChanged);
在编译过程中,我得到以下内容错误
错误 8 DragCompleted="{Binding AudioSliderChangedCommand}" 不是 有效的。 {Binding AudioSliderChangedCommand} 不是有效事件 处理程序方法名称。仅生成或上的实例方法 代码隐藏类有效。
In the view (AudioView.xaml
) i have written the following code
<Slider
Name="AudioSlider"
Width="200"
Height="23"
Grid.Column="0"
IsSelectionRangeEnabled="True"
IsSnapToTickEnabled="True"
Maximum="{Binding Path=TotalAudioPlayingSeconds, Mode=OneTime}"
Minimum="0"
Style="{StaticResource CustomStyleForSlider}"
Thumb.DragCompleted="{Binding AudioSliderChangedCommand}"
TickFrequency="1"
Value="{Binding Path=AudioPosition}"/>
Note: Also there is file AudioView.xaml.cs
.
In the view model class(AudioViewModel.cs
) i defined the following property
public event DragCompletedEventHandler AudioSliderChangedCommand;
and in the constructor of view model class (AudioViewModel.cs)
this.AudioSliderChangedCommand = new DragCompletedEventHandler(OnAudioSliderChanged);
During the compilation i am getting the following error
Error 8 DragCompleted="{Binding AudioSliderChangedCommand}" is not
valid. {Binding AudioSliderChangedCommand} is not a valid event
handler method name. Only instance methods on the generated or
code-behind class are valid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不在于您的代码隐藏,而在于您的 XAML。在某个地方执行此操作:
这会指示 XAML 反序列化器将
AudioSliderChangedCommand
处理程序附加到DragCompleted
事件。但是,AudioSliderChangedCommand不是具有适当签名的方法(可以作为处理程序附加),并且它不在您的 View 类中。最后,您不能将Binding
用于事件处理程序。要解决此问题,最简单的解决方案是在
View
中执行此操作:并
更改为
在 XAML 中
。这就是上面的工作原理:
DragCompleted
时,将调用View.DragCompletedEventHandler
方法。AudioSliderChangedCommand
来自 ViewModel 的事件(请参阅下面的注释)并引发它,传递原始事件参数重要说明
您似乎对事件、事件处理程序和命令感到困惑。您现在的代码具有误导性。
AudioSliderChangedCommand
是一个事件
,但顾名思义它是一个ICommand
。适当的名称是AudioSliderChanged
。另外,执行此操作的适当 MVVM 方法是使用某种风格的
DelegateCommand
(所有像样的 MVVM 框架都有一个;我在 Prism 中使用类名来实现)。然后,假设AudioSliderChangedCommand
确实是一个命令,视图中的代码隐藏将是:通过使用某种“事件到命令”的风格,也可以完全不使用任何代码隐藏” 附加行为。
The problem is not in your code-behind, but in your XAML. Somewhere you do this:
This instructs the XAML deserializer to attach the
AudioSliderChangedCommand
handler to theDragCompleted
event. However,AudioSliderChangedCommand
is not a method with the appropriate signature (which can be attached as a handler) and it is not in your View class. And finally, you can't useBinding
for event handlers.To solve this, the simplest solution is to do this in your
View
:and also change
to
in your XAML.
This is how the above will work:
DragCompleted
is raised, the methodView.DragCompletedEventHandler
will be calledAudioSliderChangedCommand
event (see note below) from the ViewModel and raise it, passing the original event argsImportant note
You seem to be confused about events, event handlers and commands. Your code as it stands is misleading.
AudioSliderChangedCommand
is anevent
, but the name suggests it's anICommand
. The appropriate name would beAudioSliderChanged
.Also, the appropriate MVVM way of doing this is by using some flavor of
DelegateCommand
(all decent MVVM frameworks have one; I used the class name for the implementation in Prism). Then, assuming thatAudioSliderChangedCommand
is indeed a command, the code-behind in your View would be:It would also be possible to do without any code-behind at all by using some flavor of "event to command" attached behavior.