当我通过增加它的宽度和位置加载到页面时,我正在尝试使用for循环创建动画。我的问题是Application.Current.MainWindow.Width
for循环中的Page_Loaded
不起作用。虽然离开页面时效果很好。我的代码:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
for (double i = 0; i < 50; i++)
{
Application.Current.MainWindow.Left = Application.Current.MainWindow.Left - 2;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width + 4;
}
}
private void Home_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
for (double i = 0; i < 50; i++)
{
Application.Current.MainWindow.Left = Application.Current.MainWindow.Left + 2;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width - 4;
}
}
当我转到目标页面时,我的窗口向左移动,但宽度不会增加,直到页面完全加载并且它只是跳转到最终宽度而不是动画。当我离开页面时,我的窗口和宽度都在一起改变。
在Page_Loaded下设置宽度时是否有一些限制?如果是这样,我怎么能克服这个?
XAML设置
mc:Ignorable="d"
Loaded="Page_Loaded"
Title="DatabaseViewer">
<Grid >
<TextBlock Height="75" Margin="65,20,65,0" Text="Solder Paste Database" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" />
<Button Margin="0,17,30,0" Width="50" Height="50" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Top" BorderThickness="0" Click="Export_Click"
Style="{DynamicResource SquareButtonStyle}" ToolTip="Export to Excel">
<StackPanel>
<Rectangle Width="40" Height="40" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Green">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_office_excel}" />
</Rectangle.OpacityMask>
</Rectangle>
</StackPanel>
</Button>
<DataGrid Name="dbGrid" Width="750" Height="340" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20"
HorizontalContentAlignment="Center" IsReadOnly="True" PreviewKeyDown="dbGrid_KeyDown" GridLinesVisibility="All"
BorderBrush="Black" BorderThickness="1" SelectionChanged="dbGrid_SelectionChanged" >
</DataGrid>
<Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,70,25,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label FontSize="14" FontWeight="Bold" Grid.Column="0" Grid.Row="0" Content="Selected Count: " HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<Label FontSize="14" Grid.Column="1" Grid.Row="0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Bottom" x:Name="selectedCount" />
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,70,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="26" />
</Grid.RowDefinitions>
<TextBox Width="150" FontSize="14" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" x:Name="TB" ToolTip="Column Name = Value [& Column Name = Value]"/>
<Button Margin="10,0,0,0" Padding="0,0,0,0" FontSize="14" Width="80" Content="Filter" Grid.Column="1" Grid.Row="0" VerticalContentAlignment="Center"
HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="filter_click" />
<Label Grid.Column="2" x:Name="filterLabel" Content="Invalid Search" Foreground="Red" FontWeight="Bold"/>
</Grid>
<Button Margin="20,27,0,0" Width="30" Height="30" FontSize="18" HorizontalAlignment="Left" Click="Home_Click" VerticalAlignment="Top" BorderThickness="0" UseLayoutRounding="True" BorderBrush="White"
Style="{DynamicResource MetroCircleButtonStyle}" >
<Rectangle Width="30" Height="30"
Fill="#41b1ff">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{DynamicResource appbar_arrow_left}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
</Grid>
答案 0 :(得分:1)
使用显式代码更改循环中的维度很可能会导致批处理,如您所见。更不用说你在所有这些过程中阻止了UI,所以我很惊讶你在循环过程中看到了任何变化。
基本上动画的正确方法是使用WPF Storyboards。在这里,您将定义开始和结束条件,您想要设置动画的内容(宽度,左侧)以及动画应该运行多长时间并告诉它播放。故事板然后运行,您无需再做任何事情。您可以在代码或直接XAML中定义它。
答案 1 :(得分:1)
MickyD是对的,故事板是这种情况下最好的选择。虽然我可以通过在代码隐藏中循环来实现您想要的结果,但结果往往是您看到的错误。例如,在您的情况下,您可能希望在Windows资源中进行DoubleAnimation
扩展。
XAML
<Window.Resources>
<Storyboard x:Key="sb2">
<DoubleAnimation Storyboard.TargetName="myWindowName" Storyboard.TargetProperty="Width" From="500" To="1200" Duration="0:0:3" AutoReverse="False"/>
<DoubleAnimation Storyboard.TargetName="myWindowName" Storyboard.TargetProperty="Left" From="500" To="200" Duration="0:0:3" AutoReverse="False"/>
</Storyboard>
</Window.Resources>
然后在您的窗口(或控件)Loaded
事件
private void myWindowName_Loaded(object sender, RoutedEventArgs e)
{
//Reference System.Windows.Media.Animation;
Storyboard storyBoardIn = (Storyboard)TryFindResource("sb2");
storyBoardIn.Begin();
}
您显然需要在XAML中更改故事板参数,以实现您想要的确切内容。这种解决方案在WPF中代码更少,更可靠。祝你好运!