虽然我没有清楚地得到这个问题,但我试图尽可能地给出这个答案。:-)
在我的场景中,我创建了一个主窗口,它在单击按钮时加载耗时的控件。在加载耗时的控件时,我保持了 10 秒的睡眠。
主窗口代码:
Xaml -
<Window x:Class="Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf"
Title="MainWindow" Height="350" Width="525"
x:Name="root">
<Window.Resources>
<DataTemplate x:Key="loadingTemplate" DataType="ContentControl">
<StackPanel>
<TextBlock Text="Loading..." Margin="5"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel x:Name="stpRootLayout">
<Button Content="Click to load pane" Click="Button_Click"/>
<Border BorderThickness="5" BorderBrush="Black" Height="100">
<ContentControl x:Name="cctPlaceHolder">
</ContentControl>
</Border>
</StackPanel>
Xaml.cs -
public delegate void LoadTimeConsumingControlDelegate();
public void LoadTimeConsumingControl()
{
//set our progress dialog text and value
cctPlaceHolder.Content = new TimeConsumingControl();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
cctPlaceHolder.ContentTemplate = this.TryFindResource("loadingTemplate") as DataTemplate;
System.Windows.Threading.Dispatcher cctDispatcher = cctPlaceHolder.Dispatcher;
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
LoadTimeConsumingControlDelegate update = new LoadTimeConsumingControlDelegate(LoadTimeConsumingControl);
cctDispatcher.BeginInvoke(update);
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
cctPlaceHolder.ContentTemplate = null;
};
worker.RunWorkerAsync();
}
耗时控制:
Xaml -
<UserControl x:Class="Wpf.TimeConsumingControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="some text..................................................................................................."/>
</Grid>
Xaml.cs
public TimeConsumingControl()
{
InitializeComponent();
Thread.Sleep(10000);
}
将数据模板替换为您的加载主题。
希望这是您正在寻找的东西。如果没有,你可以回复我。