28 March 2017

Windows Store và Json: Ghi file Json và Đọc File Json Bidding Hiển thị dữ liệu qua ListView trên Windows Store C#


MainPage.xaml
Windows Store 2017
<Page
    x:Class="AssignmentTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AssignmentTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="765">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBox x:Name="tb_EmpID" HorizontalAlignment="Left" Height="55" Margin="254,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290"/>
        <TextBox x:Name="tb_EmpName" HorizontalAlignment="Left" Margin="254,180,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="50" Width="290"/>
        <TextBox x:Name="tb_DOB" HorizontalAlignment="Left" Height="60" Margin="254,290,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290"/>
        <TextBox x:Name="tb_Gender" HorizontalAlignment="Left" Height="55" Margin="254,400,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290"/>
        <TextBox x:Name="tb_Address" HorizontalAlignment="Left" Height="45" Margin="254,500,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290"/>
        <TextBox x:Name="tb_Email" HorizontalAlignment="Left" Height="55" Margin="254,580,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="55" Margin="35,80,0,0" TextWrapping="Wrap" Text="Employee ID" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <TextBlock x:Name="textBlock_Copy" HorizontalAlignment="Left" Height="55" Margin="35,175,0,0" TextWrapping="Wrap" Text="Emp Name" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <TextBlock x:Name="textBlock_Copy1" HorizontalAlignment="Left" Height="55" Margin="35,295,0,0" TextWrapping="Wrap" Text="DOB" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <TextBlock x:Name="textBlock_Copy2" HorizontalAlignment="Left" Height="55" Margin="35,400,0,0" TextWrapping="Wrap" Text="Gender" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <TextBlock x:Name="textBlock_Copy3" HorizontalAlignment="Left" Height="55" Margin="35,500,0,0" TextWrapping="Wrap" Text="Address" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <TextBlock x:Name="textBlock_Copy4" HorizontalAlignment="Left" Height="55" Margin="35,580,0,0" TextWrapping="Wrap" Text="Email" VerticalAlignment="Top" Width="214" FontSize="36"/>
        <Button x:Name="btn_add" Content="ADD" HorizontalAlignment="Left" Height="59" Margin="127,671,0,0" VerticalAlignment="Top" Width="125" FontSize="20" Click="btn_add_Click"/>
        <Button x:Name="btn_save" Content="RESET" HorizontalAlignment="Left" Height="59" Margin="697,671,0,0" VerticalAlignment="Top" Width="116" FontSize="20" Click="btn_save_Click"/>
        <Button x:Name="btn_Load" Content="LOAD" HorizontalAlignment="Left" Margin="277,671,0,0" VerticalAlignment="Top" Height="59" Width="128" FontSize="20" Click="btn_Load_Click"/>
        <ListView x:Name="listView" HorizontalAlignment="Left" Background="DarkCyan" Height="495" Margin="575,140,0,0" VerticalAlignment="Top" Width="740" FontSize="10">
            <ListView.ItemTemplate>
                <DataTemplate>

                    <StackPanel Orientation="Horizontal">

                        <StackPanel>
                            <TextBlock Text="{Binding EmpID}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="{Binding EmpName}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="{Binding DOB}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="{Binding Gender}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="{Binding Address}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Text="{Binding Email}" FontSize="24" Margin="20 0 0 0"/>
                        </StackPanel>

                    </StackPanel>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Height="55" Margin="632,80,0,0" TextWrapping="Wrap" Text="IDName  -  Name   -  DOB   -  Gender  -  Address  -   Email " VerticalAlignment="Top" Width="683" FontSize="24"/>
        <Button x:Name="btn_Edit" Content="EDIT" HorizontalAlignment="Left" Margin="423,671,0,0" VerticalAlignment="Top" Height="58" Width="124" FontSize="20"/>
        <Button x:Name="btn_Delete" Content="DELETE" HorizontalAlignment="Left" Margin="572,671,0,0" VerticalAlignment="Top" Height="58" Width="96" FontSize="20"/>

    </Grid>
</Page>
MainPage.xaml.cs
Windows Store 2017
using Newtonsoft.Json;
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.Storage;
using Windows.Storage.Streams;
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 AssignmentTest
{
    public sealed partial class MainPage : Page
    {
        List<Employee> list;

        public MainPage()
        {
            this.InitializeComponent();
            list = new List<Employee>();
        }


        private async void btn_add_Click(object sender, RoutedEventArgs e)
        {

            Employee em = new Employee();
            em.EmpID = tb_EmpID.Text;
            em.EmpName = tb_EmpName.Text;
            em.DOB = tb_DOB.Text;
            em.Gender = tb_Gender.Text;
            em.Address = tb_Address.Text;
            em.Email = tb_Email.Text;

            list.Add(em);

            //Pase Object List sang Json String
            string jsonContent = JsonConvert.SerializeObject(list);

            //Folder
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            StorageFile textFile = await localFolder.CreateFileAsync("employee.json", CreationCollisionOption.ReplaceExisting);
            using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                using (DataWriter textWriter = new DataWriter(textStream))
                {
                    textWriter.WriteString(jsonContent);
                    await textWriter.StoreAsync();
                }

            }


            Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog("Them du lieu thanh cong");
            await dialog.ShowAsync();
        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            tb_EmpID.Text = "";
            tb_EmpName.Text = "";
            tb_DOB.Text = "";
            tb_Gender.Text = "";
            tb_Address.Text = "";
            tb_Email.Text = "";
        }

        private async void btn_Load_Click(object sender, RoutedEventArgs e)
        {
            // Đọc file Json
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            StorageFile textFile = await localFolder.GetFileAsync("employee.json");
            using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
            {
                DataReader textReader = new DataReader(textStream);
                uint textLength = (uint)textStream.Size;
                await textReader.LoadAsync(textLength);
                string jsonContent = textReader.ReadString(textLength);
                //Get list từ file Json
                List<Employee> listJSON = JsonConvert.DeserializeObject<List<Employee>>(jsonContent);
                //listJSON đã chứa giá trị
                //Bindding đến name="listView" để hiên thị dữ liệu
                this.listView.ItemsSource = listJSON;
                
            }
        }
    }
}
Để biết được file employee.json của chúng ta nằm ở đâu vui lòng các bạn Debug để thấy Path dẫn đến file đó!

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang