【问题标题】:Error: Each dictionary entry must have an associated key错误:每个字典条目必须有一个关联的键
【发布时间】:2018-04-28 22:01:27
【问题描述】:

xaml

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <StackPanel.Resources>
            <Storyboard x:Name="myStoryboard">
                <DoubleAnimation Storyboard.TargetName="MyAnimatedRectangle" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" />
            </Storyboard>
        </StackPanel.Resources>
        <TextBlock Margin="10">Click on the rectangle to start the animation.</TextBlock>
        <Rectangle MouseLeftButtonDown="Mouse_Clicked" x:Name="MyAnimatedRectangle" Width="100" Height="100" Fill="Blue" />
    </StackPanel>
</Grid>
</Window>

vb

Private Sub Mouse_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs)
    myStoryboard.Begin()
End Sub

c#

private void Mouse_Clicked(object sender, MouseEventArgs e)
{
    myStoryboard.Begin();
}

错误画面:

https://prnt.sc/jbeo16

我们将不胜感激。

提前致谢

【问题讨论】:

    标签: c# wpf vb.net xaml animation


    【解决方案1】:

    正如错误所说,您需要定义与每个资源关联的x:Key 指令。

    <StackPanel x:Name="myStackPanel">
        <StackPanel.Resources>
            <Storyboard x:Key="myStoryboard">
                <DoubleAnimation Storyboard.TargetName="MyAnimatedRectangle" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" />
            </Storyboard>
        </StackPanel.Resources>
        <TextBlock Margin="10">Click on the rectangle to start the animation.</TextBlock>
        <Rectangle MouseLeftButtonDown="Mouse_Clicked" x:Name="MyAnimatedRectangle" Width="100" Height="100" Fill="Blue" />
    </StackPanel>
    

    然后从代码隐藏中获取:

    private void Mouse_Clicked(object sender, MouseEventArgs e)
    {
        var myStoryboard = (Storyboard)myStackPanel.FindResource("myStoryboard");
        myStoryboard.Begin();
    }
    

    来自docs

    资源字典中的每个资源都必须有一个唯一的键。什么时候 您在标记中定义资源,您通过 x:关键指令。

    What's the difference between x:Key and x:Name in WPF?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多