27 March 2017

Windows Store và Json: TEST Create JSON Pase Object sang Json show Dialog & Toat

 Dialog
 Toat
Data.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App1
{
    class data
    {
        public class homenumber
        {
            public string location { set; get; }
            public string PhoneNumber { set; get; }
        }


        public class worknumber
        {
            public string location { set; get; }
            public string PhoneNumber { set; get; }
        }

        public class phone
        {
            public homenumber homeNumber;
            public worknumber workNumber;
        }
        public class employee
        {
            public string name { set; get; }
            public Boolean permission { set; get; }
            public List<phone> phoneNumber { set; get; }
        }
    }
}
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.UI.Popups;
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;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

        }

        private async void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            //Set value cho object
            data.homenumber home = new data.homenumber();
            home.location = "home";
            home.PhoneNumber = tb_Home_Phone.Text;
            //List<data.homenumber> homeNumber = new List<data.homenumber>();
            //homeNumber.Add(home);

            data.worknumber work = new data.worknumber();
            work.location = "work";
            work.PhoneNumber = tb_Work_Phone.Text;
            //List<data.worknumber> workNumber = new List<data.worknumber>();
            //workNumber.Add(work);

            data.phone phone = new data.phone();
            phone.homeNumber = home;
            phone.workNumber = work;
            List<data.phone> phoneNumber = new List<data.phone>();
            phoneNumber.Add(phone);

            data.employee em = new data.employee();
            em.name = tb_Name.Text;
            em.permission = checkBox.IsChecked.Value;
            em.phoneNumber = phoneNumber;

            //Pase từ Object sang Json
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(em);
            var dialog = new MessageDialog(json);
            await dialog.ShowAsync();

            // Đây là Toast
            var myToastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
            XmlNodeList myTextElement = myToastXml.GetElementsByTagName("text");

            //JsonValue jsonValue = JsonValue.Parse(json);
            //string workPhone = jsonValue.GetObject().GetNamedString("WorkPhone");

            myTextElement[0].AppendChild(myToastXml.CreateTextNode(tb_Work_Phone.Text));
            XmlNodeList myImageElement = myToastXml.GetElementsByTagName("image");
            ((XmlElement)myImageElement[0]).SetAttribute("src", "ms-appx:///Assets/piano-tiles-logo-70x70.scale-100.png");
            ToastNotification toastNotification = new ToastNotification(myToastXml);
            ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
            //var dialog = new MessageDialog("Notification successfully!");
            //await dialog.ShowAsync();
        }
    }
}
//Chúng ta tạo Object từ một Json cho sẵn dưới đây thật dễ dàng qua Website dưới đây 
    "name":"Antonio",
    "permission":true,
    "phoneNumber": 
         
            "homeNumber": 
                "location":"home",
                "PhoneNumber":"09817266124"
            },
            "workNumber": 
                "location":"work",
                "PhoneNumber":"0434198274"
            }
        }
    ]
}
Pase từ Json sang Object C# http://json2csharp.com

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang