###前提・実現したいこと
UWPのUserControlをStoryboardを使ったアニメーションで動かしたいと思っています。
後述のコードを実行したところ、MainPageでStoryboard.Begin()を実行したタイミングで以下の例外が発生しました。
UserControlで独自に追加したプロパティ(HogeValue)をStoryboardのターゲットプロパティにするにはどうしたらよいのでしょう。
宜しくお願い致します。
###エラーメッセージ
例外がスローされました: 'System.Runtime.InteropServices.COMException' (mscorlib.ni.dll の中) WinRT 情報:Cannot resolve TargetProperty HogeValue on specified object.
###該当のソースコード
** MyTextBlock.xaml.cs **
C#
1 public sealed partial class MyTextBlock : UserControl 2 { 3 public static readonly DependencyProperty HogeValueProperty = DependencyProperty.Register( 4 "HogeValue", 5 typeof(MyTextBlock), 6 typeof(double), 7 new PropertyMetadata(0d, (sender, ev) => 8 { 9 var control = (MyTextBlock)sender; 10 if (control != null) 11 { 12 control.OnHogeValueChanged(); 13 } 14 })); 15 16 public double HogeValue 17 { 18 get { return (double)GetValue(HogeValueProperty); } 19 set { SetValue(HogeValueProperty, value); } 20 } 21 22 private void OnHogeValueChanged() 23 { 24 HogeText.Text = HogeValue.ToString(); 25 } 26 27 public MyTextBlock() 28 { 29 this.InitializeComponent(); 30 } 31 }
** MyTextBlock.xaml **
XML
1<UserControl 省略...> 2 <Grid> 3 <TextBlock x:Name="HogeText"/> 4 </Grid> 5</UserControl>
MainPage.xaml.cs
C#
1 2 public sealed partial class MainPage: Page 3 { 4 public MainPage() 5 { 6 this.InitializeComponent(); 7 } 8 9 private void Button_Tapped(object sender, TappedRoutedEventArgs e) 10 { 11 Storyboard hogeStoryboard = new Storyboard(); 12 13 DoubleAnimation hogeAnimation = new DoubleAnimation 14 { 15 Duration = TimeSpan.FromSeconds(10), 16 From = 0d, 17 To = 100d, 18 }; 19 hogeAnimation.EnableDependentAnimation = true; 20 Storyboard.SetTarget(hogeAnimation, MyText); 21 Storyboard.SetTargetProperty(hogeAnimation, "HogeValue"); 22 hogeStoryboard.Children.Add(hogeAnimation); 23 hogeStoryboard.Begin(); 24 } 25 }
MainPage.xaml
XML
1<Page 省略...> 2 <StackPanel> 3 <ctrl:MyTextBlock x:Name="MyText" HorizontalAlignment="Center" VerticalAlignment="Center" HogeValue="0" /> 4 <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Start" Tapped="Button_Tapped"/> 5 </StackPanel> 6</Page>
###試したこと
Storyboard.SetTargetProperty()の第2引数を
"(MyTextBlock.HogeValue)" "(HogeValue)" "MyTextBlock.HogeValue"
に変更してみましたが結果は同じでした。
###補足情報(言語/FW/ツール等のバージョンなど)
C# + XAML
VisualStudio 2015
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。