更改事件处理程序的属性
本文关键字:属性 程序 事件处理 | 更新日期: 2023-09-27 18:27:00
我希望进度条在下载完成之前一直可见。
为了做到这一点,我将"Visibility"属性与一个名为"loadBarVisibility"的属性绑定。
XAML代码:
<ProgressBar Visibility="{Binding loadBarVisibility}" IsIndeterminate="True"/>
ViewModel代码:
public class MainViewModel : ViewModelBase
{
public static Visibility loadBarVisibility { get; set; }
private const String URL = "myurl";
public MainViewModel()
{
loadFlux();
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
}
}
public static void loadFlux()
{
loadBarVisibility = Visibility.Visible;
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(URL));
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(test_downloadFinished);
}
public static void test_downloadFinished(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
loadBarVisibility = Visibility.Collapsed;
}
}
}
但是上面的代码不起作用:进度条仍然可见。
否则,当我在"client.DownloadStringCompleted+=new DownloadStringCompletedEventHandler(test_downloadFinished);"之后放入"loadBarVisibility=Visibility。Collapsed;"时,它就起作用了
有办法解决我的问题吗?
非常感谢。
ViewModel需要实现接口INotifyPropertyChanged,以通知您的视图属性可见性已更改(http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx)