Wpf 在加载的事件中未完成渲染

Wpf 在加载的事件中未完成渲染,wpf,animation,opacity,loaded,Wpf,Animation,Opacity,Loaded,我订阅了wpf窗口的Loaded事件:Loaded+=Loaded并尝试更改代码隐藏中某些控件的不透明度。 我注意到,在方法loaded中,wpf尚未绘制控件。因此,代码无效,控件的呈现仅在方法退出后发生 1) 是否有其他事件(例如,呈现的)可供我订阅 编辑:我刚刚发现有一个OnContenterned事件,下面的代码起作用: 虽然动画可能更可取 protected override void OnContentRendered(EventArgs e) { base.OnContentR

我订阅了wpf窗口的Loaded事件:
Loaded+=Loaded
并尝试更改代码隐藏中某些控件的不透明度。
我注意到,在方法
loaded
中,wpf尚未绘制控件。因此,代码无效,控件的呈现仅在方法退出后发生

1) 是否有其他事件(例如,
呈现的
)可供我订阅

编辑:我刚刚发现有一个OnContenterned事件,下面的代码起作用:
虽然动画可能更可取

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);
   }
}
protected override void oncontentered(事件参数e)
{
基础。无内容(e);
对于(int i=0;i<100;i++)
{
Parentpanel.Opacity+=0.01;
飞溅屏。不透明度-=0.01;
Invoke(新操作(()=>{}),DispatcherPriority.ContextIdle,null);
睡眠(50);
}
}
否则,我可能必须使用一个动画,将usercontrol1的不透明度从0.1更改为1.0,将usercontrol2的不透明度从1.0更改为0.0


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

最近,当可见性发生变化时,我试图向WPF usercontrols渲染一些标准化的动画时遇到了问题。在我的应用程序中,我有几个单例静态类。在一个例子中,我添加了一个静态方法“VisibleFader”,然后传入框架元素控件,它会根据不透明属性自动将事件处理程序附加到双重动画。它工作得很好,不需要对任何其他样式、控件模板或任何其他主题实现进行任何更改

public static DoubleAnimation da;
public static void VisibleFader(FrameworkElement fe)
{
   if (da == null)
   {
      da = new DoubleAnimation();
      da.From = 0;
      da.To = 1;
      da.Duration = new Duration(TimeSpan.FromSeconds(.7));
   }

   fe.IsVisibleChanged += myFader;
}

private static void myFader(object sender, DependencyPropertyChangedEventArgs e)
{
   ((FrameworkElement)sender).BeginAnimation(FrameworkElement.OpacityProperty, da);
}
然后,在我的类中(比如你加载的事件),我只是用“userControl”对象调用这个静态方法


完成。。。因此,当可见性发生变化时,它会从零逐渐变为1。如果某个东西被隐藏了,它无论如何都会消失。

在加载的处理程序中,您可以在dispatcher上发布UI更改操作(例如,
void changepacity()
):

Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ChangeOpacity));
它将在渲染完成后执行

编辑

我看你只需要在窗口打开时启动动画。在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>


您的控件在哪里定义?例如,如果在
控制模板
中定义了它们,则在引发
加载
事件时,它们可能尚未准备就绪。在这种情况下,您将需要处理。我稍后将对此进行研究,看起来很有希望,请参阅我的编辑顺便说一句。正确的是,您的调度程序看起来比我的idea ONCONTENDERED更好,尽管在每一步之后都必须调用调度程序进行刷新。动画效果更好,看起来也更好。这对我来说很好,谢谢。我的Visual Basic.NET 4.5.1等效版本是:
Dispatcher.BeginInvoke(新操作(AddressOf methodName)、Windows.Threading.DispatcherPriority.Loaded)
<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>