【问题标题】:Message box in UWP not workingUWP 中的消息框不起作用
【发布时间】:2017-10-04 12:57:45
【问题描述】:

请帮帮我,我遇到了问题。我是 UWP 编码的新手 问题:我构建了一个由按钮组成的简单应用程序。按下按钮时会显示一条消息

错误:在模块 CommonLanguageRuntimeLibrary 中找不到类型 System.Collections.CollectionBase

这是我的代码

using System;    
using System.Collections.Generic;   
using System.IO;   
using System.Linq;   
using System.Runtime.InteropServices.WindowsRuntime;  
using Windows.Foundation;  
using Windows.Foundation.Collections;  
using Windows.UI.Xaml;  
using Windows.UI.Xaml.Controls;  
using Windows.UI.Xaml.Controls.Primitives;  
using Windows.UI.Xaml.Data;  
using Windows.UI.Xaml.Input;  
using Windows.UI.Xaml.Media;  
using Windows.UI.Xaml.Navigation;  
using System.Windows.MessageBox;  
using System.Windows.Forms;  

// The Blank Page item template is documented at     https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1  
{   
/// <summary>   
/// An empty page that can be used on its own or navigated to within a Frame.   
/// </summary>   
public sealed partial class MainPage : Page  
{    
    public MainPage()   
    {    
        this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("HI");
    }
}
}

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    UWP 中没有MessageBox,但有MessageDialog

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Popups;
    using System;
    
    namespace App1
    {
        /// <summary>   
        /// An empty page that can be used on its own or navigated to within a Frame.   
        /// </summary>   
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                var dialog = new MessageDialog("Hi!");
                await dialog.ShowAsync();
            }
        }
    }
    

    【讨论】:

    • mm8 这对我来说没用。相反,我想出了新的错误 CS4036 错误:“IAsyncOperation”不包含“GetAwaiter”的定义,并且找不到接受“IAsyncOperation”类型的第一个参数的扩展方法“GetAwaiter”(是您缺少“系统”的 using 指令?)
    • 只需在顶部添加using System;。我编辑了我的答案。
    • 我建议大家使用下面的方法。它允许您轻松创建或传递新按钮!
    【解决方案2】:

    我强烈建议您使用单独的函数来显示弹出消息。

    UWP 使用命名空间 Windows.UI.Popups 而不是 System.Windows.MessageBox,因为它仅用于 Win32 或 WinForms 应用程序

    这是显示所需消息的好方法:

    // Other namespaces (essential)
    ...
    
    // Required namespaces for this process
    using Windows.UI.Popups;
    using System.Runtime.InteropServices;
    
    namespace PopupMessageApp
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            // I will only comment those that are not obvious to comprehend.
            private async void ShowMessage(string title, string content, [Optional] object[][] buttons)
            {
                MessageDialog dialog = new MessageDialog(content, title);
    
                // Sets the default cancel and default indexes to zero. (incase no buttons are passed)
                dialog.CancelCommandIndex   = 0;
                dialog.DefaultCommandIndex  = 0;
    
                // If the optional buttons array is not empty or null.
                if (buttons != null)
                {
                    // If there's multiple buttons
                    if (buttons.Length > 1)
                    {
                        // Loops through the given buttons array
                        for (Int32 i = 0; i < buttons.Length; i++)
                        {
                            /* Assigns text and handler variables from the current index subarray.
                             * The first object at the currentindex should be a string and 
                             * the second object should be a "UICommandInvokedHandler" 
                             */
                            string text = (string)buttons[i][0];
    
                            UICommandInvokedHandler handler = (UICommandInvokedHandler)buttons[i][1];
    
                            /* Checks whether both variables types actually are relevant and correct.
                             * If not, it will return and terminate this function and not display anything.
                             */
                            if (handler.GetType().Equals(typeof(UICommandInvokedHandler)) &&
                                text.GetType().Equals(typeof(string)))
                            {
                                /* Creates a new "UICommand" instance which is required for
                                 * adding multiple buttons.
                                 */
                                UICommand button = new UICommand(text, handler);
    
                                // Simply adds the newly created button to the dialog
                                dialog.Commands.Add(button);
                            }
                            else return;
                        }
                    }
                    else
                    {
                        // Already described
                        string text = (string)buttons[0][0];
    
                        UICommandInvokedHandler handler = (UICommandInvokedHandler)buttons[0][1];
    
                        // Already described
                        if (handler.GetType().Equals(typeof(UICommandInvokedHandler)) &&
                            text.GetType().Equals(typeof(string)))
                        {
                            // Already described
                            UICommand button = new UICommand(text, handler);
    
                            // Already described
                            dialog.Commands.Add(button);
                        }
                        else return;
                    }
    
                    /* Sets the default command index to the length of the button array.
                     * The first, colored button will become the default button or index.
                     */
                    dialog.DefaultCommandIndex = (UInt32)buttons.Length;
                }
    
                await dialog.ShowAsync();
            }
    
            private async void MainPage_Load(object sender, EventArgs e)
            {
                /* Single object arrays with a string object and a "UICommandInvokedHandler" handler.
                 * The ShowMessage function will only use the first and second index of these arrays.
                 * Replace the "return" statement with a function or whatever you desire.
                 * (The "return" statement will just return and do nothing (obviously))
                 * (Edit:  Changed 'e' to 'h' in UICommandInvokedHandler's)
                 */
                object[] button_one = { "Yes", new UICommandInvokedHandler((h) => { return; }) };
                object[] button_two = { "No", new UICommandInvokedHandler((h) => { return; }) };
    
                /* Object arrays within an object array.
                 * The first index in this array will become the first button in the following message.
                 * The first button will also get a different color and will become the default index.
                 * For instance, if you press on the "enter" key, it will press on the first button.
                 * You can add as many buttons as the "Windows.UI.Popups.MessageDialog" wants you to.
                 */
                object[][] buttons = new object[][]
                {
                    button_one,
                    button_two
                };
    
                // Displays a popup message with multiple buttons
                ShowMessage("Title", "Content here", buttons);
    
                /* Displays a popup message without multiple buttons.
                 * The last argument of the ShowMessage function is optional.
                 * because of the definition of the namespace "System.Runtime.InteropServices".
                 */
                ShowMessage("Title", "Content here");
    
                // PS, I have a life, just trying to get points xD // BluDay
            }
        }
    }
    

    这样,您可以显示一个消息对话框,无论是否传递带有包含按钮的子数组的对象数组。这种方法非常有效。如果你喜欢这个,请确保你完全理解它!

    【讨论】:

    • 这是一个很棒的解决方案,感谢提供!我做了一个更正,因为按钮声明中存在命名重叠('e' 已在 MainPage_Load 参数中使用,因此将其更改为 'h')。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2019-01-18
    相关资源
    最近更新 更多