Xamarin.Forms - Screen getting blanked out

gagrim808 26 Reputation points
2022-01-16T14:05:01.253+00:00

I am working on an app that has a browsing, back and delete function :-
(App.Xaml.cs)

public partial class App : Application
{
        async void Browsing(object sender)
        {
            Browser.Children.Clear();

            FocusedItem = "";

            CurrentFolder.Add((sender as Button).Text);
            DisplayContents = FolderFunctionality.GetContent(CurrentFolder.ToArray()).ToArray();

            foreach (string a in DisplayContents)
            {
                Button button = new Button { ContentLayout = new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Left, 5) };
                button.Text = a.Substring(a.LastIndexOf('\f') + 1);
                button.ImageSource = ImageSource.FromResource("App.Resources.Devices." + a[0] + ".png", typeof(App));
                if (a[0] != 'F') button.StyleId = a.Remove(a.LastIndexOf("\f"));

                if (a[0] == 'F') button.Clicked += (snd, arg) =>
                {
                    bool Focus = (snd as Button).MinimumWidthRequest != 1;
                    Delays.Cancel();
                    Delays.Dispose();
                    Delays = new CancellationTokenSource();

                    if (Focus)
                    {
                        FocusedItem = (snd as Button).Text;
                        Task.Run(async () =>
                        {
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }
                            (snd as Button).MinimumWidthRequest = 0;
                        });
                        (snd as Button).MinimumWidthRequest = 1;
                    }
                    else Browsing(snd);
                };

                Browser.Children.Add(button);
            }

            if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here" });

            maincontent.Content = new ScrollView { Content = Browser };
            topbar.Content = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { backbutton }, Padding = 4 };
            MainPage = new ContentPage                                                              // Main Page
            {
                Opacity = 0,
                Content = new StackLayout
                {
                    Spacing = 0,
                    Children =
                    {
                        topbar,
                        maincontent,
                        ChangeOptions
                    }
                }
            };

            while (MainPage.Opacity < 1)
            {
                MainPage.Opacity += 0.04;
                await Task.Delay(1);
            }
        }

async void Delete(object sender, EventArgs args)
        {
            if (FocusedItem != "" && FolderFunctionality.FolderData.Count > 0)
            {
                if (!await MainPage.DisplayAlert("CONFIRMATION", "Are you sure you want to delete \"" + FocusedItem + "\"?", "Yes", "No")) return;

                FolderFunctionality.DeleteItem(CurrentFolder.ToArray(), FocusedItem);

                CurrentFolder.Add("\fDelete");
                Back(null, null);
            }
        }


        async void Back(object sender, EventArgs args)
        {
            Browser.Children.Clear();
            FocusedItem = "";

            CurrentFolder.RemoveAt(CurrentFolder.Count - 1);
            CurrentFolder.TrimExcess();

            DisplayContents = FolderFunctionality.GetContent(CurrentFolder.ToArray()).ToArray();

            foreach (string a in DisplayContents)
            {
                Button button = new Button { ContentLayout = new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Left, 5) };
                button.Text = a.Substring(a.LastIndexOf('\f') + 1);
                button.ImageSource = ImageSource.FromResource("App.Resources.Devices." + a[0] + ".png", typeof(App));
                if (a[0] != 'F') button.StyleId = a.Remove(a.LastIndexOf("\f"));

                if (a[0] == 'F') button.Clicked += (snd, arg) =>
                {
                    bool Focus = (snd as Button).MinimumWidthRequest != 1;
                    Delays.Cancel();
                    Delays.Dispose();
                    Delays = new CancellationTokenSource();

                    if (Focus)
                    {
                        FocusedItem = (snd as Button).Text;
                        Task.Run(async () =>
                        {
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }
                            (snd as Button).MinimumWidthRequest = 0;
                        });
                        (snd as Button).MinimumWidthRequest = 1;
                    }
                    else Browsing(snd);
                };

                Browser.Children.Add(button);
            }

            if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here" });

            maincontent.Content = new ScrollView { Content = Browser };

            if (CurrentFolder.Count < 1) topbar.Content = new StackLayout
                {
                    Padding = new Thickness(8, 4),
                    Spacing = 0,
                    BackgroundColor = Color.Transparent,
                    Orientation = StackOrientation.Horizontal,
                    Children =
                    {
                        accountbutton,
                        infobutton
                    }
                };
            else topbar.Content = new StackLayout
                {
                    Padding = new Thickness(8, 4),
                    Spacing = 0,
                    BackgroundColor = Color.Transparent,
                    Orientation = StackOrientation.Horizontal,
                    Children =
                    {
                        backbutton
                    }
                };

            MainPage = new ContentPage                                                              // Main Page
            {
                Opacity = 0,
                Content = new StackLayout
                {
                    Spacing = 0,
                    Children =
                    {
                        topbar,
                        maincontent,
                        ChangeOptions
                    }
                }
            };

            while (MainPage.Opacity < 1)
            {
                MainPage.Opacity += 0.04;
                await Task.Delay(1);
            }
        }
        private async override void OnStart(object sender, EventArgs args)                                  // Main Page of the application
        {
            DisplayContents = FolderFunctionality.GetContent(CurrentFolder.ToArray()).ToArray();

            foreach (string a in DisplayContents)
            {
                Button button = new Button { ContentLayout = new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Left, 5) };
                button.Text = a.Substring(a.LastIndexOf('\f') + 1);
                button.ImageSource = ImageSource.FromResource("App.Resources.Devices." + a[0] + ".png", typeof(App));
                if (a[0] != 'F') button.StyleId = a.Remove(a.LastIndexOf("\f"));

                if (a[0] == 'F') button.Clicked += (snd, arg) =>
                {
                    bool Focus = (snd as Button).MinimumWidthRequest != 1;
                    Delays.Cancel();
                    Delays.Dispose();
                    Delays = new CancellationTokenSource();

                    if (Focus)
                    {
                        FocusedItem = (snd as Button).Text;
                        Task.Run(async () =>
                        {
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }
                            (snd as Button).MinimumWidthRequest = 0;
                        });
                        (snd as Button).MinimumWidthRequest = 1;
                    }
                    else Browsing(snd);
                };

                Browser.Children.Add(button);
            }

            if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here" });

            maincontent.Content = new ScrollView { Content = Browser };
            topbar.Content = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { accountbutton, infobutton }, Padding = 4 };
            MainPage = new ContentPage                                                              // Main Page
            {
                Opacity = 0,
                Content = new StackLayout
                {
                    Spacing = 0,
                    Children =
                    {
                        topbar,
                        maincontent,
                        ChangeOptions
                    }
                }
            };

            while (MainPage.Opacity < 1)
            {
                MainPage.Opacity += 0.04;
                await Task.Delay(1);
            }
        }
    }

So, in this, when I click on the buttons that have the clicked event as Browsing, the Browsing function runs but the screen goes blank
(probably, the Opacity is 0). Also, when I click the delete button, the delete function runs but the same thing happens.
when I exit and resume the app, the page is properly visible.

And when I run the Back function via a button, the results are expected.

So, what causes this? Please help me.

I also get Opengl[RippleDrawable] EndAllActiveAnimators message sometimes in the console log.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,191 questions
{count} votes

1 answer

Sort by: Most helpful
  1. gagrim808 26 Reputation points
    2022-01-25T13:50:34.977+00:00

    Sorry for the late response, but I figured it out myself by changing the MinimumWidthRequest to AnchorX.

    1 person found this answer helpful.