2012-01-11 54 views
3

我怎樣才能顯示一個消息框或一些其他類似的通知顯示,而應用程序是忙的背景下,類似這樣的樣機:消息框忙着在WPF

enter image description here

+0

你的意思是等待通知,如玻璃小時? – 2012-01-11 07:01:04

+0

[有沒有WPF消息框?](http:// stackoverflow。com/questions/3830228/is-there-a-wpf-message-box)或者你總是可以創建自己的窗口... – 2012-01-11 07:01:51

+1

@CodyGray:我不這麼說這是OP所暗示的,它更多的是等待在UI上完成處理時顯示的圖標。 – 2012-01-11 07:02:46

回答

8

有一個Busy Indicator

Screenshot of the busy indicator

該工具包可以方便地通過的NuGet這使得它很容易將其添加爲:我已經使用了很多的WPF Extended Toolkit對您的項目的參考。我個人幾乎在最近的WPF項目中都使用過它(以及該工具包中的其他許多有用控件)。

要使用它,環繞在繁忙的指標XAML代碼的控制:

<extToolkit:BusyIndicator ...> 
    <Grid> 
     <Button Content="Click to do stuff" /> 
     <!-- your other stuff here --> 
    </Grid> 
</extToolkit:BusyIndicator>  

然後你只需在你想要的彈出窗口出現在IsBusy屬性設置爲true和假當它應該被隱藏。在一個適當的MVVM架構,您通常會在數據綁定XAML的財產在你的視圖模型的屬性然後您可以設置爲真/假相應:

<extToolkit:BusyIndicator IsBusy="{Binding IsBusy}" > 

但是,如果你不使用MVVM,你當然可以從您的代碼手動設置它的背後,通常在按鈕單擊處理程序:

給該控件的名稱,即可從代碼中引用它:

​​

,然後在你的xaml.cs文件:

void myButton_Click(object sender, RoutedEventArgs e) 
{ 
    busyIndicator.IsBusy = true; 
    // Start your background work - this has to be done on a separate thread, 
    // for example by using a BackgroundWorker 
    var worker = new BackgroundWorker(); 
    worker.DoWork += (s,ev) => DoSomeWork(); 
    worker.RunWorkerCompleted += (s,ev) => busyIndicator.IsBusy = false; 
    worker.RunWorkerAsync(); 
} 
+0

感謝您的幫助 – murmansk 2012-01-12 06:13:04

4

如果你的代碼MVVM很容易:

1)添加一個布爾標誌「IsBusy」與更改通知您的視圖模型。

public bool IsBusy {get {return _isBusy;} set{_isBusy=value;OnPropertyChanged("IsBusy");}} 
private bool _isBusy; 

2)添加兩個事件到你的命令「開始」和「完成」

public event Action Completed; 

public event Action Started; 

3.)在您的視圖模型,訂閱這些事件並設置忙碌狀態。

LoadImagesCommand.Started += delegate { IsBusy = true; }; 
LoadImagesCommand.Completed += delegate { IsBusy = false; }; 

4)在窗口,你現在可以綁定到該狀態

<Popup Visibility="{Binding Path=IsBusy,Converter={StaticResource boolToVisibilityConverter}}"/> 

注意,對於最後一步,你必須實例化的boolToVisibilityConverter,所以:

5)加入以下任何加載的資源詞典:

<BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/> 

就是這樣!你可以用你想要的生活填充你的Popup ...

+0

如果您不使用MVVM,這也很容易;這與MVVM無關。你可以做任何你可以用MVVM做的事情,但最終不會變得麻煩。 – 2012-01-11 08:28:41

+0

我不認爲這MVVM的東西進入我的大腦,我對WPF很新,是他們的任何簡單的方法只使用.caml和.xaml.cs文件 – murmansk 2012-01-11 09:11:29

+0

使用xaml.cs,你可以做到這一點。只有這樣才能讓你的UI更具地位,但基本上你可以在你的XAML中定義一個Popup,然後使用Popup.Visibility = Visibility.Hidden/Visible來隱藏或顯示你的彈出窗口。 – 2012-01-11 11:30:07