Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popup message box in xamarin.form

What I am trying to do is similar to DisplayAlert, popup a display page that contains image, content and a small close button at top right. The display page shouldn't cover the whole phone but just around 80% of the phone UI, background remain as parent page.

I am trying to play around with PushModalAsync and PopModalAsync, however with no luck. The output is not what I expected.

Basically, I have a listview, whenever item is selected from screen it will call for popUpMethod:

 list.ItemSelected += MyMethod;

inside MyMethod i will call popUpPage

async void MyMethod(object sender, SelectedItemChangedEventArgs e){
Content = await PopUpPage();
}

and this is my PopUpPage method

private async Task<StackLayout> PopUpPage()
{
     StackLayout objPopUp = new StackLayout() { HeightRequest = 100, WidthRequest= 100, VerticalOptions = LayoutOptions.CenterAndExpand};

     Label lblMessage = new Label();
     lblMessage.Text = "Welcome";

     objPopUp.Children.Add(lblMessage);

     return objPopUp;
}

I am trying to set the height and width inside my popup page. However, it is still covering the whole screen which is not what I want. let me know if any top up information is needed, thank you.

P/S : i designed it in xamarin.Form(portable)

like image 544
Low Chee Mun Avatar asked Apr 20 '15 04:04

Low Chee Mun


1 Answers

You can create a custom pop-up to accomplish this in Xamarin.Forms

Here is a custom ContentView that I created. It uses a BoxView to give the appearance of fading the background, and uses a Frame to add a shadow to the pop-up.

I also use animations to make the custom pop-up appear as if it is springing off of the screen!

Sample App

The code for this sample app is available on Github:

https://github.com/brminnick/InvestmentDataSampleApp

Custom Pop-up In Xamarin.Forms

Code Snippet

public class WelcomeView : ContentView
{
    readonly BoxView _backgroundOverlayBoxView;
    readonly Frame _overlayFrame;
    readonly StackLayout _textAndButtonStack;

    readonly RelativeLayout _relativeLayout;

    public WelcomeView()
    {
        const string titleText = "Welcome";
        const string bodyText = "Enjoy InvestmentDataSampleApp";
        const string okButtonText = "Ok, thanks!";

        var whiteWith75Opacity = new Color(255, 255, 255, 0.75);
        _backgroundOverlayBoxView = new BoxView
        {
            BackgroundColor = whiteWith75Opacity
        };
        _backgroundOverlayBoxView.Opacity = 0;

        _overlayFrame = new Frame
        {
            HasShadow = true,
            BackgroundColor = Color.White
        };
        _overlayFrame.Scale = 0;

        var titleLabel = new Label
        {
            FontAttributes = FontAttributes.Bold,
            Text = titleText,
            HorizontalTextAlignment = TextAlignment.Center
        };

        var bodyLabel = new Label
        {
            Text = bodyText,
            HorizontalTextAlignment = TextAlignment.Center
        };

        var blackWith75PercentOpacity = new Color(0, 0, 0, 0.75);
        var okButton = new Button
        {
            BackgroundColor = blackWith75PercentOpacity,
            TextColor = Color.White,
            BorderWidth = 1,
            BorderColor = blackWith75PercentOpacity,
            FontAttributes = FontAttributes.Bold,
            Margin = new Thickness(5),
            Text = okButtonText
        };
        okButton.Clicked += (sender, e) => 
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                await this.FadeTo(0);
                this.IsVisible = false;
                this.InputTransparent = true;
            });
        }

        _textAndButtonStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Spacing = 20,
            Children = {
                titleLabel,
                bodyLabel,
                okButton
            }
        };
        _textAndButtonStack.Scale = 0;

        _relativeLayout = new RelativeLayout();
        Func<RelativeLayout, double> gettextAndButtonStackHeight = (p) => _textAndButtonStack.Measure(_relativeLayout.Width, _relativeLayout.Height).Request.Height;
        Func<RelativeLayout, double> gettextAndButtonStackWidth = (p) => _textAndButtonStack.Measure(_relativeLayout.Width, _relativeLayout.Height).Request.Width;


        _relativeLayout.Children.Add(_backgroundOverlayBoxView,
            Constraint.Constant(-10),
            Constraint.Constant(0),
            Constraint.RelativeToParent(parent => parent.Width + 20),
            Constraint.RelativeToParent(parent => parent.Height)
        );
        _relativeLayout.Children.Add(_overlayFrame,
            Constraint.RelativeToParent(parent => parent.Width / 2 - gettextAndButtonStackWidth(parent) / 2 - 20),
            Constraint.RelativeToParent(parent => parent.Height / 2 - gettextAndButtonStackHeight(parent) / 2 - 10),
            Constraint.RelativeToParent(parent => gettextAndButtonStackWidth(parent) + 30),
            Constraint.RelativeToParent(parent => gettextAndButtonStackHeight(parent) + 30)
        );

        _relativeLayout.Children.Add(_textAndButtonStack,
             Constraint.RelativeToView(_overlayFrame, (parent, view) => view.X + 15),
             Constraint.RelativeToView(_overlayFrame, (parent, view) => view.Y + 15)
        );

        if (Device.OS == TargetPlatform.Android)
        {
            _overlayFrame.IsVisible = false;
            _textAndButtonStack.BackgroundColor = whiteWith90Opacity;
        }

        Content = _relativeLayout;
    }

    public void DisplayView()
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            var animationList = new List<Task>
            {
                _backgroundOverlayBoxView.FadeTo(1,AnimationConstants.WelcomeViewAnimationTime),
                _textAndButtonStack.ScaleTo(AnimationConstants.WelcomeViewMaxSize, AnimationConstants.WelcomeViewAnimationTime),
                _overlayFrame.ScaleTo(AnimationConstants.WelcomeViewMaxSize,AnimationConstants.WelcomeViewAnimationTime)
            };
            await Task.WhenAll(animationList);

            animationList = new List<Task>
            {
                _textAndButtonStack.ScaleTo(AnimationConstants.WelcomeViewNormalSize, AnimationConstants.WelcomeViewAnimationTime),
                _overlayFrame.ScaleTo(AnimationConstants.WelcomeViewNormalSize, AnimationConstants.WelcomeViewAnimationTime)
            };
            await Task.WhenAll(animationList);
        });
    }
}
like image 182
Brandon Minnick Avatar answered Sep 21 '22 09:09

Brandon Minnick