【发布时间】:2019-04-15 22:10:57
【问题描述】:
我有一个用户控件,我想在特定时间制作动画。我将其中两个用户控件添加到一个窗口中。我使用该窗口中的按钮来启动动画。其中一个用户控件工作得很好。第二个给我一个不可变对象实例错误的Cannot animate '(0).(1)'。我完全不明白这个!为什么一个工作而不是另一个?我正在阅读需要一个转换器 - 但我根本无法让它工作。我把代码留在这里以防万一我确实需要它。感谢您对此提供的任何帮助。我是 WPF 的新手,所以我可能会在这里做一些愚蠢的事情 - 请原谅我。这是我的用户控件代码:
<UserControl x:Class="colorCompButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:UserControlTests"
xmlns:custom="clr-namespace:UserControlTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="179" Width="317">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<Storyboard x:Key="sb2" RepeatBehavior="Forever" x:Name="thisone">
<ColorAnimation Storyboard.TargetName="badge1"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" From="Red" To="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=BadgeColor}"
Duration="0:0:1" />
</Storyboard>
</UserControl.Resources>
<Grid Margin="0,0,10,10">
<Rectangle x:Name="rect" HorizontalAlignment="Left" Height="135" VerticalAlignment="Top" Width="259" x:FieldModifier="public" Fill ="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=RectColor}"/>
<Image x:Name="image" Height="87" Margin="10,10,63,0" VerticalAlignment="Top" x:FieldModifier="public" Source ="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ImageSource}"/>
<TextBlock x:Name="textBlock" Margin="0,102,10,38" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=TxtBoxValue}" Background="Transparent" Foreground="White" FontSize="20" Padding="3"/>
<Grid Margin="222,81,0,0" x:Name="NumberIMG" Height="78" VerticalAlignment="Top" HorizontalAlignment="Left" Width="85" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=BadgeVisibile}" >
<Border BorderBrush="Maroon" BorderThickness="1" CornerRadius="120" Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=BadgeColor}" HorizontalAlignment="Left" Height="76" VerticalAlignment="Top" Width="76" x:Name="badge1" x:FieldModifier="public"></Border>
<Border BorderBrush="black" Margin="2,2,0,0" BorderThickness="3" Opacity=".5" CornerRadius="120" HorizontalAlignment="Left" Height="74" VerticalAlignment="Top" Width="74"></Border>
<Border BorderBrush="White" BorderThickness="3" CornerRadius="120" HorizontalAlignment="Left" Height="78" VerticalAlignment="Top" Width="78">
<Label x:Name="resCount" Content="46" FontSize="25" Foreground="White" HorizontalContentAlignment="center" VerticalContentAlignment="center" Margin="0,0,-3,-3"/>
</Border>
</Grid>
</Grid>
以及该控件背后的代码:
Imports System.Windows.Media.Animation
Public Class colorCompButton
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
'Public Shared Sub SetImage(obj As DependencyObject, value As ImageSource)
' obj.SetValue(ImageProperty, value)
'End Sub
'Public Shared Function GetImage(obj As DependencyObject) As ImageSource
' Return DirectCast(obj.GetValue(ImageProperty), ImageSource)
'End Function
Shared Sub New()
'register attached dependency property
Dim metadata = New FrameworkPropertyMetadata(DirectCast(Nothing, ImageSource))
'ImageProperty = DependencyProperty.RegisterAttached("Image", GetType(ImageSource), GetType(colorCompButton), metadata)
End Sub
Public Shared ReadOnly BadgeVisibileProperty As DependencyProperty = DependencyProperty.Register("BadgeVisibile", GetType(Visibility), GetType(colorCompButton), New PropertyMetadata(Visibility.Visible))
Public Property BadgeVisibile() As Visibility
Get
Return DirectCast(GetValue(BadgeVisibileProperty), Visibility)
End Get
Set(value As Visibility)
SetValue(BadgeVisibileProperty, value)
End Set
End Property
Public Shared ReadOnly BadgeColorProperty As DependencyProperty = DependencyProperty.Register("BadgeColor", GetType(Brush), GetType(colorCompButton), New PropertyMetadata(New SolidColorBrush(Colors.DarkGray)))
Public Property BadgeColor() As SolidColorBrush
Get
Return DirectCast(GetValue(BadgeColorProperty), SolidColorBrush)
End Get
Set(value As SolidColorBrush)
SetValue(BadgeColorProperty, value)
End Set
End Property
Public Shared ReadOnly buttonColorProperty As DependencyProperty = DependencyProperty.Register("buttonColor", GetType(Brush), GetType(colorCompButton), New PropertyMetadata(New SolidColorBrush(Colors.DarkGray)))
Public Property buttonColor() As SolidColorBrush
Get
Return DirectCast(GetValue(buttonColorProperty), SolidColorBrush)
End Get
Set(value As SolidColorBrush)
SetValue(buttonColorProperty, value)
End Set
End Property
Public Shared ReadOnly ImageSourceProperty As DependencyProperty = DependencyProperty.Register("ImageSource", GetType(BitmapSource), GetType(colorCompButton))
Public Property ImageSource() As ImageSource
Get
Return DirectCast(GetValue(ImageSourceProperty), ImageSource)
End Get
Set(value As ImageSource)
SetValue(ImageSourceProperty, value)
End Set
End Property
Public Shared ReadOnly RectColorProperty As DependencyProperty = DependencyProperty.Register("RectColor", GetType(Brush), GetType(colorCompButton), New PropertyMetadata(New SolidColorBrush(Colors.DarkGray)))
Public Property RectColor() As SolidColorBrush
Get
Return DirectCast(GetValue(RectColorProperty), SolidColorBrush)
End Get
Set(value As SolidColorBrush)
SetValue(RectColorProperty, value)
End Set
End Property
Public Shared TxtBoxValueProperty As DependencyProperty = DependencyProperty.Register("txtBoxValue", GetType([String]), GetType(colorCompButton))
Public Property TxtBoxValue() As [String]
Get
Return DirectCast(GetValue(TxtBoxValueProperty), [String])
End Get
Set(value As [String])
SetValue(TxtBoxValueProperty, value)
End Set
End Property
Public Sub StartLeafUp()
Dim sb As Storyboard = TryCast(Me.Resources("sb2"), Storyboard)
sb.Begin()
End Sub
Public Sub StartLeafDown()
Dim sb As Storyboard = TryCast(Me.Resources("sb2"), Storyboard)
sb.Stop()
End Sub
End Class
Friend Class MyCloneConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If TypeOf value Is Freezable Then
value = TryCast(value, Freezable).Clone()
End If
Return value
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotSupportedException()
End Function
End Class
主窗口和后面的代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UserControlTests" x:Class="MainWindow"
Title="MainWindow" Height="437" Width="1057">
<Grid>
<local:colorCompButton x:Name="color" Margin="201,62,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" BadgeColor="#FF833E1D" buttonColor="Sienna" RectColor="Sienna" ImageSource="pack://siteoforigin:,,,/images/deviation.png" TxtBoxValue="I WORK"/>
<Button x:Name="button" Content="WORKS FINE" HorizontalAlignment="Left" Margin="336,262,0,0" VerticalAlignment="Top" Width="160" Height="53"/>
<Button x:Name="button1" Content="DOES NOT WORK!!" HorizontalAlignment="Left" Margin="534,262,0,0" VerticalAlignment="Top" Width="144" Height="53"/>
<local:colorCompButton HorizontalAlignment="Left" Margin="534,62,0,0" VerticalAlignment="Top" BadgeColor="Indigo" RectColor="IndianRed" ImageSource="pack://siteoforigin:,,,/images/deviation.png" TxtBoxValue="I WON'T WORK" x:Name="res"/>
<Label x:Name="label" Content="EXACT SAME CONTROLS. WHY WILL THIS NOT WORK??" HorizontalAlignment="Left" Margin="375,365,0,0" VerticalAlignment="Top"/>
</Grid>
Imports System.Windows.Media.Animation
Class MainWindow
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
color.StartLeafUp()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
res.StartLeafUp()
End Sub
End Class
【问题讨论】: