[英]Expose Click event in custom button in user control in XAML WP8
I have a user control with a customized/custom-styled button. 我有一个带有自定义/自定义样式按钮的用户控件。 Until now, I use the Tap event to call the click action.
到目前为止,我使用Tap事件调用click操作。 However, Tap seems to be "too slow".
但是,Tap似乎“太慢”。 So when I click the button two times in a row too fast, then only the first tap fires an event.
因此,当我连续两次单击按钮太快时,只有第一次点击会触发一个事件。 The same happens when I have two buttons on my screen and I click them consecutively.
当我在屏幕上有两个按钮并连续单击它们时,也会发生相同的情况。 I guess it has something to do with the Tap event (seems to fire only when my finger is lifted from the screen).
我猜想这与Tap事件有关(似乎只有从屏幕上抬起手指时才会触发)。
<ui:JapanButton x:Name="JapanButton1" Text="Japan" Tap="JapanButton1_OnTap"/>
That's why I want to expose the Click event of the button in the user control but don't know how to do that. 这就是为什么我要在用户控件中公开按钮的Click事件,但不知道如何执行此操作的原因。
<ui:JapanButton x:Name="JapanButton1" Text="Japan" Click="JapanButton1_OnClick"/>
Here is what I currently have: 这是我目前拥有的:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerikPrimitives="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Primitives"
x:Class="UI.JapanButton"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="100" d:DesignWidth="480">
<UserControl.Resources>
<telerikPrimitives:NegativeConverter x:Key="NegativeConverter"/>
<Style x:Key="JapanButtoStyle" TargetType="Button">
<Setter Property="Background" Value="OrangeRed"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="FontSize" Value="40"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Margin="{TemplateBinding Margin}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ButtonBackground" Background="{TemplateBinding Background}" CornerRadius="16" BorderBrush="#434045" BorderThickness="2">
<ContentControl x:Name="ContentContainer" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
<ContentControl.RenderTransform>
<RotateTransform Angle="{Binding ElementName=RotateTransformButton,Path=Angle,Converter={StaticResource NegativeConverter}}"></RotateTransform>
</ContentControl.RenderTransform>
</ContentControl>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Button x:Name="ButtonJapan" Style="{StaticResource JapanButtoStyle}" Margin="3" Foreground="WhiteSmoke" Background="#9C2222">
</Button>
</Grid>
</UserControl>
C# code behind for the user control 用户控件后面的C#代码
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace UI
{
public partial class JapanButton : UserControl
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(JapanButton), null);
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public JapanButton()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var bindingTitle = new Binding();
bindingTitle.Source = Text;
ButtonJapan.SetBinding(ContentControl.ContentProperty, bindingTitle);
var bindingFontSize = new Binding();
bindingFontSize.Source = FontSize;
ButtonJapan.SetBinding(FontSizeProperty, bindingFontSize);
}
}
}
First, create your custom Click event in the UserControl: 首先,在UserControl中创建自定义的Click事件:
public event EventHandler Click;
Then, subscribe to the Click event of your button: 然后,订阅按钮的Click事件:
public JapanButton()
{
InitializeComponent();
Loaded += OnLoaded;
ButtonJapan.Click += ButtonClick;
}
Finally, in the event handler, raise your custom Click
event: 最后,在事件处理程序中,引发您的自定义
Click
事件:
private void ButtonClick(object sender, RoutedEventArgs e)
{
var eventHandler = this.Click;
if (eventHandler != null)
{
eventHandler(this, e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.