【问题标题】:How to detect AppBarButtonStyle PointerPressed and PointerReleased events?如何检测 AppBarButtonStyle PointerPressed 和 PointerReleased 事件?
【发布时间】:2013-02-07 01:23:59
【问题描述】:

我有一个在 XAML 中定义的按钮,如下所示:

<Button x:Name="CommandsBtn"
        HorizontalAlignment="Right"
        Style="{StaticResource ControlsAppBarButtonStyle}"
        Click="Commands_Click"
        FontFamily="Segoe UI Symbol"
        Background="{x:Null}"
        BorderBrush="{x:Null}"
        Margin="0"
        Grid.Column="3" />

ControlsAppBarButtonStyle 的定义如下:

<Style x:Key="ControlsAppBarButtonStyle"
       TargetType="ButtonBase"
       BasedOn="{StaticResource AppBarButtonStyle}">
  <Setter Property="AutomationProperties.AutomationId"
          Value="CommandsAppBarButton" />
  <Setter Property="AutomationProperties.Name"
          Value="Controls" />
  <Setter Property="Content"
          Value="" />
</Style>

我想检测 PointerPressed 和 PointerReleased 事件。我确实检测到点击事件。

尝试做:

CommandsBtn.PointerPressed += CommandsBtn_PointerPressed;
CommandsBtn.PointerReleased += CommandsBtn_PointerReleased;

但是事件没有被触发(或者至少没有进入我的页面)。

我该怎么做?

谢谢, 艾坦B

【问题讨论】:

  • 你喜欢用它做什么。如果您尝试在新闻发布事件中进行一些状态更改,您可以在相应的视觉状态中执行此操作。
  • 当用户按下按钮时,我打开马达,当他松开按钮时马达停止。我想使用 AppBarButtonStyle 按钮。谢谢艾坦

标签: c# xaml windows-8 windows-runtime winrt-xaml


【解决方案1】:

绑定到 Button 的 IsPressed 属性并处理绑定属性的更改以查看更改。也可以考虑使用 RepeatButton。

【讨论】:

  • “IsPressed”是否触发事件?如果没有,它就不能满足我的需求。
  • 它本身不会,但如果你将另一个属性绑定到它,你可以触发任何你想要的。
  • 嗨,“绑定另一个属性”是什么意思?谢谢艾坦
  • 好吧,您可以在视图模型中创建一个绑定到 IsPressed 属性的属性,而不是处理事件,您可以在其中适当地处理这些更改。您将设置按钮IsPressed="{Binding YourProperty, Mode=TwoWay}" 并确保定义YourProperty 的对象是DataContext。除此之外,我创建了一个附加行为ButtonBaseExtensions.ButtonStateEventBehavior 你可以在这里看到winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/…
  • 确保从“源”选项卡下载最新的源:winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多