2017-10-04 2 views
-2

도와주세요. 문제가 생겼습니다. UWP 코딩을 처음 사용했습니다. 문제 : 버튼으로 구성된 간단한 응용 프로그램을 빌드했습니다. 버튼이 메시지를 표시합니다 누르면UWP의 메시지 상자가 작동하지 않습니다.

오류 : 모듈 CommonLanguageRuntimeLibrary에서 typre System.Collections.CollectionBase를 찾을 수 없습니다 여기

가 없습니다 UWP에는 MessageBox하지만 내 코드

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

답변

3

입니다

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

mm8 이것은 나를 위해 더 이상 작동하지 않았습니다. ( \t 'IAsyncOperation ' 'GetAwaiter'없이 확장 메서드 'GetAwaiter'형의 첫 번째 인수 'IAsyncOperation 을'수용에 대한 정의를 포함하지 않는 볼 수 있습니다 : 대신 나는 는 오류 새 오류 CS4036 해낸있다 'System'에 대한 using 지시어가 누락 되었습니까?) –

+0

상단에'using System; '을 추가하십시오. 나는 내 대답을 편집했다. – mm8

+0

아래의 모든 방법을 사용하는 것이 좋습니다. 새 버튼을 쉽게 만들거나 전달할 수 있습니다! – BluDay

0

내가 강하게, 별도의를 사용하는 것이 좋습니다 포를 표시하는 기능을하십시오 MessageDialog입니다 강아지 메시지. 그것은 단지는 Win32 또는 윈폼 응용 프로그램에 사용되는 이후

UWP는 네임 스페이스 Windows.UI.Popups하지 System.Windows.MessageBox를 사용

여기에 원하는 메시지를 표시하는 좋은 방법 :

// 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 
     } 
    } 
} 

이 방법, 당신이를 표시 할 수는 버튼을 포함하는 하위 배열이있는 객체 배열을 전달하거나 전달하지 않고 메시지 대화 상자를 엽니 다. 이 방법은 매우 효율적입니다. 당신이 이것을 좋아한다면, 그것을 완전히 이해했는지 확인하십시오!

관련 문제