Message prompt box in UWP (1) and uwp message prompt box

Source: Internet
Author: User

Message prompt box in UWP (1) and uwp message prompt box

No matter what platform, some message boxes will inevitably appear in the application. Let's talk about the message boxes I used in UWP.

Pop-up windows can also be divided into two categories based on the logic of user operations.

One type that does not require user interference:

MessageDialog: The operation is simple and easy to write. For details, refer to MSDN to see the effect.

Results on PC:

 

Results On mobile:

Let's look at the code (● 'authorization' ●)

Front-end:

<Grid Background = "{ThemeResource layout}"> <RelativePanel HorizontalAlignment = "Center"> <TextBlock x: Name = "tbHW" Text = "Hello pop-up window"/> <TextBlock x: name = "tb" RelativePanel. below = "tbHW" Margin = "0, 10" FontSize = "20" Foreground = "# f90"/> </RelativePanel> <StackPanel verticalignment = "Bottom" Orientation = "Horizontal"> <Button Content = "MessageDialog" Click = "Button_Click"/> </StackPanel> </Grid>

Background:

Private void Button_Click (object sender, RoutedEventArgs e) {this. tb. text = ""; Button btn = sender as Button; switch (btn. content. toString () {case "MessageDialog": ShowMessageDialog (); break;} private async void ShowMessageDialog () {var msgDialog = new Windows. UI. popups. messageDialog ("I Am a prompt") {Title = "prompt Title"}; msgDialog. commands. add (new Windows. UI. popups. UICommand ("OK", uiCommand => {this. tb. text = $ "you clicked: {uiCommand. label} ";})); msgDialog. commands. add (new Windows. UI. popups. UICommand ("cancel", uiCommand => {this. tb. text = $ "you clicked: {uiCommand. label} ";})); await msgDialog. showAsync ();}

It is very simple, but it cannot control the display position, font color, size, etc. In actual application, it is difficult to persuade the UI sister paper... There are many ways to avoid this problem. Next we will introduce another implementation: Use Popup + UserControl to implement your own message prompt box.

Let's take a look at the results first.

Continue to look at the Code:

Create a new user control called MessagePopupWindow.

MessagePopupWindow. xaml

<Grid> <Border x: Name = "OutBorder" Background = "#55000000" verticalignment = "Stretch" HorizontalAlignment = "Stretch"/> <StackPanel x: name = "ContentGrid" Background = "White" Margin = "45, 45" Orientation = "Vertical" verticalignment = "Center"> <Grid Padding = "20"> <TextBlock x: name = "tbContent" Grid. row = "0" TextWrapping = "Wrap" HorizontalAlignment = "Center"/> </Grid> <Grid Padding = "15"> <Grid. columnDefinitions> <ColumnDefinition Width = "*"/> <ColumnDefinition Width = "15"/> <ColumnDefinition Width = "*"/> </Grid. columnDefinitions> <Button Grid. column = "0" x: name = "btnLeft" Content = "OK" HorizontalAlignment = "Stretch" placement = "Center" verticalignment = "Center" placement = "Center" BorderThickness = "0" Click = "LeftButton_Click" background = "# f90" Foreground = "White" MaxWidth = "150"/> <Button Grid. column = "2" x: name = "btnRight" Content = "cancel" Click = "RightButton_Click" HorizontalAlignment = "Stretch" align = "Center" verticalignment = "Center" align = "Center" BorderThickness = "0" background = "Gray" Foreground = "BlanchedAlmond" MaxWidth = "150"/> </Grid> </StackPanel> </Grid>

MessagePopupWindow. xaml. cs

 public sealed partial class MessagePopupWindow : UserControl    {        private Popup m_Popup;        private string m_TextBlockContent;        private MessagePopupWindow()        {            this.InitializeComponent();            m_Popup = new Popup();            this.Width = Window.Current.Bounds.Width;            this.Height = Window.Current.Bounds.Height;            m_Popup.Child = this;            this.Loaded += MessagePopupWindow_Loaded;            this.Unloaded += MessagePopupWindow_Unloaded;        }        public MessagePopupWindow(string showMsg) : this()        {            this.m_TextBlockContent = showMsg;        }        private void MessagePopupWindow_Loaded(object sender, RoutedEventArgs e)        {            this.tbContent.Text = m_TextBlockContent;            Window.Current.SizeChanged += MessagePopupWindow_SizeChanged; ;        }        private void MessagePopupWindow_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)        {            this.Width = e.Size.Width;            this.Height = e.Size.Height;        }        private void MessagePopupWindow_Unloaded(object sender, RoutedEventArgs e)        {            Window.Current.SizeChanged -= MessagePopupWindow_SizeChanged; ;        }        public void ShowWIndow()        {            m_Popup.IsOpen = true;        }        private void DismissWindow()        {            m_Popup.IsOpen = false;        }        private void LeftButton_Click(object sender, RoutedEventArgs e)        {            DismissWindow();            LeftClick?.Invoke(this, e);        }        private void RightButton_Click(object sender, RoutedEventArgs e)        {            DismissWindow();            RightClick?.Invoke(this, e);        }        public event EventHandler<RoutedEventArgs> LeftClick;        public event EventHandler<RoutedEventArgs> RightClick;    }

 

The MainPage. xaml changes are as follows:

MainPage. xaml. cs adds a case in Button_Click to process the Click Event of the M button,

case "Custom":                    ShowMessagePopupWindow();                    break;

The ShowMessagePopupWindow method is mainly as follows:

Private void ShowMessagePopupWindow () {var msgPopup = new Controls. messagePopupWindow ("I Am a prompt"); msgPopup. leftClick + = (s, e) => {this. tb. text = "you clicked: OK" ;}; msgPopup. rightClick + = (s, e) => {this. tb. text = "you clicked: Cancel" ;}; msgPopup. showWIndow ();}

In this way, a custom message prompt box is complete...

There is another prompt box that does not require user intervention to disappear. We will try again later.

Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.