2014-04-07 25 views
50

Tôi muốn sử dụng MessageBox để hiển thị lỗi tải xuống trong ứng dụng WP8.1 của mình.Hộp thư ứng dụng toàn cầu: "Tên 'MessageBox' không tồn tại trong ngữ cảnh hiện tại"

Tôi nói thêm:

using System.Windows; 

nhưng khi tôi gõ:

MessageBox.Show(""); 

tôi nhận được lỗi:

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

Trong Object Browser Tôi thấy rằng lớp này nên tồn tại và trong " Project-> Add reference ... -> Assemblies-> Framework "được chỉ ra rằng tất cả các assembly đều được tham chiếu.

Tôi có bỏ lỡ điều gì đó không? Hoặc có cách nào khác để hiển thị một cái gì đó như MessageBox?

Trả lời

102

Đối với ứng dụng toàn cầu, API mới yêu cầu bạn sử dụng await MessageDialog().ShowAsync() (trong Windows.UI.Popups) để đưa nó phù hợp với Win 8.1.

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

Điều đó đã giúp, cảm ơn. – sprrw

+4

Toàn bộ doanh nghiệp Async này giống như một loại vi-rút. Bạn bắt đầu đặt nó ở một nơi và nó lây lan sang mọi thứ, làm cho bạn tái cấu trúc một số lượng lớn mã. Không chắc chắn nếu điều này là dự định hoặc tôi đang làm một cái gì đó sai – Rajiv

+0

@Rajiv afaik nó được dự định, nhưng hãy cho tôi biết nếu bạn tìm thấy bất cứ điều gì đó là chống lại/hỗ trợ này, cho tôi biết. – Ave

43

Chỉ muốn thêm vào câu trả lời ZombieSheep của: cũng có, tùy biến khá dễ dàng

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

thử điều này:

using Windows.UI.Popups; 

mã:

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


    } 

để kích hoạt một số Chức năng Khi "Có" hoặc "Không" được nhấp, Bạn ca n cũng sử dụng:

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

Bạn cũng có thể tạo lớp như lớp tiếp theo. Dưới đây mã một ví dụ sử dụng:

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

} 

Ví dụ để sử dụng nó trong một lớp học

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

    } 
} 
+0

Việc triển khai này không giống như hành vi chặn luồng giống với MessageBox gốc và có thể rất khó hiểu đối với các nhà phát triển mới. –

Các vấn đề liên quan