Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

My gift / contribution to the community ==> Easy way to show a dialog box

$
0
0

I've often see how to do this but never see what I'm offering to the community (I don't pretend to have search the whole net to confirm that :))

This little class is a small peace of code to show one way to add a dialogbox based on a Page...

To use this class, you just have to call it like this:

MyDlgExtension.ShowDialog(this, new DialPage());

Where DialPage is just an xaml page containing stuff...
My approach is just a refactoring of advises about adding AbsoluteLayout to your page, and so on...
Of course, it is not perfect in the sense that I don't handle in this class the fact that many "DialogBox" could be added to one Page
(could be done easily by replacing the content add from the page to "dialog to").

It has just to be seen as a base for better things... but I don't have time to put in in github and make it grows like it should. You can reuse these few lines of code, improve, change, don't use.. it's free.. it's my very small gift to the community of Xamarin.Forms I always enjoy reading here.

public class MyDlgExtension
    {
        private static AbsoluteLayout newLayout = null;
        private static View oldLayout = null;
        private static View dialog = null;

        public static void ShowDialog(ContentPage parent, ContentPage dlg)
        {
            if (newLayout == null)
            {
                bool isStackLayout = false;

                StackLayout stlayout = null;
                AbsoluteLayout absLayout = null;

                oldLayout = parent.Content;

                if (oldLayout is StackLayout)
                {
                    stlayout = oldLayout as StackLayout;
                    isStackLayout = true;
                }
                else if (oldLayout is AbsoluteLayout)
                {
                    absLayout = oldLayout as AbsoluteLayout;
                }

                AbsoluteLayout.SetLayoutBounds(dlg.Content, new Rectangle(0, 0, 1, 1));
                AbsoluteLayout.SetLayoutFlags(dlg.Content, AbsoluteLayoutFlags.All);

                dialog = dlg.Content;

                if (isStackLayout)
                {
                    newLayout = new AbsoluteLayout();
                    AbsoluteLayout.SetLayoutBounds(stlayout, new Rectangle(0, 0, 1, 1));
                    AbsoluteLayout.SetLayoutFlags(stlayout, AbsoluteLayoutFlags.All);

                    newLayout.Children.Add(stlayout);
                    newLayout.Children.Add(dlg.Content);
                    parent.Content = newLayout;
                }
                else
                {
                    parent.Content = dlg.Content;
                    //absLayout.Children.Add(dlg.Content);
                }
            }
            else
            {
                dialog.IsVisible = true;
            }
        }

        public static void HideDialog()
        {
            if(dialog != null)
            {
                dialog.IsVisible = false;
            }
        }
    }

I'm Nicolas ETIENNE, from Toulouse, France... :)
Et oui, je suis français !!!


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>