Wiki


Wiki Table of Contents

Tags

Page Details

Published by:
1 person found this article useful.

100% of people found this useful
Silverlight Tip of the Day #24: How to Apply a XAML Template to a Class

Hãy nói là bạn có 1 lớp nơi mà bạn muốn khai báo phần tử thông qua một mẫu (template) XAML  thay vì tự động tạo ra chúng.

Thí dụ, mã XAML dưới đây khai báo một hình ảnh và thêm nó vào một canvas như thế này:

 

<Canvas x:Name="MyCanvas">
    <Image x:Name="MyImage" Source="Grass.png"></Image>
</Canvas>

Và để  tạo ra những thứ giống như đoạn XAML trên , bạn cần phải khai báo những dòng code sau:

Image myImage = new Image();
Uri uri = new Uri("Grass.png", UriKind.Relative);
ImageSource imgSrc = new System.Windows.Media.Imaging.BitmapImage(uri);
myImage.SetValue(Image.SourceProperty, imgSrc);

Cái cách mà chúng ta làm điều này  thông qua một template trong một lớp là  đầu tiên đặt XAML vào một chuỗi như thế này:

 

private const string _contentTemplate
       = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
           "                  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
           "<Image x:Name=\"MyImage\" Source=\"Grass.png\"></Image>" +
           "</ControlTemplate>";

Lớp của bạn cần được kế thừa từ Control:

public class MyControl : Control

Nào, bây giờ trong hàm khởi tạo của class của bạn, bạn có thể load template này thông qua hàm  XamlReader.Load() .  Bạn sẽ cần thêm vào thư viện để tham khảo là namespace System.Windows.Markup

public MyControl()
{
    Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
    ApplyTemplate();
}

ApplyTemplate() là nơi bạn có thể lấy những phần tử mà được khai báo trong đoạn mã XAML của bạn. Override  hàm  OnApplyTemplate như thế này:

private _myImage;
 
public override void OnApplyTemplate()
{
    _myImage = (Image)GetTemplateChild("MyImage");
}

Class hoàn chỉnh:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
 
namespace MapEditor
{
    public class MyImage : Control
    {
        Image _myImage = null;
 
        private const string _contentTemplate
               = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
                   "                  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
                   "<Image x:Name=\"MyImage\" Source=\"Grass.png\"></Image>" +
                   "</ControlTemplate>";
 
        public override void OnApplyTemplate()
        {
            _myImage = (Image)GetTemplateChild("MyImage");
        }
 
        public MyImage()
        {
            Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
            ApplyTemplate();
        }
 
    }
}

Thank you,
--Mike Snow

Recent Comments

By: Trinh Minh Cuong Posted on 07-18-2009 10:19 AM

Very good job, Quỳnh. Em có thể viết một quyển sách nhỏ về SilverLight rồi đấy.