Universal Apps MessageBox: "The name 'MessageBox' does not exist in the current context"
C#Windows Phone-8.1Win Universal-AppC# Problem Overview
I want to use MessageBox for showing download errors in my WP8.1 app.
I added:
using System.Windows;
but when I type:
MessageBox.Show("");
I get error:
"The name 'MessageBox' does not exist in the current context"
In Object Browser I found that such class should exist and in "Project->Add reference... ->Assemblies->Framework" is shown that all assemblies are referenced.
Do I miss something? Or is there another way how to show something like messagebox?
C# Solutions
Solution 1 - C#
For Universal Apps, the new APIs require you to use await MessageDialog().ShowAsync()
(in Windows.UI.Popups) to bring it into line with Win 8.1.
var dialog = new MessageDialog("Your message here");
await dialog.ShowAsync();
Solution 2 - C#
Just wanted to add to ZombieSheep's answer: also, customization is quite simple
var dialog = new MessageDialog("Are you sure?");
dialog.Title = "Really?";
dialog.Commands.Add(new UICommand { Label = "Ok", Id = 0 });
dialog.Commands.Add(new UICommand { Label = "Cancel", Id = 1 });
var res = await dialog.ShowAsync();
if ((int)res.Id == 0)
{ *** }
Solution 3 - C#
try this:
using Windows.UI.Popups;
code:
private async void Button_Click(object sender, RoutedEventArgs e)
{
MessageDialog msgbox = new MessageDialog("Would you like to greet the world with a \"Hello, world\"?", "My App");
msgbox.Commands.Clear();
msgbox.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
msgbox.Commands.Add(new UICommand { Label = "No", Id = 1});
msgbox.Commands.Add(new UICommand { Label = "Cancel", Id = 2 });
var res = await msgbox.ShowAsync();
if ((int)res.Id == 0)
{
MessageDialog msgbox2 = new MessageDialog("Hello to you too! :)", "User Response");
await msgbox2.ShowAsync();
}
if ((int)res.Id == 1)
{
MessageDialog msgbox2 = new MessageDialog("Oh well, too bad! :(", "User Response");
await msgbox2.ShowAsync();
}
if ((int)res.Id == 2)
{
MessageDialog msgbox2 = new MessageDialog("Nevermind then... :|", "User Response");
await msgbox2.ShowAsync();
}
}
To Trigger some Function When "Yes" or "No" is clicked, You can also use:
msgbox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.TriggerThisFunctionForYes)));
msgbox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.TriggerThisFunctionForNo)));
Solution 4 - C#
You could also make a class like the next one. Below the code a usage example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
namespace someApp.ViewModels
{
public static class Msgbox
{
static public async void Show(string mytext)
{
var dialog = new MessageDialog(mytext, "Testmessage");
await dialog.ShowAsync();
}
}
}
The example to use it in a class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace someApp.ViewModels
{
public class MyClass{
public void SomeMethod(){
Msgbox.Show("Test");
}
}
}
Solution 5 - C#
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
}
public static class Msgbox {
static public async void Show(string m) {
var dialog = new MessageDialog( m);
await dialog.ShowAsync();
}
}
private void Button_Click(object sender, RoutedEventArgs e) {
Msgbox.Show("This is a test to see if the message box work");
//Content.ToString();
}
}
Solution 6 - C#
For new UWP apps (starting from Windows 10) Microsoft recommends using [ContentDialog][1] instead.
Example:
private async void MySomeMethod()
{
ContentDialog dlg = new ContentDialog()
{
Title = "My Content Dialog:",
Content = "Operation completed!",
CloseButtonText = "Ok"
};
await dlg.ShowAsync();
}
Usage:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
MySomeMethod();
}
Remark: You can use different styles etc. for the ContentDialog
. Please refer to the link above for various usages for ContentDialog.
[1]: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.contentdialog