wpf 如何修复键属性只能用于 IDictionary 中包含的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21719583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:48:55  来源:igfitidea点击:

How to fix key attribute can only be used on element that is Contained in IDictionary

wpfxaml

提问by Sanath Shetty

how do i fix the Error the "Key" attribute can only be used on an element that is contained in "IDictionary".

如何修复错误“Key”属性只能用于包含在“IDictionary”中的元素。

<Window x:Class="WpfApplication9.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>
    <Grid
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:system = "clr-namespace:System;assembly=mscorlib"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid.Resources>
            <!-- Particle Styling -->
            <SolidColorBrush x:Key = "ParticleColor" Color = "#006699"/>
            <SolidColorBrush x:Key = "ParticleBackgroundColor" Color = "Transparent"/>
            <system:Double x:Key = "ParticleOpacity">1</system:Double>
            <system:Double x:Key = "ParticleRadius">5</system:Double>

            <system:Double x:Key = "StartingPointX">0</system:Double>
            <system:Double x:Key = "StartingPointY">-20</system:Double>

            <system:Double x:Key = "RotationPointX">0.5</system:Double>
            <system:Double x:Key = "RotationPointY">0.5</system:Double>

            <!-- StoryBoard -->
            <system:TimeSpan x:Key = "StoryBoardBeginTimeP0">00:00:00.000</system:TimeSpan>
            <system:TimeSpan x:Key = "StoryBoardBeginTimeP1">00:00:00.100</system:TimeSpan>
            <system:TimeSpan x:Key = "StoryBoardBeginTimeP2">00:00:00.200</system:TimeSpan>
            <system:TimeSpan x:Key = "StoryBoardBeginTimeP3">00:00:00.300</system:TimeSpan>
            <system:TimeSpan x:Key = "StoryBoardBeginTimeP4">00:00:00.400</system:TimeSpan>
            <Duration x:Key = "StoryBoardDuration">00:00:01.800</Duration>

            <!-- Particle Origin Angles -->
            <system:Double x:Key = "ParticleOriginAngleP0">0</system:Double>
            <system:Double x:Key = "ParticleOriginAngleP1">-10</system:Double>
            <system:Double x:Key = "ParticleOriginAngleP2">-20</system:Double>
            <system:Double x:Key = "ParticleOriginAngleP3">-30</system:Double>
            <system:Double x:Key = "ParticleOriginAngleP4">-40</system:Double>

            <!-- Particle Position & Timing 1 -->
            <system:Double x:Key = "ParticleBeginAngle1">0</system:Double>
            <system:Double x:Key = "ParticleEndAngle1">90</system:Double>
            <system:TimeSpan x:Key = "ParticleBeginTime1">00:00:00.000</system:TimeSpan>
            <Duration x:Key = "ParticleDuration1">00:00:00.750</Duration>

            <!-- Particle Position & Timing 2 -->
            <system:Double x:Key = "ParticleBeginAngle2">90</system:Double>
            <system:Double x:Key = "ParticleEndAngle2">270</system:Double>
            <system:TimeSpan x:Key = "ParticleBeginTime2">00:00:00.751</system:TimeSpan>
            <Duration x:Key = "ParticleDuration2">00:00:00.300</Duration>

            <!-- Particle Position & Timing 3 -->
            <system:Double x:Key = "ParticleBeginAngle3">270</system:Double>
            <system:Double x:Key = "ParticleEndAngle3">360</system:Double>
            <system:TimeSpan x:Key = "ParticleBeginTime3">00:00:01.052</system:TimeSpan>
            <Duration x:Key = "ParticleDuration3">00:00:00.750</Duration>

            <Style x:Key = "EllipseStyle" TargetType = "Ellipse">
                <Setter Property = "Width" Value = "{StaticResource ParticleRadius}"/>
                <Setter Property = "Height" Value = "{StaticResource ParticleRadius}"/>
                <Setter Property = "Fill" Value = "{StaticResource ParticleColor}"/>
                <Setter Property = "RenderTransformOrigin" Value = "0.5, 0.5"/>
                <Setter Property = "Opacity" Value = "{StaticResource ParticleOpacity}"/>
            </Style>
        </Grid.Resources>
        <Canvas Width = "50" Height = "50">
            <Canvas.Triggers>
                <EventTrigger RoutedEvent = "Canvas.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard
                        x:Key = "MetroLoadingAnimation"
                        BeginTime = "{StaticResource StoryBoardBeginTimeP0}"
                        Duration = "{StaticResource StoryBoardDuration}"
                        RepeatBehavior = "Forever">
                                <DoubleAnimation
                            Storyboard.TargetName = "p0"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle1}"
                            To = "{StaticResource ParticleEndAngle1}"
                            BeginTime = "{StaticResource ParticleBeginTime1}"
                            Duration = "{StaticResource ParticleDuration1}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p0"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle2}"
                            To = "{StaticResource ParticleEndAngle2}"
                            BeginTime = "{StaticResource ParticleBeginTime2}"
                            Duration = "{StaticResource ParticleDuration2}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p0"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle3}"
                            To = "{StaticResource ParticleEndAngle3}"
                            BeginTime = "{StaticResource ParticleBeginTime3}"
                            Duration = "{StaticResource ParticleDuration3}"/>
                            </Storyboard>
                        </BeginStoryboard>
                        <BeginStoryboard>
                            <Storyboard
                        x:Key = "MetroLoadingAnimation"
                        BeginTime = "{StaticResource StoryBoardBeginTimeP1}"
                        Duration = "{StaticResource StoryBoardDuration}"
                        RepeatBehavior = "Forever">

                                <DoubleAnimation
                            Storyboard.TargetName = "p1"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle1}"
                            To = "{StaticResource ParticleEndAngle1}"
                            BeginTime = "{StaticResource ParticleBeginTime1}"
                            Duration = "{StaticResource ParticleDuration1}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p1"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle2}"
                            To = "{StaticResource ParticleEndAngle2}"
                            BeginTime = "{StaticResource ParticleBeginTime2}"
                            Duration = "{StaticResource ParticleDuration2}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p1"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle3}"
                            To = "{StaticResource ParticleEndAngle3}"
                            BeginTime = "{StaticResource ParticleBeginTime3}"
                            Duration = "{StaticResource ParticleDuration3}"/>
                            </Storyboard>
                        </BeginStoryboard>
                        <BeginStoryboard>
                            <Storyboard
                        x:Key = "MetroLoadingAnimation"
                        BeginTime = "{StaticResource StoryBoardBeginTimeP2}"
                        Duration = "{StaticResource StoryBoardDuration}"
                        RepeatBehavior = "Forever">

                                <DoubleAnimation
                            Storyboard.TargetName = "p2"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle1}"
                            To = "{StaticResource ParticleEndAngle1}"
                            BeginTime = "{StaticResource ParticleBeginTime1}"
                            Duration = "{StaticResource ParticleDuration1}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p2"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle2}"
                            To = "{StaticResource ParticleEndAngle2}"
                            BeginTime = "{StaticResource ParticleBeginTime2}"
                            Duration = "{StaticResource ParticleDuration2}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p2"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle3}"
                            To = "{StaticResource ParticleEndAngle3}"
                            BeginTime = "{StaticResource ParticleBeginTime3}"
                            Duration = "{StaticResource ParticleDuration3}"/>
                            </Storyboard>
                        </BeginStoryboard>

                        <BeginStoryboard>
                            <Storyboard
                        x:Key = "MetroLoadingAnimation"
                        BeginTime = "{StaticResource StoryBoardBeginTimeP3}"
                        Duration = "{StaticResource StoryBoardDuration}"
                        RepeatBehavior = "Forever">

                                <DoubleAnimation
                            Storyboard.TargetName = "p3"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle1}"
                            To = "{StaticResource ParticleEndAngle1}"
                            BeginTime = "{StaticResource ParticleBeginTime1}"
                            Duration = "{StaticResource ParticleDuration1}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p3"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle2}"
                            To = "{StaticResource ParticleEndAngle2}"
                            BeginTime = "{StaticResource ParticleBeginTime2}"
                            Duration = "{StaticResource ParticleDuration2}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p3"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle3}"
                            To = "{StaticResource ParticleEndAngle3}"
                            BeginTime = "{StaticResource ParticleBeginTime3}"
                            Duration = "{StaticResource ParticleDuration3}"/>
                            </Storyboard>
                        </BeginStoryboard>

                        <BeginStoryboard>
                            <Storyboard
                        x:Key = "MetroLoadingAnimation"
                        BeginTime = "{StaticResource StoryBoardBeginTimeP4}"
                        Duration = "{StaticResource StoryBoardDuration}"
                        RepeatBehavior = "Forever">

                                <DoubleAnimation
                            Storyboard.TargetName = "p4"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle1}"
                            To = "{StaticResource ParticleEndAngle1}"
                            BeginTime = "{StaticResource ParticleBeginTime1}"
                            Duration = "{StaticResource ParticleDuration1}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p4"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle2}"
                            To = "{StaticResource ParticleEndAngle2}"
                            BeginTime = "{StaticResource ParticleBeginTime2}"
                            Duration = "{StaticResource ParticleDuration2}"/>
                                <DoubleAnimation
                            Storyboard.TargetName = "p4"
                            Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)"
                            From = "{StaticResource ParticleBeginAngle3}"
                            To = "{StaticResource ParticleEndAngle3}"
                            BeginTime = "{StaticResource ParticleBeginTime3}"
                            Duration = "{StaticResource ParticleDuration3}"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Canvas.Triggers>
            <Border
        x:Name = "p0"
        Background = "{StaticResource ParticleBackgroundColor}"
        Opacity = "{StaticResource ParticleOpacity}">
                <Border.RenderTransform>
                    <RotateTransform/>
                </Border.RenderTransform>
                <Border.RenderTransformOrigin>
                    <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
                </Border.RenderTransformOrigin>
                <Ellipse Style = "{StaticResource EllipseStyle}">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
                            <RotateTransform Angle = "{StaticResource ParticleOriginAngleP0}"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Border>
            <Border
        x:Name = "p1"
        Background = "{StaticResource ParticleBackgroundColor}"
        Opacity = "{StaticResource ParticleOpacity}">
                <Border.RenderTransform>
                    <RotateTransform/>
                </Border.RenderTransform>
                <Border.RenderTransformOrigin>
                    <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
                </Border.RenderTransformOrigin>
                <Ellipse Style = "{StaticResource EllipseStyle}">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
                            <RotateTransform Angle = "{StaticResource ParticleOriginAngleP1}"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Border>
            <Border
        x:Name = "p2"
        Background = "{StaticResource ParticleBackgroundColor}"
        Opacity = "{StaticResource ParticleOpacity}">
                <Border.RenderTransform>
                    <RotateTransform/>
                </Border.RenderTransform>
                <Border.RenderTransformOrigin>
                    <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
                </Border.RenderTransformOrigin>
                <Ellipse Style = "{StaticResource EllipseStyle}">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
                            <RotateTransform Angle = "{StaticResource ParticleOriginAngleP2}"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Border>
            <Border
        x:Name = "p3"
        Background = "{StaticResource ParticleBackgroundColor}"
        Opacity = "{StaticResource ParticleOpacity}">
                <Border.RenderTransform>
                    <RotateTransform/>
                </Border.RenderTransform>
                <Border.RenderTransformOrigin>
                    <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
                </Border.RenderTransformOrigin>
                <Ellipse Style = "{StaticResource EllipseStyle}">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
                            <RotateTransform Angle = "{StaticResource ParticleOriginAngleP3}"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Border>
            <Border
        x:Name = "p4"
        Background = "{StaticResource ParticleBackgroundColor}"
        Opacity = "{StaticResource ParticleOpacity}">
                <Border.RenderTransform>
                    <RotateTransform/>
                </Border.RenderTransform>
                <Border.RenderTransformOrigin>
                    <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/>
                </Border.RenderTransformOrigin>
                <Ellipse Style = "{StaticResource EllipseStyle}">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/>
                            <RotateTransform Angle = "{StaticResource ParticleOriginAngleP4}"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Border>
        </Canvas>
    </Grid>
</Grid>

回答by Rohit Vats

As error stated you can only set x:Keyattribute on elements which exist in resourceDictionary and not on elements outside dictionary.

正如错误所述,您只能x:Key在 resourceDictionary 中存在的元素上设置属性,而不能在 dictionary 之外的元素上设置属性

<Storyboard x:Key = "MetroLoadingAnimation">

Remove x:Keyfrom storyboard and instead set x:Name.

x:Key从故事板中删除并设置x:Name.

<Storyboard x:Name = "MetroLoadingAnimation">