Test WADS2
--Tạo JSON--
//Contact c = new Contact();
//c.name="Antonio"
string json = Newtonsoft.Json.JsonConvert.SerializeObject(c);
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile = await localFolder.CreateFileAsync("contact3.json",CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (DataWriter textWriter = new DataWriter(textStream))
{
textWriter.WriteString(json);
await textWriter.StoreAsync();
}
}
--Đọc JSON--
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile = await localFolder.GetFileAsync("contact3.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);
JsonValue jsonValue = JsonValue.Parse(jsonContent);
string name = jsonValue.GetObject().GetNamedString("name");
tbName.Text = name; //VD: đưa value name vào textBox
}
--Hiển thị Value trên ComboBox--
protected override void OnNavigatedTo(NavigationEventArgs e)
{
List<string> item = new List<string>();
item.Add("family");
item.Add("family_2");
item.Add("family_3");
comboBox.DataContext = item;
}
<ComboBox x:Name="comboBox" ItemsSource="{Binding}" />
--Display ảnh từ Pictures Library--
var file = await
Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("human.png");
var stream = await file.OpenReadAsync();
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
this.image.Source = bitmapImage;
//Chú ý vào file manifest cấp quyền
Tạo Page Create Contact
MainPage.xaml
Windows Store 2017
<Page
x:Class="TestWSAD2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestWSAD2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="textBlock" FontSize="30" HorizontalAlignment="Left" Height="58" Margin="335,69,0,0" TextWrapping="Wrap" Text="Create new Contact" VerticalAlignment="Top" Width="615"/>
<TextBlock x:Name="textBlock1" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="225,185,0,0" TextWrapping="Wrap" Text="Contact Name" VerticalAlignment="Top" Width="185"/>
<TextBlock x:Name="textBlock1_Copy" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="225,280,0,0" TextWrapping="Wrap" Text="Contact Number" VerticalAlignment="Top" Width="185"/>
<TextBlock x:Name="textBlock1_Copy1" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="225,384,0,0" TextWrapping="Wrap" Text="Contact Group" VerticalAlignment="Top" Width="185"/>
<TextBlock x:Name="textBlock1_Copy2" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="225,502,0,0" TextWrapping="Wrap" Text="Contact Image" VerticalAlignment="Top" Width="185"/>
<TextBox x:Name="tbName" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="505,185,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="329"/>
<TextBox x:Name="tbNumber" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="505,280,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="329"/>
<TextBox x:Name="tbImage" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="505,502,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="329"/>
<ComboBox x:Name="comboBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="40" Margin="505,384,0,0" VerticalAlignment="Top" Width="329" FontSize="18" />
<Button x:Name="btnBrowser" Content="Browser..." HorizontalAlignment="Left" Height="43" Margin="877,502,0,0" VerticalAlignment="Top" Width="182" Click="btnBrowser_Click"/>
<Button x:Name="btnSave" Content="Save Contact" HorizontalAlignment="Left" Height="43" Margin="542,610,0,0" VerticalAlignment="Top" Width="181" Click="btnSave_Click"/>
</Grid>
</Page>
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.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
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;
namespace TestWSAD2
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//Display comboBox
List<string> item = new List<string>();
item.Add("family");
item.Add("family_2");
item.Add("family_3");
comboBox.DataContext = item;
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
Contact c = new Contact();
c.name = tbName.Text;
c.number = tbNumber.Text;
c.group = comboBox.SelectedItem.ToString();
c.image = tbImage.Text;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(c);
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile = await localFolder.CreateFileAsync("contact3.json",CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (DataWriter textWriter = new DataWriter(textStream))
{
textWriter.WriteString(json);
await textWriter.StoreAsync();
}
}
Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog("Them du lieu thanh cong");
await dialog.ShowAsync();
Frame.Navigate(typeof(SearchContact));
}
private async void btnBrowser_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".gif");
picker.FileTypeFilter.Add(".jpeg");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
tbImage.Text = file.Name;
else
tbImage.Text = "Chua chon file";
}
}
}
class Contact
{
public string name { set; get; }
public string number { set; get; }
public string group { set; get; }
public string image { set; get; }
}
Add thư viện hỗ trợ JSON
Cấu trúc Project
Tạo page SearchContact
SearchContact.xaml
Windows Store 2017
<Page x:Class="TestWSAD2.SearchContact" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestWSAD2" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock x:Name="textBlock" FontSize="30" HorizontalAlignment="Left" Height="58" Margin="165,85,0,0" TextWrapping="Wrap" Text="Search Contact" VerticalAlignment="Top" Width="615"/> <TextBlock x:Name="textBlock1" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="55,201,0,0" TextWrapping="Wrap" Text="Contact Name" VerticalAlignment="Top" Width="185"/> <TextBlock x:Name="textBlock2" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="55,296,0,0" TextWrapping="Wrap" Text="Contact Number" VerticalAlignment="Top" Width="185"/> <TextBlock x:Name="textBlock3" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="55,400,0,0" TextWrapping="Wrap" Text="Contact Group" VerticalAlignment="Top" Width="185"/> <TextBlock x:Name="textBlock4" FontSize="25" HorizontalAlignment="Left" Height="40" Margin="55,518,0,0" TextWrapping="Wrap" Text="Contact Image" VerticalAlignment="Top" Width="185"/> <TextBox x:Name="tbName" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="335,201,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="416"/> <TextBox x:Name="tbNumber" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="335,296,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="416"/> <TextBox x:Name="tbImage" HorizontalAlignment="Left" FontSize="18" Height="40" Margin="335,518,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="416"/> <ComboBox x:Name="comboBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="40" Margin="335,400,0,0" VerticalAlignment="Top" Width="416" FontSize="18" /> <Button x:Name="btnSearchContact" Content="Search Contact" HorizontalAlignment="Left" Height="46" Margin="843,198,0,0" VerticalAlignment="Top" Width="214" Click="btnSearchContact_Click"/> <Button x:Name="btnBack" Content="Back" HorizontalAlignment="Left" Height="37" Margin="447,622,0,0" VerticalAlignment="Top" Width="148" Click="btnBack_Click" d:LayoutOverrides="HorizontalAlignment"/> <Image x:Name="image" HorizontalAlignment="Right" Margin="0,345,312,210" Width="208" d:LayoutOverrides="VerticalAlignment"/> </Grid> </Page>
SearchContact.xaml.cs
Windows Store 2017
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Data.Json; 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.Media.Imaging; using Windows.UI.Xaml.Navigation; namespace TestWSAD2 { public sealed partial class SearchContact : Page { public SearchContact() { this.InitializeComponent(); } private void btnBack_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(MainPage)); } private async void btnSearchContact_Click(object sender, RoutedEventArgs e) { StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile textFile = await localFolder.GetFileAsync("contact3.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); JsonValue jsonValue = JsonValue.Parse(jsonContent); string name = jsonValue.GetObject().GetNamedString("name"); if (tbName.Text.Equals(name)) { //TexBox name tbName.Text = name; //TextBox number string number = jsonValue.GetObject().GetNamedString("number"); tbNumber.Text = number; //Combobox group string group = jsonValue.GetObject().GetNamedString("group"); List<String> list = new List<String>(); list.Add(group); comboBox.DataContext = list; //TextBox image string image = jsonValue.GetObject().GetNamedString("image"); tbImage.Text = image; //Show image var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(image); var stream = await file.OpenReadAsync(); var bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); this.image.Source = bitmapImage; } } } } }
Run Test App
Create success!
Go to Search page
Display kết quả Search và hiển thị hình ảnh
0 nhận xét:
Post a Comment