在WP8中创建一个自定义按钮控件

本文关键字:一个 自定义 按钮 控件 WP8 创建 | 更新日期: 2023-09-27 18:20:53

。。。


好的,谢谢Scott Nimrod,我找到了一种方法:创建一个新项目,添加一个新的用户控件,构建到dll并添加对我的应用程序项目的引用

如果有人需要的话,这是我的代码:)

在我的应用程序的xaml页面中(点击事件有效,但2个图像"sourceNormal"answers"sourcePressed"不可见)

<local:ButtonImage Width="40" Height="40"
                         Click="ButtonImage_Click"
                         SourceNormal="/download_home.png"                             
                         SourcePressed="/download_nowplaying.png"/>

UserControl XAML

<UserControl x:Class="MyCustomControl.ButtonImage"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
DataContext="{Binding RelativeSource={RelativeSource Self}}"         
xmlns:ntd="clr-namespace:MyCustomControl">
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Button x:Name="Button_Image" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Collapsed</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Collapsed</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Image x:Name="ImageNormal" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourceNormal}" Stretch="Uniform"/>
                    <Image x:Name="ImagePressed" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourcePressed}" Stretch="Uniform"/>                        
                </Grid> 
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

用户控制代码背后:

namespace MyCustomControl
{
public partial class ButtonImage : UserControl
{
    public ButtonImage()
    {
        InitializeComponent();            
        Button_Image.Click += ButtonImage_Click;
    }
    public ImageSource SourceNormal
    {
        get { return (ImageSource)GetValue(SourceNormalProperty);}
        set { SetValue(SourceNormalProperty, value); }
    }        
    public ImageSource SourcePressed
    {
        get { return (ImageSource)GetValue(SourcePressedProperty); }
        set { SetValue(SourcePressedProperty, value); }
    }
    public event EventHandler Click;
    void ButtonImage_Click(object sender, RoutedEventArgs e)
    {
        var eventHandler = this.Click;
        if (eventHandler != null)
        {
            eventHandler(this, e);
        }
    }

    /////////////////////// SourceNormal /////////////////////////////////////////////////////
    //----------------------------------------------------------------------------------------
    public static readonly DependencyProperty SourceNormalProperty = DependencyProperty.RegisterAttached(
        "SourceNormal",
        typeof(ImageSource),
        typeof(ButtonImage),
        new PropertyMetadata(null)
    );
    public static ImageSource GetSourceNormal(UIElement element)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        return (ImageSource)element.GetValue(SourceNormalProperty);
    }
    public static void SetSourceNormal(UIElement element, ImageSource value)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        element.SetValue(SourceNormalProperty, value);
    }
    //----------------------------------------------------------------------------------------
    /////////////////////// SourcePressed ////////////////////////////////////////////////////
    //----------------------------------------------------------------------------------------
    public static readonly DependencyProperty SourcePressedProperty = DependencyProperty.RegisterAttached(
        "SourcePressed",
        typeof(ImageSource),
        typeof(ButtonImage),
        new PropertyMetadata(null)
    );
    public static ImageSource GetSourcePressed(UIElement element)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        return (ImageSource)element.GetValue(SourcePressedProperty);
    }
    public static void SetSourcePressed(UIElement element, ImageSource value)
    {
        if (element == null)
        {
            new ArgumentNullException("element");
        }
        element.SetValue(SourcePressedProperty, value);
    }
}

}

在WP8中创建一个自定义按钮控件

答案是肯定的。

Telerik用他们的RadControls做到了这一点。

只需创建一个单独的项目,并添加WPF对UI所依赖的主要引用。这也是实现Prism模块时的一种模式。

在您的其他项目中,只需添加对在中定义样式的dll的引用即可。