■ UWP API를 사용하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="UWP API 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Button Name="sendButton" HorizontalAlignment="Center" VerticalAlignment="Center" Width="180" Height="30" Content="토스트 메시지 보내기" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Windows; using Windows.Data.Xml.Dom; using Windows.UI.Notifications; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.sendButton.Click += sendButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 토스트 메시지 보내기 버튼 클릭시 처리하기 - sendButton_Click(sender, e) /// <summary> /// 토스트 메시지 보내기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sendButton_Click(object sender, RoutedEventArgs e) { string text1 = "알림"; string text2 = $"현재 시각은 {DateTime.Now:HH:mm:ss} 입니다."; string imageURI = "ms-appx:///Images/sample.png"; string xml = $@" <toast> <visual> <binding template='ToastGeneric'> <image placement='appLogoOverride' src='{imageURI}'/> <text>{text1}</text> <text>{text2}</text> <image src='{imageURI}'/> </binding> </visual> </toast>"; XmlDocument document = new XmlDocument(); document.LoadXml(xml); ToastNotification notification = new ToastNotification(document); ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier(); notifier.Show(notification); } #endregion } } |
[설정]
1. .NET Framework 버전을 사용하는 경우 [비주얼 스튜디오]에서 [도구] / [Nuget 패키지 관리자] / [패키지 관리자 설정] 메뉴를 클릭한다.
2. [옵션] 대화 상자에서 아래와 같이 설정한다.
3. 아래 누겟 패키지를 설치한다.
• Microsoft.Windows.SDK.Contracts
• System.Runtime.WindowsRuntime
• System.Runtime.WindowsRuntime.UI.Xaml
4. [새 프로젝트 추가] 대화 상자에서 아래와 같이 [Windows 애플리케이션 패키징 프로젝트] 템플릿을 선택하고 [다음] 버튼을 클릭한다.
5. [새 프로젝트 구성] 대화 상자에서 아래와 같이 입력하고 [만들기] 버튼을 클릭한다.
6. [TestProjectSetup] 프로젝트에서 [애플리케이션] 항목에 [TestProject] 프로젝트를 추가한다.
7. [TestProjectSetup] 프로젝트의 [Package.appxmanifest] 파일에서 아래와 같이 설정한다.