0
我希望進度條在下載完成之前可見。從事件處理程序中更改屬性
爲了做到這一點,我將「Visibility」屬性與一個名爲「loadBarVisibility」的屬性綁定在一起。
XAML代碼:
<ProgressBar Visibility="{Binding loadBarVisibility}" IsIndeterminate="True"/>
視圖模型代碼:
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);」之後,它就起作用了。
有沒有辦法解決我的問題?
非常感謝。
我加了: 新的公共事件PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) if(PropertyChanged!= null) PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } 但我不能叫NotifyPropertyChanged因爲「loadbarvisibility」是一個靜態的屬性(不能從setter方法調用).. – Tang 2012-03-06 15:36:37
方法和屬性在你視圖模型不應該是一成不變的。有關MVVM模式的完整概述,請參閱http://msdn.microsoft.com/en-us/magazine/dd419663.aspx – Guillaume 2012-03-06 16:03:38