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 > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > รู้จักกับ Custom User Control และการสร้าง UserControl บน Windows Phone



Clound SSD Virtual Server

รู้จักกับ Custom User Control และการสร้าง UserControl บน Windows Phone

รู้จักกับ Custom User Control และการสร้าง UserControl บน Windows Phone ในกรณีที่เราได้เรียกใช้ Controls หรือกลุ่มของ Controls ในหลาย ๆ ส่วนของ Application หรือหลาย ๆ Page แทนที่เราจะสร้างขึ้นมาในแต่ล่ะ Page เราสามารถใช้การสร้างกลุ่มของ Controls เหล่านั้นไว้ใน Page ของ User Controls และใช้การเรียกงาน User Control ใน Page ต่าง ๆ ซึ่งวิธีนี้จะสะดวกในการเขียนและการแก้ไขหน้าจอ Layout ต่าง ๆ ของ Application ไม่ให้เกิดความซ้ำซ้อนในการเขียนโปรแกรมได้

Windows Phone Custom User Control



Example การสร้าง UserControl และการเรียกใช้งาน UserControl แบบง่าย ๆ

Windows Phone Custom User Control

เพิ่ม Item เข้ามาใหม่โดยการคลิกขวาที่ Project -> Add -> New Items เลือกเป็น Windows Phone User Control

Windows Phone Custom User Control

ไฟล์ถูก Add เข้ามาใน Project

Windows Phone Custom User Control

เมื่อดูมุมมอง Design จะเห็นหน้าจอว่างเปล่า

Windows Phone Custom User Control

มุมมองของ XAML จะเห็นว่ามีชื่อ User Control ที่เราได้สร้างขึ้นมา

Windows Phone Custom User Control

ออกแบบ User Control ดังภาพ

MyUserControl.xaml
    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Height="72">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Height="45" Width="45" HorizontalAlignment="Left" Margin="49,12,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Source="Images/WP.jpg" />
        <HyperlinkButton Content="Menu1" Grid.Column="1" Height="30" HorizontalAlignment="Left" Name="Link1" VerticalAlignment="center" Width="100" Click="Link1_Click" />
        <HyperlinkButton Content="Menu2" Grid.Column="2" Height="30" HorizontalAlignment="Left" Name="Link2" VerticalAlignment="center" Width="100" Click="Link2_Click" />
        <HyperlinkButton Content="Menu3" Grid.Column="3" Height="30" HorizontalAlignment="Left" Name="Link3" VerticalAlignment="center" Width="100" Click="Link3_Click" />
    </Grid>

XAML ที่ใช้สำหรับสร้าง Layout

VB.NET
    Private Sub Link1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Your Click Menu 1")
    End Sub

    Private Sub Link2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Your Click Menu 2")
    End Sub

    Private Sub Link3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Your Click Menu 3")
    End Sub









C#
private void Link1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
	MessageBox.Show("Your Click Menu 1");
}

private void Link2_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
	MessageBox.Show("Your Click Menu 2");
}

private void Link3_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
	MessageBox.Show("Your Click Menu 3");
}

สร้าง Event ในภาษา VB.NET และ C#



การเรียกใช้ Windows Phone User Control

กลับมายัง Application Page ที่จะเรียกใช้ User Control ที่ได้สร้างขึ้น

Windows Phone Custom User Control

ในกรณีที่เรียกใช้ภายใต้ Grid ให้กำหนดค่าดังภาพ

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent" xmlns:src="clr-namespace:PhoneApp" >
        <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 x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <src:MyUserControl Margin="0,0,0,526" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="148,213,0,0" Name="TextBlock1" Text="Main Page" VerticalAlignment="Top" Width="167" TextAlignment="Center" />
        </Grid>
        
    </Grid>

xmlns:src="clr-namespace:PhoneApp" เป็นการเรียก NameSpace ของ Application
src:MyUserControl อ้างถึง User Control

Windows Phone Custom User Control

หน้าจอ Page ที่ได้ ซึ่งจะเห็นว่าจะแสดง Layout ที่มาจาก User Control ที่ได้สร้างขึ้น

Screenshot

Windows Phone Custom User Control

ทดสอบเรียกผ่าน Emulator แสดงผลลัพธ์ของ User Control ดังภาพ

Windows Phone Custom User Control

เมื่อคลิกที่ Item ของ User Control จะแสดง Event ต่าง ๆ ที่ได้กำหนดขึ้นในขั้นตอนการสร้าง User Control








สรุป
User Control จะเหมาะสำหรับ Application ที่ต้องการเรียกใช้งาน Control ชุดนั้นบ่อย ๆ เช่นในตัวอย่างจะสร้างเป็น Menu และ Menu นี้จะเสามารถเรียกใช้งานได้จากหลาย ๆ Page ของ Application

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-02 22:10:44 / 2017-03-25 22:10:35
  Download : Download  รู้จักกับ Custom User Control  และการสร้าง UserControl บน Windows Phone
 Sponsored Links / Related

 
รู้จักกับ Controls เครื่องมือ ต่าง ๆ ที่ใช้สำหรับการเขียนโปรแกรมบน Windows Phone
Rating :

 
AdControl - Windows Phone Controls
Rating :

 
Border - Windows Phone Controls
Rating :

 
Button - Windows Phone Controls
Rating :

 
Canvas - Windows Phone Controls
Rating :

 
CheckBox - Windows Phone Controls
Rating :

 
Ellipse - Windows Phone Controls
Rating :

 
Grid - Windows Phone Controls
Rating :

 
HyperlinkButton - Windows Phone Controls
Rating :

 
Image - Windows Phone Controls
Rating :

 
ListBox - Windows Phone Controls
Rating :

 
Map - Windows Phone Controls
Rating :

 
MediaElement - Windows Phone Controls
Rating :

 
Panorama - Windows Phone Controls
Rating :

 
PasswordBox - Windows Phone Controls
Rating :

 
Pivot - Windows Phone Controls
Rating :

 
ProgressBar - Windows Phone Controls
Rating :

 
RadioButton - Windows Phone Controls
Rating :

 
Rectangle - Windows Phone Controls
Rating :

 
RepeatButton - Windows Phone Controls
Rating :

 
ScrollViewer - Windows Phone Controls
Rating :

 
Slider - Windows Phone Controls
Rating :

 
StackPanel - Windows Phone Controls
Rating :

 
TextBlock - Windows Phone Controls
Rating :

 
TextBox - Windows Phone Controls
Rating :

 
ToggleButton - Windows Phone Controls
Rating :

 
WebBrowser - Windows Phone Controls
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 00
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 อัตราราคา คลิกที่นี่