我正在尝试在我的应用程序中使用 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/