C# UWP正在尝试使用附加的依赖属性设置scrollviewer的动画
我试图在UWP中设置scrollviewer水平偏移的动画。但动画目标无法识别附加的属性C# UWP正在尝试使用附加的依赖属性设置scrollviewer的动画,c#,uwp,windows-10-universal,uwp-xaml,doubleanimation,C#,Uwp,Windows 10 Universal,Uwp Xaml,Doubleanimation,我试图在UWP中设置scrollviewer水平偏移的动画。但动画目标无法识别附加的属性 <Grid x:Name="maingrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.Resources> <Storyboard x:Key="animation" x:Name="animation"> <Do
<Grid x:Name="maingrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Storyboard x:Key="animation" x:Name="animation">
<DoubleAnimation Storyboard.TargetName="scrolviewer"
Storyboard.TargetProperty="(local:ScrollviewerBehaviour.Horizontalofset)"
Duration="0:0:1" From="0" To="80"/>
</Storyboard>
</Grid.Resources>
<Button Width="100" Height="50" Click="Button_Click_1"/>
<ScrollViewer x:Name="scrolviewer" local:ScrollviewerBehaviour.Horizontalofset="0" Width="200" Height="100" HorizontalScrollBarVisibility="Visible">
<TextBlock Text="aaaaaaaaaaaaaaaaaaaaaaaaa" FontSize="20"/>
</ScrollViewer>
</Grid>
我是否遗漏了任何内容由于Windows运行时XAML实现的现有限制,您无法设置自定义附加属性的动画。请参阅中的动画XAML附加属性部分 对于您的情况,您可以创建一个自定义usercontrol并为此usercontrol定义一个依赖项属性。然后,可以设置此依赖项属性的动画 在该属性的PropertyChangedCallback处理程序方法中,可以更改ScrollViewer的Horizontalofset 有关详细信息,请参阅我的以下代码示例:
<UserControl
x:Class="Appanimate.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Appanimate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<ScrollViewer x:Name="scrolviewer" Width="200" Height="100" HorizontalScrollBarVisibility="Visible">
<TextBlock Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" FontSize="20"/>
</ScrollViewer>
</Grid>
请注意,您需要启用
EnableDependentAnimation
,否则,您的动画将无法工作。您是否通过我的解决方案解决了问题?
<UserControl
x:Class="Appanimate.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Appanimate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<ScrollViewer x:Name="scrolviewer" Width="200" Height="100" HorizontalScrollBarVisibility="Visible">
<TextBlock Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" FontSize="20"/>
</ScrollViewer>
</Grid>
public sealed partial class MyUserControl1 : UserControl
{
public MyUserControl1()
{
this.InitializeComponent();
}
public double Horizontalofset
{
get { return (double)GetValue(HorizontalofsetProperty); }
set { SetValue(HorizontalofsetProperty, value); }
}
// Using a DependencyProperty as the backing store for Horizontalofset. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HorizontalofsetProperty =
DependencyProperty.Register("Horizontalofset", typeof(double), typeof(MyUserControl1), new PropertyMetadata(0,PropertyChangedCallback));
public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var distance = (d as MyUserControl1).scrolviewer.ScrollableWidth;
if (distance > (double)e.NewValue)
{
var ret = (d as MyUserControl1).scrolviewer.ChangeView((double)e.NewValue, (d as MyUserControl1).scrolviewer.VerticalOffset, (d as MyUserControl1).scrolviewer.ZoomFactor);
Debug.WriteLine(ret);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
animation.Begin();
}