可见性切换

时间:2014-08-30 09:29:13

标签: c# wpf

在我的XAML中,有一个按钮和一个标签。启动时,按钮可见,标签不可见。单击install_btn时,标签应变为可见而不是按钮。 这是代码的一部分:

private void install_btn_Click(object sender, RoutedEventArgs e)
{
    inst_label.Visibility = Visibility.Visible;
    progress.Visibility = Visibility.Visible;
    install_btn.Visibility = Visibility.Hidden;
}

然后有一些像webClient.DownloadFile()这样的代码。但只有在处理webClient之后,可见性才会切换。如何在下载文件(或其他内容)之前执行切换?

1 个答案:

答案 0 :(得分:-1)

您应该允许应用程序更新它的UI:

private void install_btn_Click(object sender, RoutedEventArgs e)
{
    inst_label.Visibility = Visibility.Visible;
    progress.Visibility = Visibility.Visible;
    install_btn.Visibility = Visibility.Hidden;
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
    webClient.DownloadFile();
}