0

我有一个简单的代码,我检查了其他问题,但还不能。

我有一个应用程序,它从 web 检索的 xml 文件中加载一些数据,然后将其显示在 longlistselector 中。

我做到了,它有效,现在我想添加一个不确定的进度条,它在我完成数据加载之前一直保持活动状态。

我将进度条包含在堆栈面板中,在我的 longlistselector 之前,并将其可见性绑定到函数 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 库,并使用了可见性类。无论如何,加载完成后我无法让进度条消失,它会继续。

有什么建议吗?我在哪里做错了?

提前致谢!

解决方案:在应用程序激活时执行加载数据,因此此时甚至不会呈现内容。

4

2 回答 2

0

您需要报告对视图所做的更改:

改变

public Visibility ProgressBarVisibility { get; set; }

经过

private Visibility _progressBarVisibility;
public Visibility ProgressBarVisibility
{
    get { return _progressBarVisibility;}
    set { _progressBarVisibility = value; RaisePropertyChanged("ProgressBarVisibility");}
}

确保实现 INotifyPropertyChanged 或实现它的基本 ViewModel (MVVMligth : ViewModelBase)。

于 2013-08-18T15:19:28.030 回答
0

您的 MainViewModel 必须实现INotifyPropertyChanged以向 View 发出其中一个属性已更改的信号。此外,当您更改ProgressBarVisibility属性时,它应该触发PropertyChanged事件。

有许多MVVM 框架附带了 INotifyPropertyChanged 的​​一些实现,但您可以轻松地自己实现一些简单的东西。

于 2013-08-18T15:21:19.010 回答