我希望在下载完成之前可以看到进度条。
为此,我将“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;
}
}
}
但上面的代码不起作用:进度条仍然可见。
否则,当我输入“loadBarVisibility = Visibility.Collapsed;”就在“client.DownloadStringCompleted + = new DownloadStringCompletedEventHandler(test_downloadFinished);”之后,它有效。
有没有办法解决我的问题?
非常感谢。
答案 0 :(得分:2)
您的ViewModel需要实现接口INotifyPropertyChanged以通知您查看属性visiblilty已更改(http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx)