Silverlight - 小改寫Image物件

2010/03/01

因為Silverlight 的 Image 物件

我覺得用起來很不方便..

不過我相信一定有一些他的設計考量..

我自己重新做一顆 UserControl ..

我增加三個Property  - ImgSrc , ImgUriKind ,ImgStrech

這樣可以讓在客戶程式的用起來長這樣..


C#:

 this.mImage1.ImgSrc = "http://www.celebritywonder.com/picture/Anne_Hathaway/AnneHathaway_Mazur_7219842.jpg";
 this.mImage5.ImgUriKind = UriKind.RelativeOrAbsolute;
 this.mImage5.ImgSrc = "images/4.jpg";


XAML:




 <my:MImage HorizontalAlignment="Left" Margin="12,12,0,0" Name="mImage1" ImgSrc="http://www.mannythemovieguy.com/images/anne-hathaway.jpg" VerticalAlignment="Top" Width="100" Height="100" ImgStretch="Fill" />
 <my:MImage Height="100" HorizontalAlignment="Left" ImgSrc="http://www.mannythemovieguy.com/images/anne-hathaway.jpg" Margin="118,12,0,0" Name="mImage2" VerticalAlignment="Top" Width="100" ImgStretch="None" />
 <my:MImage Height="100" HorizontalAlignment="Left" ImgSrc="http://www.mannythemovieguy.com/images/anne-hathaway.jpg" Margin="224,12,0,0" Name="mImage3" VerticalAlignment="Top" Width="100" ImgStretch="Uniform" />
 <my:MImage Height="100" HorizontalAlignment="Left" ImgSrc="http://www.mannythemovieguy.com/images/anne-hathaway.jpg" Margin="330,12,0,0" Name="mImage4" VerticalAlignment="Top" Width="100" ImgStretch="UniformToFill" />








則這一顆物件我叫做MImage...

他的內容是..

XAML:


 <UserControl x:Class="MallgicControls.MImage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="100" d:DesignWidth="100" SizeChanged="UserControl_SizeChanged">

    <Image x:Name="MainImage">

    </Image>
</UserControl>





C#:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace MallgicControls
{
    public partial class MImage : UserControl
    {
        public MImage()
        {
            InitializeComponent();
        }


        private string _imageSource;
        private Stretch _imageStretch;
        private UriKind _uriKind = UriKind.RelativeOrAbsolute;

        public UriKind ImgUriKind
        {
            get
            {
                return _uriKind;
            }
            set
            {
                this._uriKind = value;

            }
        }

        public string ImgSrc
        {
            get
            {
                return this._imageSource;
            }
            set
            {
                this._imageSource = value;
                this.MainImage.Source = (new BitmapImage(new Uri(_imageSource, _uriKind)));

            }
        }

        public Stretch ImgStretch
        {
            get
            {
                return this._imageStretch;
            }
            set
            {
                this._imageStretch = value;
                this.MainImage.Stretch = value;
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.MainImage.Width = this.Width;
            this.MainImage.Height = this.Height;
        }
    }
}




不是完美做法...

但是至少解決我目前覺得難用的地方....


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.