【问题标题】:XAML ProgressRing doesn't stopXAML ProgressRing 不会停止
【发布时间】:2016-09-26 15:49:18
【问题描述】:

我的问题是 ProgressRing 一直在运行。我使用 MVVM。 ProgressRing 只能在应用获取新数据时激活。

我在 VM 中有代码(我更新了 ViewModel):

public class MainPageViewModel : ObservableCollection<AdModel>, ISupportIncrementalLoading{
    private Visibility _loaderVisibility;
    public Visibility LoaderVisibility
    {
        get { return _loaderVisibility; }
        private set
        {
            _loaderVisibility = value;

        }}
    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        CoreDispatcher coreDispatcher = Window.Current.Dispatcher;

        return Task.Run<LoadMoreItemsResult>(async () =>
        {
            await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    this.LoaderVisibility = Visibility.Visible;
                });

            var NewAds = new ObservableCollection<AdModel>();

            do{NewAds = await LoadAds();//get data from API}
            while (NewAds == null);

            await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    foreach (AdModel item in NewAds)
                    {
                        this.Add(item);
                    }
                    this.LoaderVisibility = Visibility.Collapsed;
                });

            return new LoadMoreItemsResult() { Count = count };
        }).AsAsyncOperation<LoadMoreItemsResult>();}}

【问题讨论】:

    标签: xaml windows-phone-8 mvvm windows-runtime


    【解决方案1】:

    首先,您需要在更改 LoaderVisibility 属性时触发 PropertyChanged 事件,以便通知 UI 新值。请参阅有关如何Implement Property Change Notification 的示例。

    LoaderVisibility 属性应该如下所示(这个 VM 类需要实现INotifyPropertyChanged 接口,所以你有PropertyChanged 事件)。

    private Visibility _loaderVisibility; 
    public Visibility LoaderVisibility 
    {
        get { return _loaderVisibility; }
        set 
        {
            _loaderVisibility = value;
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("LoaderVisibility"));
        }
    }
    

    其次,不要在后面的代码中设置 ProgressRing's Visibility 属性,这会破坏您在 XAML 中连接的数据绑定。

    【讨论】:

    • 我的类继承自 ObservableCollection 所以实现了这个接口。您的解决方案不起作用,我有:“事件 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' 只能出现在 += 或 -=" 的左侧
    • 你能粘贴整个VM类的代码吗?删除不相关的属性。
    • 已更新。我删除了 XAML 和后面的代码,因为代码很多。
    • ProgressRing 一直有效,但可见性会在适当的时候改变。我不知道..
    • 等一下……你的意思是你的问题不是显示/隐藏 ProgressRing,而是启动/停止它?
    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多