XAML ProgressRing 不会停止
XAML ProgressRing doesn't stop
我的问题是 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>();}}
首先您需要在更改 LoaderVisibility
属性 时触发 PropertyChanged
事件,以便将新值通知 UI。请参阅有关如何 Implement Property Change Notification.
的示例
LoaderVisibility 属性 应该是这样的(这个 VM class 需要实现 INotifyPropertyChanged
接口,所以你有 PropertyChanged
事件)。
private Visibility _loaderVisibility;
public Visibility LoaderVisibility
{
get { return _loaderVisibility; }
set
{
_loaderVisibility = value;
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("LoaderVisibility"));
}
}
其次,不要在后面的代码中设置 ProgressRing's Visibility
属性,这会破坏您在 XAML 中连接的数据绑定。
我的问题是 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>();}}
首先您需要在更改 LoaderVisibility
属性 时触发 PropertyChanged
事件,以便将新值通知 UI。请参阅有关如何 Implement Property Change Notification.
LoaderVisibility 属性 应该是这样的(这个 VM class 需要实现 INotifyPropertyChanged
接口,所以你有 PropertyChanged
事件)。
private Visibility _loaderVisibility;
public Visibility LoaderVisibility
{
get { return _loaderVisibility; }
set
{
_loaderVisibility = value;
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("LoaderVisibility"));
}
}
其次,不要在后面的代码中设置 ProgressRing's Visibility
属性,这会破坏您在 XAML 中连接的数据绑定。