我的问题是ProgressRing一直在运行。我使用MVVM。只有当app获取新数据时,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>();}}
答案 0 :(得分:2)
首先,您需要在更改 (Activity)context).runOnUiThread(new Runnable() {
public void run() {
// your code
Toast.makeText(magContext, "hi", Toast.LENGTH_SHORT).show();
}
});
属性时触发PropertyChanged
事件,以便可以通知UI新值。请参阅有关如何Implement Property Change Notification的示例。
LoaderVisibility属性应该如下所示(此VM类需要实现LoaderVisibility
接口,因此您有INotifyPropertyChanged
事件。
PropertyChanged
其次,不要在代码后面设置private Visibility _loaderVisibility;
public Visibility LoaderVisibility
{
get { return _loaderVisibility; }
set
{
_loaderVisibility = value;
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("LoaderVisibility"));
}
}
属性,这会破坏你在XAML中连接的数据绑定。