Xamarin-页面冻结,而不是显示活动指示器
我有这样一个页面:Xamarin-页面冻结,而不是显示活动指示器,xamarin,loading,freeze,activity-indicator,Xamarin,Loading,Freeze,Activity Indicator,我有这样一个页面: <StackLayout> <CollectionView ItemsSource="{Binding Galleries}" x:Name="myCollection" SelectionMode="Single" SelectionChanged="CollectionView_SelectionChanged"> <CollectionView.ItemsLayout> <Grid
<StackLayout>
<CollectionView ItemsSource="{Binding Galleries}" x:Name="myCollection" SelectionMode="Single" SelectionChanged="CollectionView_SelectionChanged">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ThumbUrl}"
Aspect="AspectFit" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<ActivityIndicator BindingContext="{x:Reference myCollection}" IsRunning="{Binding IsLoading}"/>
</StackLayout>
问题是,每当我点击这个页面时,它都会冻结,直到加载了图库,然后直接跳到图库。我希望它显示画廊(空)与活动指示器,直到画廊加载,而不是冻结
我做错了什么
编辑:根据要求,库页面代码:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count > 0)
{
var item = (GalleryListEntry)e.CurrentSelection.FirstOrDefault();
((CollectionView)sender).SelectedItem = null;
Navigation.PushModalAsync(new Gallery(item.PCode, item.GCode));
}
}
画廊.Xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:GalShare.ViewModel"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="GalShare.Views.Gallery">
<StackLayout>
<CollectionView ItemsSource="{Binding Galleries}" x:Name="myCollection" SelectionMode="Single" SelectionChanged="CollectionView_SelectionChanged">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ThumbUrl}"
Aspect="AspectFit" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<ActivityIndicator BindingContext="{x:Reference myCollection}" IsRunning="{Binding IsLoading}"/>
</StackLayout>
}
GalleryViewModel.cs
using GalShare.Model;
using GalShare.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace GalShare.ViewModel
{
class GalleryViewModel
{
public string pCode { get; set; }
public string gCode { get; set; }
public ObservableCollection<picdata> Galleries { get; set; }
public GalleryViewModel(string pCode, string gCode)
{
this.pCode = pCode;
this.gCode = gCode;
Galleries = new GalleryService().GetImageList(pCode,gCode);
}
}
}
使用GalShare.Model;
使用GalShare.Service;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统文本;
命名空间GalShare.ViewModel
{
类GalleryViewModel
{
公共字符串pCode{get;set;}
公共字符串gCode{get;set;}
公共可观测集合库{get;set;}
公共GalleryViewModel(字符串pCode、字符串gCode)
{
this.pCode=pCode;
this.gCode=gCode;
Galleries=new GalleryService().GetImageList(pCode,gCode);
}
}
}
GalleryService.cs
using GalShare.Model;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Text;
namespace GalShare.Service
{
public class JsonGalListTxt
{
public string Galurl { get; set; }
}
public class JsonTxt
{
public Settings Settings { get; set; }
public IList<File> Files { get; set; }
}
public class Settings
{
public string Path { get; set; }
}
public class File
{
public string file { get; set; }
}
public class galURL
{
public string galurl { get; set; }
}
class GalleryService
{
// public string pCode { get; set; }
// public string gCode { get; set; }
public ObservableCollection<picdata> Images { get; set; }
public ObservableCollection<picdata> GetImageList(string pCode, string gCode)
{
WebClient client = new WebClient();
string GalUrl = client.DownloadString("https://www.mypage.it/getUrl.php?pid=" + pCode + "&galid=" + gCode);
var deserializedUrl = JsonConvert.DeserializeObject<galURL>(GalUrl);
Images = new ObservableCollection<picdata>();
string downloadString = client.DownloadString(deserializedUrl.galurl);
var deserialized = JsonConvert.DeserializeObject<JsonTxt>(downloadString);
foreach (File img in deserialized.Files)
{
Images.Add(new picdata()
{
ImageName = img.file,
BaseUrl = deserialized.Settings.Path.ToString(),
ThumbUrl = deserialized.Settings.Path.ToString() + "/thumbs" + img.file
});
}
return Images;
}
}
}
使用GalShare.Model;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
Net系统;
使用系统文本;
名称空间GalShare.Service
{
公共类JSONGALLISTXT
{
公共字符串Galurl{get;set;}
}
公共类JsonTxt
{
公共设置{get;set;}
公共IList文件{get;set;}
}
公共类设置
{
公共字符串路径{get;set;}
}
公共类文件
{
公共字符串文件{get;set;}
}
公共类galur
{
公共字符串galurl{get;set;}
}
高级厨房服务
{
//公共字符串pCode{get;set;}
//公共字符串gCode{get;set;}
公共可见收集映像{get;set;}
公共ObservableCollection GetImageList(字符串pCode、字符串gCode)
{
WebClient客户端=新的WebClient();
string GalUrl=client.DownloadString(“https://www.mypage.it/getUrl.php?pid=“+pCode+”&galid=“+gCode”);
var deserializedUrl=JsonConvert.DeserializeObject(GalUrl);
图像=新的ObservableCollection();
string downloadString=client.downloadString(反序列化durl.galurl);
var deserialized=JsonConvert.DeserializeObject(下载字符串);
foreach(反序列化.Files中的文件img)
{
Images.Add(新的picdata()
{
ImageName=img.file,
BaseUrl=反序列化的.Settings.Path.ToString(),
ThumbUrl=反序列化的.Settings.Path.ToString()+“/thumbs”+img.file
});
}
返回图像;
}
}
}
当您从构造函数创建VM时,它正在加载主线程上的所有图像,阻止页面在完成之前实际显示
public Gallery(string photographerCode, string galleryCode)
{
InitializeComponent();
BindingContext = new GalleryViewModel(photographerCode, galleryCode);
}
至少,我会将其移动到出现时的
,这样页面将首先显示,然后加载图像
您可能还希望将图像请求移动到另一个线程,这样它就不会阻塞UI。您还可以使用类似的库来帮助您管理这些内容您需要显示库的代码page@Jason谢谢,用请求的页面更新了帖子。谢谢,我只是想把这两行都放到出现的
中,但没有任何变化。。页面仍然冻结,只有在页面完全加载后才能转到下一页尝试注释该行如果我注释bindingcontext行,则其速度很快。。(没有图片)实际上我不在乎加载是否需要很长时间。。但是我需要通知用户,所以它至少应该改变页面,并显示旋转的东西。。如何将此加载放在不同的线程上?我试图将其放在新任务上,但这样做根本不起任何作用:protectedoverride void OnAppearing(){base.OnAppearing();task.Run(()=>{BindingContext=new GalleryViewModel(pCode,gCode,gUrl);}
public Gallery(string photographerCode, string galleryCode)
{
InitializeComponent();
BindingContext = new GalleryViewModel(photographerCode, galleryCode);
}