Xamarin.Forms listview and kitkat phone emulator



我使用 VS 2015 做一个简单的跨平台应用程序,其中包含 Xamarin.Forms 列表视图,并且列表视图数据绑定到对远程 API Web 服务的 C# 调用的结果。该 Web api 服务的调用仅返回大约 50 个数据行,当用户在模拟器上向下滚动列表视图时,它们将逐渐显示。我也使用安卓的VS模拟器。该应用程序可与 10.1 英寸棉花糖 6.0.0 xhdpi 平板电脑 API 级别 23 模拟器配合使用。但它在 4.5 英寸 Kitkat 4.4 hdpi 手机 api 级别 19 模拟器的向下滚动过程中死亡并关闭。

我不知道列表视图如何与手机模拟器配合使用。我是使用Xamarin.Forms UI小部件的新手。我想崩溃可能与4.5英寸Kitkat手机模拟器上的内存问题有关,但我不确定。

我在VS中使用Android设备日志记录捕获了一些崩溃日志,如下所示:

04-28 07:47:08.295 D/Mono    ( 1239): [0xb92a8220] hill climbing, change max number of threads 3
04-28 07:47:08.447 D/dalvikvm( 1239): GC_FOR_ALLOC freed 1124K, 19% free 5397K/6588K, paused 1ms, total 2ms
04-28 07:47:08.683 D/Mono    ( 1239): [0xb955cad0] hill climbing, change max number of threads 2
04-28 07:47:08.883 D/dalvikvm( 1239): GC_CONCURRENT freed 307K, 11% free 5924K/6588K, paused 0ms+0ms, total 1ms
04-28 07:47:09.999 D/Mono    ( 1239): [0xb92b3b00] hill climbing, change max number of threads 3
04-28 07:47:10.699 D/Mono    ( 1239): [0xb92a8220] hill climbing, change max number of threads 2
04-28 07:47:10.815 D/DonatelloNative_Selector(  133): Removing selector for fd 33
04-28 07:47:10.815 D/DonatelloNative_Selector(  133): Removing selector for fd 32
04-28 07:47:10.815 W/InputDispatcher(  464): channel 'b3268bf0 SchoolOfFineArt.Droid/md51c583ee22c53a99167a61678b0024deb.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
04-28 07:47:10.815 E/InputDispatcher(  464): channel 'b3268bf0 SchoolOfFineArt.Droid/md51c583ee22c53a99167a61678b0024deb.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-28 07:47:10.815 W/InputDispatcher(  464): Attempted to unregister already unregistered input channel 'b3268bf0 SchoolOfFineArt.Droid/md51c583ee22c53a99167a61678b0024deb.MainActivity (server)'
04-28 07:47:10.815 I/WindowState(  464): WIN DEATH: Window{b3268bf0 u0 SchoolOfFineArt.Droid/md51c583ee22c53a99167a61678b0024deb.MainActivity}
04-28 07:47:10.815 I/ActivityManager(  464): Process SchoolOfFineArt.Droid (pid 1239) has died.
04-28 07:47:10.815 W/ActivityManager(  464): Force removing ActivityRecord{b337aba8 u0 SchoolOfFineArt.Droid/md51c583ee22c53a99167a61678b0024deb.MainActivity t2}: app died, no saved state
04-28 07:47:10.859 D/Zygote  (  138): Process 1239 terminated by signal (11)
04-28 07:47:10.863 W/EGL_emulation(  663): eglSurfaceAttrib not implemented
04-28 07:47:10.863 W/InputMethodManagerService(  464): Got RemoteException sending setActive(false) notification to pid 1239 uid 10087
04-28 07:47:10.867 W/Binder  (  634): Caught a RuntimeException from the binder stub implementation.
04-28 07:47:10.867 W/Binder  (  634): java.lang.NullPointerException
04-28 07:47:10.867 W/Binder  (  634):   at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
04-28 07:47:10.867 W/Binder  (  634):   at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
04-28 07:47:10.867 W/Binder  (  634):   at android.os.Binder.execTransact(Binder.java:404)
04-28 07:47:10.867 W/Binder  (  634):   at dalvik.system.NativeStart.run(Native Method)

以下是我的应用程序的一些代码:

带有列表视图小部件的 Xaml 代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:school="clr-namespace:SchoolOfFineArt;assembly=SchoolOfFineArt" 
             x:Class="SchoolOfFineArt.StudentListPage">
  <ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0" />
  </ContentPage.Padding>
  <ContentPage.BindingContext>
    <school:SchoolViewModel />
  </ContentPage.BindingContext>
  <StackLayout BindingContext="{Binding StudentBody}">
    <Label Text="{Binding School}" FontSize="Large" FontAttributes="Bold"
           HorizontalTextAlignment="Center" />
    <ListView ItemsSource="{Binding Students}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ImageCell ImageSource="{Binding PhotoFilename}" 
                     Text="{Binding FullName}" 
                     Detail="{Binding GradePointAverage, StringFormat='G.P.A. = {0:F2}'}" />
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</ContentPage>

用于从 Web API 服务调用和分析 XML 结果的 C# 代码:

using System;
using System.IO;
using System.Net;
using System.Xml.Serialization;
using Xamarin.Forms;

namespace SchoolOfFineArt
{
    public class SchoolViewModel : ViewModelBase
    {
        StudentBody studentBody;
        Random rand = new Random();
        public SchoolViewModel()
        {
            string uri = "http://xamarin.github.io/xamarin-forms-book-preview-2" +
                             "/ElPasoHighSchool/students.xml";
            HttpWebRequest request = WebRequest.CreateHttp(uri);
            request.BeginGetResponse((arg) =>
            {
                // Deserialize XML file.
                Stream stream = request.EndGetResponse(arg).GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                XmlSerializer xml = new XmlSerializer(typeof(StudentBody));
                StudentBody = xml.Deserialize(reader) as StudentBody;
                // Set StudentBody property in each Student object.
                foreach (Student student in StudentBody.Students)
                {
                    student.StudentBody = StudentBody;
                }
            }, null);
            // Adjust GradePointAverage randomly.
            Device.StartTimer(TimeSpan.FromSeconds(0.1),
                () =>
                {
                    if (studentBody != null)
                    {
                        int index = rand.Next(studentBody.Students.Count);
                        Student student = studentBody.Students[index];
                        double factor = 1 + (rand.NextDouble() - 0.5) / 5;
                        student.GradePointAverage = Math.Round(
                            Math.Max(0, Math.Min(5, factor * student.GradePointAverage)), 2);
                    }
                    return true;
                });
        }
        public StudentBody StudentBody
        {
            protected set { SetProperty(ref studentBody, value); }
            get { return studentBody; }
        }
    }
}

xaml 页代码的 C# 代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace SchoolOfFineArt
{
    public partial class StudentListPage : ContentPage
    {
        public StudentListPage()
        {
            InitializeComponent();
        }
    }
}
我不知道逐渐

,但看起来它们在调用完成后一次全部创建。这两个异步操作可能在 studentBody.Students中相互踩踏:可能会添加到正在进行的枚举中。尝试仅在第一个异步操作完成后启动第二个异步操作。

最新更新