Xamarin Forms Microcharts 生成 "OutOfMemoryError"

标签 xamarin xamarin.forms

我正在尝试在我的应用程序中使用 Microcharts 图表,该应用程序使用 Xamarin.Forms(针对 Android、iOS 和 UWP)。

我尝试按照几个教程来显示图表,但每次都会导致错误:

Unhandled Exception: Java.Lang.OutOfMemoryError: Failed to allocate a 240048012 byte allocation with 5713730 free bytes and 87MB until OOM

如果我创建一个新的 Xamarin.Forms 项目,则不会发生此错误,并且运行绝对正常(我在同一台 Android 设备 Samsung SM-J320FN 上运行)。

下面是简化的 XAML 代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Foo.DetailPage" xmlns:forms="clr-namespace:Microcharts.Forms;assembly=Microcharts.Forms"> <ContentPage.Content>

<StackLayout Grid.Row="2"> <forms:ChartView x:Name="priceChart" HeightRequest="150"/> </StackLayout>

这是背后的代码:

//Temp data for charts List<Entry> entries = new List<Entry> { new Entry(200) { Color=SKColor.Parse("#FF1943"), Label ="January", ValueLabel = "200" }, new Entry(400) { Color = SKColor.Parse("00BFFF"), Label = "March", ValueLabel = "400" }, new Entry(-100) { Color = SKColor.Parse("#00CED1"), Label = "Octobar", ValueLabel = "-100" }, };

public DetailPage(string Code)
{
     ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.FromHex("27b286");

    InitializeComponent();

    priceChart.Chart = new LineChart() { Entries = entries, BackgroundColor = SKColor.Parse("#00FFFFFF") };
}

如果没有此图表,页面可以绝对正常运行,即使生成列表并且图表包含在 XAML 中,似乎是在通过后面的代码初始化图表时导致了问题。

最佳答案

我添加了<application android:hardwareAccelerated="true" android:largeHeap="true"></application>添加到我的 AndroidManifest.xml 中,这工作正常,但是我仍然不知道是什么导致使用了这么多内存。

关于Xamarin Forms Microcharts 生成 "OutOfMemoryError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51495505/

相关文章:

xamarin - 如果用户按 xamarin 表单中的硬件后退按钮,如何取消订阅 MessagingCenter?

powershell - 在安装在 Mac 上的 VSTS 代理中运行 powershell 构建步骤?

ios - 如何转换此代码以检测相机焦点?

xamarin - 将 Xamarin 按钮设置为其文本的大小,但不要变大

c# - 如何访问属性内部的属性?

Xamarin Forms 导航堆栈

xamarin.forms - 如何访问嵌入在自定义控件中的用户修改值?

xamarin - firebase 是否支持 Xamarin.iOS

c# - 如何使用 OnPlatform Xamarin 更改 BackgroundColor

c# - xamarin 与 Microsoft.EntityFrameworkCore.Sqlite 冲突