Universal Apps MessageBox: “De naam ‘MessageBox’ bestaat niet in de huidige context”

Ik wil MessageBox gebruiken om downloadfouten in mijn WP8.1-app te tonen.

Ik heb toegevoegd:

using System.Windows;

maar als ik typ:

MessageBox.Show("");

Ik krijg een foutmelding:

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

In Object Browser vond ik dat zo’n klasse zou moeten bestaan ​​en in “Project->Add reference… ->Assemblies->Framework” wordt getoond dat naar alle assembly’s wordt verwezen.

Mis ik iets? Of is er een andere manier om iets als messagebox weer te geven?


Antwoord 1, autoriteit 100%

Voor Universal Apps vereisen de nieuwe API’s dat u await MessageDialog().ShowAsync()(in Windows.UI.Popups) gebruikt om het in overeenstemming te brengen met Win 8.1.

var dialog = new MessageDialog("Your message here");
await dialog.ShowAsync();

Antwoord 2, autoriteit 38%

Ik wilde alleen iets toevoegen aan het antwoord van ZombieSheep: aanpassen is ook vrij eenvoudig

       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)
        { *** }

Antwoord 3, autoriteit 24%

probeer dit:

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

Om een ​​bepaalde functie te activeren Als er op “Ja” of “Nee” wordt geklikt, kunt u ook het volgende gebruiken:

msgbox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.TriggerThisFunctionForYes)));
msgbox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.TriggerThisFunctionForNo)));

Antwoord 4, autoriteit 2%

Je zou ook een klas kunnen maken zoals de volgende. Onder de code een gebruiksvoorbeeld:

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

Het voorbeeld om het in een klas te gebruiken

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

Other episodes