How to insert data to Mobile Services - Windows Store App (Windows Azure)
How to insert data to Mobile Services - Windows Store App (Windows Azure) สรุปวิธีการบันทึก Insert ข้อมูลเข้า Table ของ Mobile Services บน Windows Azure ด้วย Windows Store App แบบสั้น ๆ ง่าย ๆ
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;
using Windows.UI.Popups;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace myApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
///
public class MyMember
{
public int Id { get; set; }
[JsonProperty(PropertyName = "username")]
public string Username { get; set; }
[JsonProperty(PropertyName = "password")]
public string Password { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "tel")]
public string Tel { get; set; }
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
}
public sealed partial class MainPage : Page
{
private MobileServiceCollection<MyMember, MyMember> items;
private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>();
public MainPage()
{
this.InitializeComponent();
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
items = await memberTable.ToCollectionAsync();
var insertItem = new MyMember
{
Username = txtUsername.Text
,
Password = txtPassword.Password
,
Name = txtName.Text
,
Email = txtEmail.Text
,
Tel = txtTel.Text
};
await memberTable.InsertAsync(insertItem);
items.Add(insertItem);
// Dailog
MessageDialog msgDialog = new MessageDialog("Register Data Successfully.", "Status :");
await msgDialog.ShowAsync();
}
catch (MobileServiceInvalidOperationException ex)
{
throw ex;
}
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}
Example
Windows Store App : Register Form (Azure Mobile Services)