0

我是使用 WPF 的新手,我使用这里的博客文章制作了一个动画扩展器(似乎是一个流行的第一个控件:)) ,但我似乎无法从资源中获取情节提要......

这是我的 XAML:

<Expander 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"
             x:Class="CustomControls.CustomExpanderControl"
             x:Name="ExpanderAnimated"
             d:DesignHeight="400" d:DesignWidth="150" Height="24" Width="150" Opacity="0.5" Header="ExpanderAnimated">
    <Expander.Resources>
        <Storyboard x:Key="OnExpanded">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ExpanderAnimated">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="ExpanderAnimated">
                <EasingDoubleKeyFrame KeyTime="0" Value="24"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="150"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="OnCollapsed">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ExpanderAnimated">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.5"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="ExpanderAnimated">
                <EasingDoubleKeyFrame KeyTime="0" Value="150"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Expander.Resources>
    <Expander.Triggers>
        <EventTrigger RoutedEvent="Expander.Expanded"/>
        <EventTrigger RoutedEvent="Expander.Collapsed"/>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource OnExpanded}"/>
            <BeginStoryboard Storyboard="{StaticResource OnCollapsed}"/>
        </EventTrigger>
    </Expander.Triggers>

</Expander>

以及背后的相关代码:

private Storyboard sbCollapsed
{
    get { return (Storyboard)this.Resources["OnCollapsed"]; }
    set { Resources["OnCollapsed"] = value; }
}
private Storyboard sbExpanded
{
    get { return (Storyboard)this.Resources["OnExpanded"]; }
    set { Resources["OnExpanded"] = value; }
}
private SplineDoubleKeyFrame CollapsedFromHeight
{
    get
    {
        return (SplineDoubleKeyFrame)
           ((DoubleAnimationUsingKeyFrames)sbCollapsed.Children[0]).KeyFrames[0];
    }
}
private SplineDoubleKeyFrame CollapsedToHeight
{
    get
    {
        return (SplineDoubleKeyFrame)
           ((DoubleAnimationUsingKeyFrames)sbExpanded.Children[0]).KeyFrames[1];
    }
}
private SplineDoubleKeyFrame ExpandedFromHeight
{
    get
    {
        return (SplineDoubleKeyFrame)
           ((DoubleAnimationUsingKeyFrames)sbCollapsed.Children[0]).KeyFrames[0];
    }
}
private SplineDoubleKeyFrame ExpandedToHeight
{
    get
    {
        return (SplineDoubleKeyFrame)
           ((DoubleAnimationUsingKeyFrames)sbExpanded.Children[0]).KeyFrames[1];
    }
}

(这是整个 XAML,如果需要,我可以发布整个 c#)

我需要在 2 个private StoryBoard属性中进行哪些更改才能正确设置它们?

例外情况如下:

NullReferenceException: Object reference not set to an instance of an object.

StackTrace

at CustomControls.ExpanderAnimated.get_CollapsedToHeight() // This is the private property as above
at CustomControls.ExpanderAnimated.get_CollapsedHeight() // This is the public property

InnerException: None

编辑

这发生在设计器中

我点击扩展器: 在此处输入图像描述

它显示了这个异常: 在此处输入图像描述

编辑2

所以我改变了Storyboard get {}返回

FindResource("OnCollapsed") as Storyboard;

这会引发异常ResourceReferenceKeyNotFoundException: 'OnCollapsed' resource not found。..... 除了它在 XAML 中!gr

4

0 回答 0