我有一个简单的代码,我检查了其他问题,但还没有。
我有一个应用程序,它从Web检索到的xml文件中加载一些数据,然后将它显示在longlistselector中。
我做到了,它有效,现在我想添加一个不确定的进度条,它一直保持活动状态,直到我完成数据加载。
我将进度条包含在我的longlistselector之前的stackpanel中,并且我将其可见性限制在函数ProgressBarVisibility中(参见下面的代码)。
<phone:PivotItem Header="Status">
<StackPanel>
<ProgressBar Value ="0" IsIndeterminate="True" Visibility="{Binding ProgressBarVisibility}"/>
<phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding PivotOne}">
<phone:LongListSelector.ItemTemplate>
<!-- lots of code here -->
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</StackPanel>
</phone:PivotItem>
在MainViewModel.cs中,这就是我写这个东西的方式。
using System.Windows;
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.PivotOne = new ObservableCollection<ItemViewModel>();
this.PivotTwo = new ObservableCollection<ItemViewModel>();
this.PivotThree = new ObservableCollection<ItemViewModel>();
}
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<ItemViewModel> PivotOne { get; private set; }
public ObservableCollection<ItemViewModel> PivotTwo { get; private set; }
public ObservableCollection<ItemViewModel> PivotThree { get; private set; }
private string _detailPageTitle = "Default";
/// <summary>
/// DetailPageTitle ritorna il titolo della pagina di dettaglio. Viene settato nella funzione che carica la pagina secondaria
/// </summary>
/// <returns></returns>
public string DetailPageTitle
{
get
{
return _detailPageTitle;
}
set
{
if (value != _detailPageTitle)
{
_detailPageTitle = value;
NotifyPropertyChanged("DetailPageTitle");
}
}
}
public bool IsDataLoaded
{
get;
private set;
}
private Visibility _progressBarVisibility = Visibility.Collapsed;
public Visibility ProgressBarVisibility
{
get
{
return _progressBarVisibility;
}
set
{
if (value != _progressBarVisibility)
{
_progressBarVisibility = value;
NotifyPropertyChanged("ProgressBarVisibility");
}
}
}
private Visibility _progressBarVisibility = Visibility.Visible;
public Visibility ProgressBarVisibility
{
get
{
return _progressBarVisibility;
}
set
{
if (value != _progressBarVisibility)
{
_progressBarVisibility = value;
NotifyPropertyChanged("ProgressBarVisibility");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public void LoadData()
{
//progressbar is visible, data not loaded
this.IsDataLoaded = false;
ProgressBarVisibility = Visibility.Visible;
// Load Static and dynamic data -- populate the different pivots
LoadStaticData();
LoadXMLFile();
// data loaded, progressbar collapsed
this.IsDataLoaded = true;
ProgressBarVisibility = Visibility.Collapsed;
}
所以我包含了system.windows库,并使用了可见性类。 无论如何,当加载完成时,我无法让进度条消失,它会继续运行。
有什么建议吗?我在哪里做错了?
提前致谢!
解决方案:在应用程序激活时执行loaddata,因此此时内容甚至无法呈现。
答案 0 :(得分:0)
您需要报告对视图所做的更改:
更改
public Visibility ProgressBarVisibility { get; set; }
通过
private Visibility _progressBarVisibility;
public Visibility ProgressBarVisibility
{
get { return _progressBarVisibility;}
set { _progressBarVisibility = value; RaisePropertyChanged("ProgressBarVisibility");}
}
确保实现INotifyPropertyChanged或实现它的基本ViewModel(MVVMLigth:ViewModelBase)。
答案 1 :(得分:0)
您的MainViewModel必须实现INotifyPropertyChanged
以向View发信号通知其中一个属性已更改。此外,当您更改ProgressBarVisibility
属性时,它应触发PropertyChanged
事件。
有一些MVVM frameworks带有一些INotifyPropertyChanged的实现,但您可以自己轻松实现something simple。