Binding WP8-绑定进度条可见性
我有一个简单的代码,我检查了其他问题,但还不能 我有一个应用程序,它从从web检索的xml文件中加载一些数据,然后将其显示在longlistselector中 我做到了,它工作了,现在我想添加一个不确定的progressbar,它在我完成数据加载之前保持活动状态 我在longlistselector之前将progressbar封装在stackpanel中,并将其可见性绑定到函数ProgressBarVisibility(请参见下面的代码)Binding WP8-绑定进度条可见性,binding,windows-phone-8,progress-bar,visibility,Binding,Windows Phone 8,Progress Bar,Visibility,我有一个简单的代码,我检查了其他问题,但还不能 我有一个应用程序,它从从web检索的xml文件中加载一些数据,然后将其显示在longlistselector中 我做到了,它工作了,现在我想添加一个不确定的progressbar,它在我完成数据加载之前保持活动状态 我在longlistselector之前将progressbar封装在stackpanel中,并将其可见性绑定到函数ProgressBarVisibility(请参见下面的代码) 在MainViewModel.cs中,我就是这样写的
在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;
公共类MainViewModel:INotifyPropertyChanged
{
公共主视图模型()
{
this.PivotOne=新的ObservableCollection();
this.pivotwo=新的ObservableCollection();
this.PivotThree=新的ObservableCollection();
}
///
///ItemViewModel对象的集合。
///
公共可观测集合数据透视一{get;private set;}
公共可观测集合{get;private set;}
公共可观测集合{get;private set;}
私有字符串\u detailPageTitle=“默认”;
///
///详细页面标题:丽托娜·伊尔·蒂托洛·迪塔格里奥·帕吉纳。塞塔托·内拉·福齐安·切·卡卡利·帕吉纳第二集
///
///
公共字符串DetailPageTitle
{
得到
{
返回详细页面标题;
}
设置
{
如果(值!=\u detailPageTitle)
{
_detailPageTitle=值;
NotifyPropertyChanged(“DetailPageTitle”);
}
}
}
公共布尔值已加载
{
得到;
私人设置;
}
私有可见性_progressBarVisibility=可见性。已折叠;
公众能见度
{
得到
{
返回(u)可见性;
}
设置
{
如果(值!=\u)
{
_progressBarVisibility=值;
NotifyPropertyChanged(“ProgressBarVisibility”);
}
}
}
私有可见性_progressBarVisibility=可见性.Visibility;
公众能见度
{
得到
{
返回(u)可见性;
}
设置
{
如果(值!=\u)
{
_progressBarVisibility=值;
NotifyPropertyChanged(“ProgressBarVisibility”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共void LoadData()
{
//progressbar可见,未加载数据
this.IsDataLoaded=false;
ProgressBarVisibility=可见性。可见;
//加载静态和动态数据--填充不同的数据透视
LoadStaticData();
LoadXMLFile();
//数据已加载,progressbar已折叠
this.IsDataLoaded=true;
ProgressBarVisibility=可见性。已折叠;
}
因此,我包括system.windows库,并使用visibility类。
无论如何,加载完成后,我无法让progressbar消失,它一直在运行
有什么建议吗?我哪里做错了
提前谢谢
解决方案:loaddata在应用程序激活时执行,因此此时甚至不会呈现内容。您需要报告对视图所做的更改: 改变
public Visibility ProgressBarVisibility { get; set; }
借
确保实现INotifyPropertyChanged或实现它的基础ViewModel(MVVMLigth:ViewModelBase)
InotifyProperty的一些实现带来了许多变化,但您可以轻松实现自己。最好的方法是替换Visibilit类型并将其更改为bool,然后使用BooleanToVisibilityConverter。您好,我实际上已经在我的模型中实现了它。我使用了它,但无论如何我都无法让它工作。我现在正确地编辑了这个问题。您好,我实际上在我的模型中实现了它。我使用了它,但无论如何我都无法让它工作。我现在正确地编辑了这个问题。@Mik1893,为了确保我们没有遗漏一些明显的内容,您能发布您的
NotifyPropertyChanged
实现和MainViewModel类声明吗(您是否真的在实现INotifyPropertyChanged
接口?)@Mik1893,另一件要检查的事情是,当您调试应用程序并将其添加到主应用程序时,输出窗口中是否有任何绑定错误question@Mik1893,鉴于您发布的代码,我无法重现您的错误(在我的情况下,进度条已折叠);还有其他一些问题。顺便说一句,您实现LoadStaticData
的方式似乎是在UI线程上进行同步操作,这意味着您的UI可能没有响应。或者,如果您的LoadData
是从MainPage.cs的构造函数调用的,那么数据将在页面启动之前加载甚至渲染,这意味着您的进度条甚至不会被渲染。
public Visibility ProgressBarVisibility { get; set; }
private Visibility _progressBarVisibility;
public Visibility ProgressBarVisibility
{
get { return _progressBarVisibility;}
set { _progressBarVisibility = value; RaisePropertyChanged("ProgressBarVisibility");}
}