繁体   English   中英

如何在另一个项目中使用自定义WPF / XAML控件?

[英]How do I use a custom WPF/XAML control in another project?

我能够创建一个扩展Button类的自定义WPF Glass Button,并使用在资源字典中定义的Style中定义的ControlTemplate。

在包含控件的项目中,它可以正常工作并表现出预期的效果。

当我尝试将控件放置在另一个项目中时,我什么也没得到-什么也没出现。 没有错误,但没有控件显示自己。

我尝试使用UserControl构造控件,但是触发器中某些属性出错-

<UserControl.Triggers>
    <Trigger Property="IsPressed" Value="True"> <!--'IsPressed' member is not valid because it does not have a qualifying type name.-->
        <Setter Property="Visibility" TargetName="Glow" Value="Hidden"/> <!--The name "Glow" is not recognized."-->
        <Setter Property="Opacity" TargetName="Shine" Value="0.4"/> <!--The name "Shine" is not recognized."-->
        <Setter Property="Background" TargetName="Border" Value="#CC000000"/> <!--The name "Border" is not recognized."-->
    </Trigger>

这就是XAML的全部内容-

<UserControl x:Class="WPFTools.Controls.GB"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Storyboard x:Key="GlowOn">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Glow">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="GlowOff">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Glow">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
<Border x:Name="Border"
        BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
        Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
        BorderThickness="4" CornerRadius="4">
    <Grid x:Name="Contents">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border x:Name="Glow" BorderThickness="1" Height="Auto" Grid.RowSpan="2" VerticalAlignment="Stretch" Width="Auto" CornerRadius="4" Opacity="0"
                              Background="{Binding GlowColor, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
        <Viewbox Stretch="Uniform" Height="Auto" Grid.RowSpan="2" VerticalAlignment="Center" Width="Auto">
            <ContentPresenter x:Name="Content" HorizontalAlignment="Center"/>
        </Viewbox>
        <Border x:Name="Shine" BorderThickness="1,1,1,0" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch" BorderBrush="{x:Null}" CornerRadius="4,4,0,0"
                                Background="{Binding ShineColor, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
    </Grid>
</Border>
<UserControl.Triggers>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Visibility" TargetName="Glow" Value="Hidden"/>
        <Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
        <Setter Property="Background" TargetName="Border" Value="#CC000000"/>
    </Trigger>
    <Trigger Property="IsMouseOver" Value="True">
        <Trigger.ExitActions>
            <BeginStoryboard x:Name="GlowOff_BeginStoryboard" Storyboard="{StaticResource GlowOff}"/>
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="GlowOn_BeginStoryboard" Storyboard="{StaticResource GlowOn}"/>
        </Trigger.EnterActions>
    </Trigger>
</UserControl.Triggers>

这是该类的代码-

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPFTools.Controls {
    /// <summary>
    /// Interaction logic for GB.xaml
    /// </summary>
    public partial class GB : Button {

        private static readonly DependencyProperty
    _ShineColor = DependencyProperty.Register( "ShineColor", typeof( Brush ), typeof( GlassButton ), new FrameworkPropertyMetadata(
        new LinearGradientBrush( ) {
            GradientStops = new GradientStopCollection( ) {
                            new GradientStop(Color.FromArgb(0x99, 0xFF, 0xFF, 0xFF), 0.0D),
                            new GradientStop(Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF), 1.0D) },
            EndPoint = new Point( 0.5D, 1.0D ), StartPoint = new Point( 0.5D, 0.0D )
        } ) ), _GlowColor = DependencyProperty.Register( "GlowColor", typeof( Brush ), typeof( GlassButton ), new FrameworkPropertyMetadata(
        new RadialGradientBrush( ) {
            GradientStops = new GradientStopCollection( ){
                            new GradientStop( Color.FromArgb( 0xB2, 0x8D, 0xD8, 0xFF ), 0.0D),
                            new GradientStop( Color.FromArgb( 0x00, 0x8D, 0xD8, 0xFF ), 1.0D) },
            RelativeTransform = new TranslateTransform( ) { X = -0.25D, Y = 0.5D },
        } ) );

        public Brush ShineColor {
            get { return GetValue( GB._ShineColor ) as Brush; }
            set { SetValue( GB._ShineColor, value ); }
        }

        public Brush GlowColor {
            get { return GetValue( GB._GlowColor ) as Brush; }
            set { SetValue( GB._GlowColor, value ); }
        }

        public GB( ) {
            InitializeComponent( );
        }
    }
}

我显然在这里做错了-我只需要知道它是什么。

因此,事实证明,提供的链接给了我解决问题的答案-

首先:创建包含自定义WPF控件的项目时,我犯了一个错误,即更改了在Themes文件夹中创建的Generic.xaml的名称。 显然这是一个禁忌。

我犯的第二个错误是我没有修改assembly.cs中的ThemeInfo来表明需要资源。

解决这两个问题已解决了我的问题,该按钮现在应有的样子出现了,我可以继续对其进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM