Xamarin Forms - 线程效率

标签 xamarin error-handling crash xamarin.android xamarin.forms

我正在开发一个 Xamarin (PCL) 跨平台应用程序,目前正在我的 Android 物理设备和模拟器上进行测试。该应用程序在高端手机上运行完美,但在三星S4等低端手机上“意外崩溃”。仅在我执行多个事件和任务后它才会崩溃。

我假设这与这些手机的任务线程容量有关。如果我在这方面是正确的,我应该如何使我的应用程序能够在所有手机上顺利运行且无错误?

编辑 - 错误显示“应用程序已意外关闭”。该错误不会出现在特定行上,因为它与代码无关。仅当执行许多事件时,它才会在某些“旧”手机上出现故障。事件示例包括:将数据添加到数据库、更新/删除数据、从一个事件移动到另一个事件、显示用户输入数据的日历和饼图。

编辑2 - 一些处理图像的代码:

Xaml:

<ScrollView HorizontalOptions="Fill" Orientation="Horizontal">
                                <StackLayout Orientation="Vertical" BackgroundColor="GhostWhite" >
                                    <StackLayout Orientation="Horizontal" BackgroundColor="GhostWhite" >
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="dairy" Image="{Binding Dairy_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="alcohol" Image="{Binding Alcohol_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="eggs" Image="{Binding Egg_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fastfood" Image="{Binding Fastfood_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fish" Image="{Binding Fish_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fruit" Image="{Binding Fruit_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="grain" Image="{Binding Grain_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="legume" Image="{Binding Legume_Image}"/>
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal" BackgroundColor="GhostWhite" >
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="meat" Image="{Binding Meat_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="munchies" Image="{Binding Munchies_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="nuts" Image="{Binding Nut_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="potato" Image="{Binding Potato_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="soda" Image="{Binding Soda_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="sweets" Image="{Binding Sweet_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="vegetables" Image="{Binding Vegetable_Image}"/>
                                    </StackLayout>
                                </StackLayout>

.CS:

 public UserMealINC_vm(User_Profiles up, DateTime day)
        {
            try
            {
                Day = day;
                User_pro = up;
                Bool_Food_Type = false;
                Food_Name = "";
                Type = "";
                Food_Weight = "0";
                Selected_Food = new List<string>();

                //All meal item are first initialized with the non-coloured images
                Dairy_Image = "drawable/dairy.png";
                Alcohol_Image = "drawable/alcohol.png";
                Egg_Image = "drawable/eggs.png";
                Fastfood_Image = "drawable/fastfood.png";
                Fish_Image = "drawable/fish.png";
                Fruit_Image = "drawable/fruit.png";
                Grain_Image = "drawable/grain.png";
                Legume_Image = "drawable/legume.png";
                Meat_Image = "drawable/meat.png";
                Munchies_Image = "drawable/munchies.png";
                Nut_Image = "drawable/nuts.png";
                Potato_Image = "drawable/potato.png";
                Soda_Image = "drawable/soda.png";
                Sweet_Image = "drawable/sweets.png";
                Vegetable_Image = "drawable/vegetables.png";

                this.Button_Clicked_Food = new Command<string>((key) =>
                {
                    //Change the item selected from color to non-color, or vice-versa

                    if (Selected_Food.Contains(key))
                    {
                        if (key == "dairy")
                        {
                            Dairy_Image = "drawable/dairy.png";
                        }
                        else if (key == "alcohol")
                        {
                            Alcohol_Image = "drawable/alcohol.png";
                        }
                        else if (key == "eggs")
                        {
                            Egg_Image = "drawable/eggs.png";
                        }
                        else if (key == "fastfood")
                        {
                            Fastfood_Image = "drawable/fastfood.png";
                        }
                        else if (key == "fish")
                        {
                            Fish_Image = "drawable/fish.png";
                        }
                        else if (key == "fruit")
                        {
                            Fruit_Image = "drawable/fruit.png";
                        }
                        else if (key == "grain")
                        {
                            Grain_Image = "drawable/grain.png";
                        }
                        else if (key == "legume")
                        {
                            Legume_Image = "drawable/legume.png";
                        }
                        else if (key == "meat")
                        {
                            Meat_Image = "drawable/meat.png";
                        }
                        else if (key == "munchies")
                        {
                            Munchies_Image = "drawable/munchies.png";
                        }
                        else if (key == "nuts")
                        {
                            Nut_Image = "drawable/nuts.png";
                        }
                        else if (key == "potato")
                        {
                            Potato_Image = "drawable/potato.png";
                        }
                        else if (key == "soda")
                        {
                            Soda_Image = "drawable/soda.png";
                        }
                        else if (key == "sweets")
                        {
                            Sweet_Image = "drawable/sweets.png";
                        }
                        else if (key == "vegetables")
                        {
                            Vegetable_Image = "drawable/vegetables.png";
                        }
                        else
                        {
                            //Key out of bounds???
                        }

                        Selected_Food.Remove(key);
                    }
                    else
                    {
                        if (key == "dairy")
                        {
                            Dairy_Image = "drawable/dairy_color.png";
                        }
                        else if (key == "alcohol")
                        {
                            Alcohol_Image = "drawable/alcohol_color.png";
                        }
                        else if (key == "eggs")
                        {
                            Egg_Image = "drawable/eggs_color.png";
                        }
                        else if (key == "fastfood")
                        {
                            Fastfood_Image = "drawable/fastfood_color.png";
                        }
                        else if (key == "fish")
                        {
                            Fish_Image = "drawable/fish_color.png";
                        }
                        else if (key == "fruit")
                        {
                            Fruit_Image = "drawable/fruit_color.png";
                        }
                        else if (key == "grain")
                        {
                            Grain_Image = "drawable/grain_color.png";
                        }
                        else if (key == "legume")
                        {
                            Legume_Image = "drawable/legume_color.png";
                        }
                        else if (key == "meat")
                        {
                            Meat_Image = "drawable/meat_color.png";
                        }
                        else if (key == "munchies")
                        {
                            Munchies_Image = "drawable/munchies_color.png";
                        }
                        else if (key == "nuts")
                        {
                            Nut_Image = "drawable/nuts_color.png";
                        }
                        else if (key == "potato")
                        {
                            Potato_Image = "drawable/potato_color.png";
                        }
                        else if (key == "soda")
                        {
                            Soda_Image = "drawable/soda_color.png";
                        }
                        else if (key == "sweets")
                        {
                            Sweet_Image = "drawable/sweets_color.png";
                        }
                        else if (key == "vegetables")
                        {
                            Vegetable_Image = "drawable/vegetables_color.png";
                        }
                        else
                        {
                            //Key out of bounds???
                        }
                        Selected_Food.Add(key);
                    }
                });                
            }
            catch (Exception ex)
            {
                App.Current.MainPage.DisplayAlert("UserMealINC_vm 1!", ex.Message, "OK");
            }
        }

最佳答案

事实证明,我的应用程序中的所有多个图像导致设备内存溢出。 (根据@hvaughan3的建议)

This link has the answer to it.

您基本上只需在 Android list (在应用程序下)中添加这两行 -

android:hardwareAccelerated="false"
android:largeHeap="true"

关于Xamarin Forms - 线程效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45615722/

相关文章:

javascript - Angular 误差和超时工厂

firebase - 在Flutter中捕获云Firestore文档快照错误

Java:为什么这个列表落后于我的游戏 “Out of Memory”

ios - Xamarin.iOS 中的等效 CGPointZero 是什么?

asp.net-web-api - Xamarin.Android 上的 StringContent 为空

ios - MAUI iOS 部署 'There was an error while trying to automatically provision the project'

php - 避免 fatal error 内存在它发生之前耗尽

ios - UIAlertView 未在 Monotouch Release 版本中显示

iOS6 应用程序在后台崩溃。如何解码崩溃日志?

objective-c - 加载 View 之前,应用程序崩溃iOS> 6