渲染未在Loaded事件中完成

Ger*_*ard 4 wpf animation opacity loaded

我订阅了wpf窗口的Loaded事件:Loaded += loaded;并尝试更改后面代码中某些控件的不透明度.
我注意到在方法loaded中控件还没有被wpf绘制.因此代码没有效果,控件的呈现仅在退出方法后才会发生.

1)是否有其他事件,例如Rendered我可以订阅?

编辑:我刚刚发现有一个OnContentRendered事件和以下代码工作:
虽然动画可能是首选.

protected override void OnContentRendered(EventArgs e)
{
   base.OnContentRendered(e);
   for (int i = 0; i < 100; i++)
   {
       Parentpanel.Opacity += 0.01;
       Splashscreen.Opacity -= 0.01;
       Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
       Thread.Sleep(50);
   }
}
Run Code Online (Sandbox Code Playgroud)

否则,我可能不得不使用一个动画,将usercontrol1的不透明度从0.1更改为1.0,将usercontrol2的不透明度从1.0更改为0.0.

2)你知道这样一个动画的例子吗?

maj*_*cha 8

在Loaded处理程序中,您可以void ChangeOpacity()在调度程序上发布UI更改操作(例如):

Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ChangeOpacity));
Run Code Online (Sandbox Code Playgroud)

它将在渲染完成后执行.

编辑

我看到你只需要在窗口打开时启动一个动画.它很容易在XAML中完成,这是一个在Blend中生成的工作示例:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Window.Resources>
        <Storyboard x:Key="myStoryboard">
            <DoubleAnimationUsingKeyFrames
                         Storyboard.TargetProperty="(UIElement.Opacity)"
                         Storyboard.TargetName="myControl">
                <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource myStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <StackPanel>
        <TextBox x:Name="myControl" Text="I'm disappearing..." />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)