1

Storyboard当关闭窗口“此 Visual 未连接到 PresentationSource”时,我得到了此错误。

我的场景包括两个窗口和一个UserControl

这是window1的xaml代码:

<Window x:Name="LWindow" x:Class="WpfAppXtesting.WindowTest1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppXtesting"
    mc:Ignorable="d"
    WindowStyle="None"
    Title="WindowTest1" Height="160" Width="435">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        </ResourceDictionary.MergedDictionaries>

        <!-- Storyboard -->
        <Storyboard x:Key="OpenLWindow">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="LWindow">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="434.4"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="LWindow">
                <EasingDoubleKeyFrame KeyTime="0" Value="37"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="37"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="159.8"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="CloseLWindow" Completed="CloseLWindow_Completed">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="LWindow">
                <EasingDoubleKeyFrame KeyTime="0" Value="434.4"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="434.4"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="LWindow">
                <EasingDoubleKeyFrame KeyTime="0" Value="159.8"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="37"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

    </ResourceDictionary>
</Window.Resources>

<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource OpenLWindow}"/>
    </EventTrigger>
</Window.Triggers>

<Grid>
    <Button Content="Button" Width="267" Click="Button_Click" Margin="81,43,80.8,38.4" />

</Grid>

这是window1背后的代码:

using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace WpfAppXtesting
{
/// <summary>
/// Interaction logic for WindowTest1.xaml
/// </summary>
public partial class WindowTest1 : Window
{
    private Storyboard closeLWindow;
    public WindowTest1()
    {
        InitializeComponent();
        closeLWindow = FindResource("CloseLWindow") as Storyboard;
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.closeLWindow.Begin();
    }
    private void CloseLWindow_Completed(object sender, EventArgs e)
    {
        this.Close();
    }
}
}

这是window2的xaml代码:

<Window x:Class="WpfAppXtesting.WindowTest2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppXtesting"
    mc:Ignorable="d"
    Title="WindowTest2" Height="450" Width="800">
<Grid>
    <local:UserControl1 HorizontalAlignment="Left" Height="100" Margin="52,10,0,0" VerticalAlignment="Top" Width="642"/>

</Grid>

这是window2背后的代码:

using System.Windows;
namespace WpfAppXtesting
{
/// <summary>
/// Interaction logic for WindowTest2.xaml
/// </summary>
public partial class WindowTest2 : Window
{
    public WindowTest2()
    {
        InitializeComponent();
    }


}
}

这是 usercontrol1 的代码:

<UserControl x:Name="Test1" x:Class="WpfAppXtesting.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfAppXtesting"
         mc:Ignorable="d" 
         d:DesignHeight="32" d:DesignWidth="800">

<Grid x:Name="GridRoot" Background="Aqua" Margin="0,0,447,0">
    <Button Content="Button" Click="Button_Click" Margin="41,0,35,0" />

</Grid>

这是usercontrol1的代码:

using System.Windows;
using System.Windows.Controls;
namespace WpfAppXtesting
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowTest1 lWindow = new WindowTest1();
        lWindow.ShowDialog();
    }
}
}

我认为问题出在windowsstyle=none.

如何解决这个问题。

4

1 回答 1

1

尝试使用Dispatcher该类在消息循环的下一次迭代中关闭窗口:

private void CloseLWindow_Completed(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(new Action(() => Close()));
}
于 2018-06-20T10:12:53.030 回答