Xamarin.Forms Android 应用程序抛出内存不足错误 (XAML)
Xamarin.Forms Android Application Throws OutofMemory Error (XAML)
我是 Xamarin.Forms(2 周)的新手,所以我 运行 遇到了许多错误、警告等...
现在我正在尝试 运行 一个只有一页的应用程序,但有 5 张图像被用作按钮的背景,仅此而已。
但每次我 运行 我的 phone 或模拟器上的 android 应用程序(Windows 应用程序 运行 很好),我得到"OutofMemory Error"...我迷路了,我什至没有那么多代码...
如果有人能告诉我我做错了什么或给我解决问题的线索,我将不胜感激!!
MainPage.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:local="clr-namespace:PAEC"
x:Class="PAEC.MainPage">
<Grid >
<Image Source="background.png" Aspect="AspectFill"/>
<StackLayout VerticalOptions="Center" HorizontalOptions="Fill">
<Button
Image="Circle_Green.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Red.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Blue.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Grey.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Orange.png"
BackgroundColor="Transparent"
/>
</StackLayout>
</Grid>
</ContentPage>
MainPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PAEC
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
Output
Android 在显示时将所有图像转换为原始位图,因此会占用大量内存。 Windows 和 iOS 不会这样做,这就是它在这些平台上运行良好的原因。
为了解决这个问题,您需要确保您的图像显示的尺寸正确。如果您有一张 1000x1000 的图像,那么它将为整个图像大小创建一个原始位图,即使它在屏幕上仅显示为 48x48。
另一件事是进入 Android 项目的属性,将 Java 最大堆大小设置为 1G
我是 Xamarin.Forms(2 周)的新手,所以我 运行 遇到了许多错误、警告等...
现在我正在尝试 运行 一个只有一页的应用程序,但有 5 张图像被用作按钮的背景,仅此而已。
但每次我 运行 我的 phone 或模拟器上的 android 应用程序(Windows 应用程序 运行 很好),我得到"OutofMemory Error"...我迷路了,我什至没有那么多代码...
如果有人能告诉我我做错了什么或给我解决问题的线索,我将不胜感激!!
MainPage.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:local="clr-namespace:PAEC"
x:Class="PAEC.MainPage">
<Grid >
<Image Source="background.png" Aspect="AspectFill"/>
<StackLayout VerticalOptions="Center" HorizontalOptions="Fill">
<Button
Image="Circle_Green.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Red.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Blue.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Grey.png"
BackgroundColor="Transparent"
/>
<Button
Image="Circle_Orange.png"
BackgroundColor="Transparent"
/>
</StackLayout>
</Grid>
</ContentPage>
MainPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PAEC
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
Output
Android 在显示时将所有图像转换为原始位图,因此会占用大量内存。 Windows 和 iOS 不会这样做,这就是它在这些平台上运行良好的原因。
为了解决这个问题,您需要确保您的图像显示的尺寸正确。如果您有一张 1000x1000 的图像,那么它将为整个图像大小创建一个原始位图,即使它在屏幕上仅显示为 48x48。
另一件事是进入 Android 项目的属性,将 Java 最大堆大小设置为 1G