I am creating an app for Android in Xamarin Forms (latest) on Visual Studio. On my Android 12.1 emulator, it works fine, whether I use Target Framework as 12, 11, or 10.
But on my actual smartphone (API level 10), and on another smartphone (API level 7), it seems to crash as soon as I run this function -
void Browsing(object sender)
{
Browser.Children.Clear();
FocusedItem = null;
CurrentFolder.Insert(0, ((sender as Frame).BindingContext as FolderFunctionality.Item).Name);
FolderFunctionality.Item Folder = FolderFunctionality.Get(CurrentFolder.GetRange(1, CurrentFolder.Count - 1), CurrentFolder[0]);
foreach (FolderFunctionality.Item a in Folder)
{
Frame button = new Frame
{
Padding = 10,
CornerRadius = 15,
HasShadow = false,
BackgroundColor = Color.LightSkyBlue,
Content = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
Padding = 0,
Spacing = 10,
Children =
{
new Image { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Start, HeightRequest = 50, WidthRequest = 50, Source = ImageSource.FromResource("App.Resources.Devices." + a.Type + ".png", typeof(App)) },
new Label { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.FillAndExpand, LineBreakMode = LineBreakMode.TailTruncation, TextColor = Color.Black, Padding = 0, FontSize = 20, Text = a.Name },
}
}
};
TapGestureRecognizer tap = new TapGestureRecognizer();
button.BindingContext = a;
if (a.Type == "F") tap.Tapped += (snd, arg) =>
{
PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;
((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;
PreviouslyFocusedItem = snd as Frame;
bool UnFocus = !(snd as Frame).HasShadow;
Delays.Cancel();
Delays.Dispose();
Delays = new CancellationTokenSource();
PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;
((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;
if (UnFocus)
{
FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;
Task.Run(async () =>
{
try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }
PreviouslyFocusedItem.HasShadow = false;
});
(snd as Frame).HasShadow = true;
}
else Browsing(snd);
};
else tap.Tapped += (snd, arg) =>
{
PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;
((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;
PreviouslyFocusedItem = snd as Frame;
bool UnFocus = !(snd as Frame).HasShadow;
Delays.Cancel();
Delays.Dispose();
Delays = new CancellationTokenSource();
PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;
((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;
if (UnFocus)
{
FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;
Task.Run(async () =>
{
try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }
PreviouslyFocusedItem.HasShadow = false;
});
(snd as Frame).HasShadow = true;
}
else DeviceController(snd);
};
button.GestureRecognizers.Add(tap);
Browser.Children.Add(button);
}
if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here", BindingContext = new FolderFunctionality.Item() });
maincontent.Content = new ScrollView { Content = Browser, HorizontalScrollBarVisibility = ScrollBarVisibility.Never };
topbar.Content = new StackLayout
{
Padding = new Thickness(8, 4),
Spacing = 0,
BackgroundColor = Color.Transparent,
Orientation = StackOrientation.Horizontal,
Children =
{
backbutton,
AppName,
new Frame { Padding = 0, WidthRequest = 62, HeightRequest = 62, BackgroundColor = Color.Transparent }
}
};
MainPage = new ContentPage // Main Page
{
Opacity = 0,
Content = new StackLayout
{
Spacing = 0,
Children =
{
topbar,
maincontent,
ChangeOptions
}
}
};
MainPage.FadeTo(1);
}
Where FolderFunctionality and FolderFunctionality.Item is a class created by me (Very less chances that it causes problems as another function using it in the same way runs as expected).
The console shows this message last -
[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7ffb9b4f40 in tid 32564 (companyname.app), pid 32564 (companyname.app)
Any ideas about the solution will be highly appreciated.