Universal Apps MessageBox: "The name" MessageBox "does not exist in the current context"

I want to use MessageBox to display loading errors in my WP8.1 application.

I added:

using System.Windows; 

but when I type:

 MessageBox.Show(""); 

I get an error:

 "The name 'MessageBox' does not exist in the current context" 

In Object Explorer, I found that such a class must exist, and in "Project-> Add reference ... → Assemblies-> Framework" it is shown that all assemblies are referenced.

Did I miss something? Or is there another way to show something like messages?

+57
c # win-universal-app
Apr 7 '14 at 10:06 on
source share
7 answers

For Universal Apps, the new APIs require you to use await MessageDialog().ShowAsync() (on Windows.UI.Popups) to bring it into line with Win 8.1.

 var dialog = new MessageDialog("Your message here"); await dialog.ShowAsync(); 
+120
Apr 7 '14 at 10:21
source share

Just wanted to add the answer to ZombieSheep: also, the setup is pretty 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) { *** } 
+48
Sep 17 '14 at 20:09
source share

try the following:

  using Windows.UI.Popups; 

the 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 start a function When Yes or No is pressed, you can also use:

 msgbox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.TriggerThisFunctionForYes))); msgbox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.TriggerThisFunctionForNo))); 
+30
Sep 25 '14 at 7:35
source share

You can also create a class similar to the following. The following is an example of use:

 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(); } } } 

Class usage example

 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"); } } } 
+2
Apr 23 '17 at 10:13
source share
 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(); } } 
+2
May 05 '18 at 21:14
source share

For new UWP applications (starting with Windows 10), Microsoft recommends using ContentDialog .

An 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(); } 

Note : you can use different styles, etc. For ContentDialog . Please refer to the link above for various uses of ContentDialog.

0
Jun 19 '19 at 20:04 on
source share

just use:

 using System.Windows.Forms; 
-one
Sep 02 '17 at 15:39 on
source share



All Articles