Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Azure > Windows Azure (Mobile Services) and Windows Phone (WP8) > ตอนที่ 16 : Show Case 4 : Delete Data (WP and Mobile Services)



Clound SSD Virtual Server

ตอนที่ 16 : Show Case 4 : Delete Data (WP and Mobile Services)

ตอนที่ 16 : Show Case 4 : Delete Data (WP and Mobile Services) ตัวอย่าง Show Case การเขียน Windows Phone กับ Mobile Services เพื่อที่จะทำการลบ Delete ข้อมูลในที่อยู่ในตาราง Table ด้วยการแสดงรายการทั้งหมดบน LongListSelector จากนั้นผู้ใช้สามารถที่จะเลือกรายการที่ต้องการลบ และในการลบจะมี Dialog สำหรับการ Confirm หลังจาก Confirm แล้วข้อมูลก็จะถูกลบออกจากรายการของ LongListSelector รวมทั้งลบออกจากรายการของ Mobile Services ที่อยู่บน Windows Azure

Windows Phone  Delete Data Mobile Services

Windows Phone Mobile Services Delete Data


สำหรับขั้นตอนนั้นก็คือสร้าง Page ด้วย LongListSelector จากนั้นดึงข้อมูลมาจาก Mobile Services และแต่ล่ะ Item สามารถที่จะเลือกรายการเพื่อ Delete ลบข้อมูลของ Mobile Services ที่อยู่บน Windows Azure ได้


Example ตัวอย่างการทำระบบลบข้อมูล Delete Data บน Mobile Services ด้วย Windows Azure

Windows Phone  Delete Data Mobile Services

ตอนนี้เรามี Mobile Services อยู่ 1 รายการ

Windows Phone  Delete Data Mobile Services

ชื่อตารางว่า MyMember

Windows Phone  Delete Data Mobile Services

มีข้อมูลอยู่ทั้งหมด 3 รายการ


กลับมายัง Project ของ Windows Phone บน Visual Studio

Windows Phone  Delete Data Mobile Services

โครงสร้างไฟล์








Windows Phone  Delete Data Mobile Services

ในไฟล์ App.xaml.cs ให้ Copy Url และ key มาวางดังรูป จากนั้นออกแบบ Layout และเขียน Code ดังนี้

MainPage.xaml

Windows Phone  Delete Data Mobile Services

<phone:PhoneApplicationPage
    x:Class="myPhoneApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="ThaiCrate.Com" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="45"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <phone:LongListSelector Grid.Row="4" Grid.ColumnSpan="2" Name="myListItems">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <Button Content="Del" Height="72" Tag="{Binding Id}" HorizontalAlignment="Left" Name="btnDel" Click="ClickDelete" VerticalAlignment="Top" Width="108" />
                                <TextBlock Text="{Binding Username}"  TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding Tel}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding Email}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>


MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using myPhoneApp.Resources;

using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;

namespace myPhoneApp
{

    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 partial class MainPage : PhoneApplicationPage
    {

        private MobileServiceCollection<MyMember, MyMember> items;
        private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private async void RefreshMemberItems()
        {
            try
            {
                items = await memberTable.ToCollectionAsync();
            }
            catch (MobileServiceInvalidOperationException e)
            {
                MessageBox.Show(e.Message, "Error loading items", MessageBoxButton.OK);
            }

            myListItems.ItemsSource = items;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RefreshMemberItems();
        }

        private async  void ClickDelete(object sender, RoutedEventArgs e)
        {
            Button btnDel = (Button)e.OriginalSource;
            if (MessageBox.Show("Are you sure?", "Delete ", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {

                //** Get Item for Delete ***/
                items = await memberTable
                .Where(memberItem => memberItem.Id == Convert.ToInt32(btnDel.Tag.ToString()))
                .ToCollectionAsync();
                
                //*** Delete ***/
                MyMember member = items[0];
                await memberTable.DeleteAsync(member);
                RefreshMemberItems();

                MessageBox.Show("Delete Completed.", "Result", MessageBoxButton.OK);

            }

        }

    }

}


Screenshot

Windows Phone  Delete Data Mobile Services

แสดงรายการข้อมูลจาก Mobile Services บน Windows Phone

Windows Phone  Delete Data Mobile Services

มี Dialog สำหรับการ Confirm การลบข้อมูล

Windows Phone  Delete Data Mobile Services

ลบข้อมูลเรียบร้อย

Windows Phone  Delete Data Mobile Services

ข้อมูลหายไปจาก LongListSelector

Windows Phone  Delete Data Mobile Services

เมื่อกลับไปดูที่ Mobile Services ข้อมูลก็จะถูกลบ Delete ออกไป

สามารถทำการดาวน์โหลด Code ทั้งหมดได้จากท้ายบทความ

บทความถัดไปที่แนะนำให้อ่าน









บทความที่เกี่ยวข้อง


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-11 21:18:04 / 2017-03-24 11:37:23
  Download : Download  ตอนที่ 16 : Show Case 4 : Delete Data (WP and Mobile Services)
 Sponsored Links / Related

 
ตอนที่ 1 : Windows Phone(WP) กับ Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียม Windows Phone(WP) ก่อนที่จะเขียนบน Azure Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง Windows Phone(WP) กับ Mobile Services และการเรียกใช้งาน
Rating :

 
ตอนที่ 4 : สร้าง Project Windows Phone(WP) และการเชื่อมต่อกับ Mobile Services
Rating :

 
ตอนที่ 5 : Widows Phone สร้าง Table บน Mobile Services และการ Insert ข้อมูล
Rating :

 
ตอนที่ 6 : Windows Phone (WP) อ่าน Data จาก Table ของ Azure Mobile Service
Rating :

 
ตอนที่ 7 : อ่านข้อมูล Mobile Services การใช้ Where แสดงผลบน Windows Phone
Rating :

 
ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย Windows Phone
Rating :

 
ตอนที่ 9 : การทำ Push Notifications in Mobile Services ด้วย Windows Phone
Rating :

 
ตอนที่ 10 : Validate และ Modify data in Mobile Services บน Windows Phone
Rating :

 
ตอนที่ 11 : สร้าง Refine Mobile Services queries with paging บน Windows Phone
Rating :

 
ตอนที่ 12 : Scripts to authorize users in Mobile Services บน Windows Phone
Rating :

 
ตอนที่ 13 : Show Case 1 : Register Form (WP and Mobile Services)
Rating :

 
ตอนที่ 14 : Show Case 2 : Login User Password (WP and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 3 : Update Data (WP and Mobile Services)
Rating :

 
ตอนที่ 17 : บทความอื่น ๆ เกี่ยวกับ Windows Phone กับ Azure Mobile Services
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่