我在XAML应用程序中遇到错误。
我收到的错误是
' [未知]' property不指向路径中的DependencyObject '(0)(1)[2](2)' ...
我在周围搜索并发现需要在Page.XAML for RotateTransform中的<Image.RenderTransform>
标签内进行搜索,但我不确定自从我是XAML的新手以及仍在学习的内容。
的App.xaml
<Storyboard x:Key="spin">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="{Binding}"
RepeatBehavior="Forever">
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
Page.XAML
<Page.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{DynamicResource spin}"/>
</EventTrigger>
</Page.Triggers>
<Image x:Name="image1"
Margin="0, 0, 5, 0"
Source="{StaticResource inProcessImage}"
Width="18"
RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="image2"
Margin="0, 0, 5, 0"
Source="{StaticResource inProcessImage}"
Width="18"
RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
背后的代码
startAni("spin", image1.Name);
startAni("spin", image2.Name);
void startAni(string storyboardName, string objectName)
{
Storyboard sb = FindResource(storyboardName) as Storyboard;
foreach (var child in sb.Children)
Storyboard.SetTargetName(child, objectName);
sb.Begin(this); // do not forget the this keyword
}
答案 0 :(得分:0)
错误是由这部分引起的:
<Page.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{DynamicResource spin}"/>
</EventTrigger>
</Page.Triggers>
事件触发器未正确加载故事板(未设置TargetName
)。所以你看到了这个错误。
您可以将startAni
调用放入页面的已加载事件中(在代码后面。)
这对我来说非常好:
public Page() {
InitializeComponent();
Loaded += Page_Loaded;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
startAni("spin", image1.Name);
startAni("spin", image2.Name);
}
仅限XAML的解决方案如下所示:
<TimelineCollection x:Key="Ani">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="{Binding}"
RepeatBehavior="Forever">
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="360"/>
</DoubleAnimationUsingKeyFrames>
</TimelineCollection>
使用TimelineCollection而不是故事板,然后您可以在Page.XAML中设置TargetName。这根本不需要代码。
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard TargetName="image1" Children="{DynamicResource Ani}" />
</BeginStoryboard>
<BeginStoryboard>
<Storyboard TargetName="image2" Children="{DynamicResource Ani}" />
</BeginStoryboard>
</EventTrigger>
另一种方法是将故事板放在样式中并将样式应用于每个图像。这将更清晰,更可重复使用。您可以大致了解如何执行此操作here