在 Windows 8 Metro 中编写自定义控件

本文关键字:自定义控件 Metro Windows | 更新日期: 2023-09-27 18:31:47

我正在针对 WinRT 编写我的第一个自定义用户控件,但我遇到了一个问题。

我想公开一个图像,PART_NwBadge它在我的控件中作为依赖项属性的可见性。然后我想通过样式中的资源库提供默认值。这部分不起作用。相反,将应用依赖项属性(在 BadgedButton.cs 中)中的默认值。

甚至有可能做到我所描述的吗?还是应该在 C# 代码中设置默认值?如果我确实需要在 C# 代码中设置值,有人会评论如何在代码中加载图像资源吗?经过大量搜索,我还没有找到有效的解决方案。

最后,由于这是我第一次认真尝试编写自定义控件,因此请建议我可以进行的任何改进,即使它们与问题没有直接关系。

Windows 8 消费者预览版
C#/WinRT/Metro
Visual Studio 11 Beta

主题/通用.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="using:InkSdkTestApplication.Controls">
    <Style TargetType="l:BadgedButton">
        <Setter Property="Width" Value="36"/>
        <Setter Property="Height" Value="36"/>
        <Setter Property="Background" Value="#1C1C1C"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="NwBadge">
            <Setter.Value>
                <Image Width="16" Height="16" Source="../Assets/mouse_16x16.png"/>            
            </Setter.Value>
        </Setter>
        <Setter Property="NwBadgeVisibility" Value="Collapsed"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="l:BadgedButton">
                    <Border x:Name="PART_Border"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <ContentPresenter x:Name="PART_Content"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              Content="{TemplateBinding Content}"/>
                            <Image x:Name="PART_NwBadge"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Top"
                                   Width="16" Height="16"
                                   Visibility="{TemplateBinding NwBadgeVisibility}"
                                   Source="{TemplateBinding NwBadge}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

控件/标记按钮.cs

namespace InkSdkTestApplication.Controls
{
    public sealed class BadgedButton : Control
    {
        #region // Dependency Properties
        public static DependencyProperty ContentProperty =
            DependencyProperty.Register(
                "Content",
                typeof(FrameworkElement),
                typeof(BadgedButton),
                new PropertyMetadata(null));
        public static DependencyProperty NwBadgeProperty =
            DependencyProperty.Register(
                "NwBadge",
                typeof(Image),
                typeof(BadgedButton),
                new PropertyMetadata(null));
        public static DependencyProperty NwBadgeVisibilityProperty =
            DependencyProperty.Register(
                "NwBadgeVisibility",
                typeof(Visibility),
                typeof(BadgedButton),
                new PropertyMetadata(Visibility.Visible));
        #endregion
        #region // Public Properties
        public FrameworkElement Content
        {
            get { return (FrameworkElement)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }
        public Image NwBadge
        {
            get { return (Image)GetValue(NwBadgeProperty); }
            set { SetValue(NwBadgeProperty, value); }
        }
        public Visibility NwBadgeVisibility
        {
            get { return (Visibility)GetValue(NwBadgeVisibilityProperty); }
            set { SetValue(NwBadgeVisibilityProperty, value); }
        }
        #endregion
        public BadgedButton()
        {
            this.DefaultStyleKey = typeof(BadgedButton);
        }
    }
}

在 Windows 8 Metro 中编写自定义控件

http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx