执行故事板以关闭在 showdialog 中打开的 window 时出错
Error when execute storyboard to close a window open in showdialog
当 Storyboard
关闭 window 'This Visual is not connected to a PresentationSource.'
时,我得到了这个错误
我的场景包含两个 window 和一个 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();
}
}
}
这是 xaml window2:
的代码
<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
。
如何解决这个问题。
尝试使用 Dispatcher
class:
在消息循环的下一次迭代中关闭 window
private void CloseLWindow_Completed(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(new Action(() => Close()));
}
当 Storyboard
关闭 window 'This Visual is not connected to a PresentationSource.'
我的场景包含两个 window 和一个 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();
}
}
}
这是 xaml window2:
的代码<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
。
如何解决这个问题。
尝试使用 Dispatcher
class:
private void CloseLWindow_Completed(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(new Action(() => Close()));
}