绑定滑块控件的事件

发布于 2024-10-23 17:06:34 字数 1063 浏览 0 评论 0原文

在视图(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-10-30 17:06:34

问题不在于您的代码隐藏,而在于您的 XAML。在某个地方执行此操作:

DragCompleted="{Binding AudioSliderChangedCommand}"

这会指示 XAML 反序列化器将 AudioSliderChangedCommand 处理程序附加到 DragCompleted 事件。但是,AudioSliderChangedCommand不是具有适当签名的方法(可以作为处理程序附加),并且它不在您的 View 类中。最后,您不能将 Binding 用于事件处理程序。

要解决此问题,最简单的解决方案是在 View 中执行此操作:

private void DragCompletedEventHandler(object sender, DragCompletedEventArgs e)
{
    var viewModel = (YourViewModelType)this.DataContext;
    viewModel.OnAudioSliderChanged(this, e);
}

DragCompleted="{Binding AudioSliderChangedCommand}"

更改为

DragCompleted="DragCompletedEventHandler"

在 XAML 中

。这就是上面的工作原理:

  1. 在您的视图中,当引发 DragCompleted 时,将调用 View.DragCompletedEventHandler 方法。
  2. 此方法将获取 AudioSliderChangedCommand 来自 ViewModel 的事件(请参阅下面的注释)并引发它,传递原始事件参数

重要说明

您似乎对事件、事件处理程序和命令感到困惑。您现在的代码具有误导性。 AudioSliderChangedCommand 是一个事件,但顾名思义它是一个ICommand。适当的名称是 AudioSliderChanged

另外,执行此操作的适当 MVVM 方法是使用某种风格的 DelegateCommand (所有像样的 MVVM 框架都有一个;我在 Prism 中使用类名来实现)。然后,假设 AudioSliderChangedCommand 确实是一个命令,视图中的代码隐藏将是:

private void DragCompletedEventHandler(object sender, DragCompletedEventArgs e)
{
    var viewModel = (YourViewModelType)this.DataContext;
    viewModel.AudioSliderChangedCommand.Execute();
}

通过使用某种“事件到命令”的风格,也可以完全不使用任何代码隐藏” 附加行为。

The problem is not in your code-behind, but in your XAML. Somewhere you do this:

DragCompleted="{Binding AudioSliderChangedCommand}"

This instructs the XAML deserializer to attach the AudioSliderChangedCommand handler to the DragCompleted 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 use Binding for event handlers.

To solve this, the simplest solution is to do this in your View:

private void DragCompletedEventHandler(object sender, DragCompletedEventArgs e)
{
    var viewModel = (YourViewModelType)this.DataContext;
    viewModel.OnAudioSliderChanged(this, e);
}

and also change

DragCompleted="{Binding AudioSliderChangedCommand}"

to

DragCompleted="DragCompletedEventHandler"

in your XAML.

This is how the above will work:

  1. In your View, when DragCompleted is raised, the method View.DragCompletedEventHandler will be called
  2. This method will get hold of the AudioSliderChangedCommand event (see note below) from the ViewModel and raise it, passing the original event args

Important note

You seem to be confused about events, event handlers and commands. Your code as it stands is misleading. AudioSliderChangedCommand is an event, but the name suggests it's an ICommand. The appropriate name would be AudioSliderChanged.

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 that AudioSliderChangedCommand is indeed a command, the code-behind in your View would be:

private void DragCompletedEventHandler(object sender, DragCompletedEventArgs e)
{
    var viewModel = (YourViewModelType)this.DataContext;
    viewModel.AudioSliderChangedCommand.Execute();
}

It would also be possible to do without any code-behind at all by using some flavor of "event to command" attached behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文