C# UWP中的消息框不工作
请帮帮我,我遇到了一个问题。我是UWP编码的新手 问题:我构建了一个简单的应用程序,其中包含一个按钮。按下按钮时,将显示一条消息 错误:在模块CommonLanguageUntimeLibrary中找不到typre System.Collections.CollectionBase 这是我的密码C# UWP中的消息框不工作,c#,uwp,C#,Uwp,请帮帮我,我遇到了一个问题。我是UWP编码的新手 问题:我构建了一个简单的应用程序,其中包含一个按钮。按下按钮时,将显示一条消息 错误:在模块CommonLanguageUntimeLibrary中找不到typre System.Collections.CollectionBase 这是我的密码 using System; using System.Collections.Generic; using System.IO; using System.Linq; usin
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");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用System.Windows.MessageBox;
使用System.Windows.Forms;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间App1
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
MessageBox.Show(“HI”);
}
}
}
在UWP中没有消息框
,但有一个消息对话框
:
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();
}
}
}
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Popups;
使用制度;
名称空间App1
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
var dialog=newmessagedialog(“嗨!”);
wait dialog.ShowAsync();
}
}
}
我强烈建议您使用单独的功能来显示弹出消息
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" statemnet will just return and do nothing (obviously))
*/
object[] button_one = { "Yes", new UICommandInvokedHandler((e) => { return; }) };
object[] button_two = { "No", new UICommandInvokedHandler((e) => { 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
}
}
}
//其他名称空间(基本)
...
//此进程所需的命名空间
使用Windows.UI.Popups;
使用System.Runtime.InteropServices;
命名空间PopupMessageApp
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
//我只评论那些不容易理解的东西。
私有异步void ShowMessage(字符串标题、字符串内容、[可选]对象[][]按钮)
{
MessageDialog=新建MessageDialog(内容、标题);
//将默认取消和默认索引设置为零。(如果未传递任何按钮)
dialog.CancelCommandIndex=0;
dialog.DefaultCommandIndex=0;
//如果可选按钮数组不为空或null。
如果(按钮!=null)
{
//如果有多个按钮
如果(按钮长度>1)
{
//循环给定的按钮数组
对于(Int32 i=0;i