【问题标题】:Xamarin.Forms listview and kitkat phone emulatorXamarin.Forms listview 和 kitkat 手机模拟器
【发布时间】:2016-08-23 07:34:46
【问题描述】:

我使用 VS 2015 做一个简单的跨平台应用程序,其中包含 Xamarin.Forms 列表视图,列表视图数据绑定到 C# 调用远程 API Web 服务的结果。该 web api 服务的调用只返回大约 50 个数据行,当用户在模拟器上向下滚动列表视图时,它们将逐渐显示。我还使用适用于 Android 的 VS 模拟器。该应用程序适用于 10.1" Marshmallow 6.0.0 xhdpi 平板电脑 API 级别 23 模拟器。但它在 4.5" Kitkat 4.4 hdpi 手机 API 级别 19 模拟器的向下滚动过程中死亡并自行关闭。

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

我在 VS 中使用 Android Device Logging 捕获了一些崩溃日志,如下所示:

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();
        }
    }
}

【问题讨论】:

    标签: listview android-emulator xamarin.forms


    【解决方案1】:

    我不知道逐渐,但看起来它们是在调用完成后立即创建的。这两个异步操作可能会在 studentBody.Students 中相互叠加:可能会添加到正在进行的枚举中。仅在第一个异步操作完成后尝试启动第二个异步操作。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多