2013-02-18 65 views
1

我有一個WPF應用程序。我正在使用自動隱藏窗格來託管非常複雜的用戶控件。 當用戶單擊窗格時,大約需要10秒。加載控件,我試圖創建一個「加載...」窗口通知用戶該應用程序正在工作。 在這個等待窗口中,我有一個動畫來旋轉圖像。 我的問題是2天后我仍然無法旋轉輪子,因爲我使用的是與加載usercontrol相同的線程。我的「加載」窗口工作得很好,如果我沒有窗格更新運行它。 我嘗試了幾件事 - BackgroundWorker,Dispatcher.Invoke。我在網絡中找到的所有示例都是關於在保持UI活動的同時執行後臺任務。在UI中更新2個元素

任何鉛?

+0

您正在後臺線程中加載用戶控件,而不是旋轉的輪子 - 對嗎? – 2013-02-18 12:20:38

回答

1

雖然我沒有明確得到這個問題,但我想給這個答案是更好,因爲我能得到這個問題。:-)

我的方案,我已經創建了一個主窗口,加載時間消耗按鈕點擊的控制。加載耗時的控件時,我一直保持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); 
    } 

替換爲您加載主題數據模板。

希望這是你正在尋找的東西。如果沒有,你可以回到我身邊。

+0

非常感謝您的時間和精力。對此,我真的非常感激。在我的動畫沒有移動之前,我嘗試了它。幸運的是我在帖子中找到了解決方案。 – Kulpemovitz 2013-02-18 17:21:15