简体   繁体   中英

WPF Toolkit BusyIndicator

I have a problem trying to update the UI. What I need it is that after displaying the BusyIndicator , need the message changes, and when finished 5 seconds, display another message for two seconds and then hide the BusyIndicator . Thx!

XAML

<xctk:BusyIndicator IsBusy="{Binding IsBusy}" DisplayAfter="0">
    <xctk:BusyIndicator.BusyContentTemplate>
        <DataTemplate>
            <StackPanel>
                <mahApps:ProgressRing IsActive="{Binding IsBusy}"/>
                <Label Content="{Binding ShowMessage}"/>
            </StackPanel>
        </DataTemplate>
    </xctk:BusyIndicator.BusyContentTemplate>

    ...

</xctk:BusyIndicator>

XAML ViewModel

public string ShowMessage
{
    get { return _showMessage; }
    set
    {
        _showMessage = value;
        RaisePropertyChanged("ShowMessage");
    }
}

private void Save()
{
    ShowMessage = "Wait please...";

    Task.Factory.StartNew(() =>
    {
        IsBusy = true; // Show busyindicator and ProgressRing

        Thread.Sleep(5000); // 5 seconds to see the animation (Here is a SQL insert)

        /// Hide ProgressRing only

        ShowMessage = "Save complete.";

        Thread.Sleep(2000); // 2 seconds to see "ShowMessage"

    }).ContinueWith(x =>
    {
        IsBusy = false; // hide busyindicator and ProgressRing

        ...

    }, TaskScheduler.FromCurrentSynchronizationContext());
}

在此处输入图片说明

A bit late but, ShowMessage = "Save complete."; is not running on the UI thread. For RaisePropertyChanged to work it must, so you need insert another Continuation and Task to execute ShowMessage using FromCurrentSynchronizationContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM