[WP7]動態換圖

[WP7]動態換圖

 

一開始先從工具箱拉出一個image控制項 然後新增一個Images資料夾

把images.jpg 跟images2.jpg 這兩張圖放進去

再貼上以下程式碼

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 Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;

namespace changeIMG
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 建構函式
        public MainPage()
        {
            InitializeComponent();
            BitmapImage myimage = new BitmapImage(new Uri("/changeIMG;component/Images/images.jpg", UriKind.Relative));
            image1.Source = myimage;

        }

        private void image1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
                BitmapImage myimage = new BitmapImage(new Uri("/changeIMG;component/Images/images2.jpg", UriKind.Relative));
                image1.Source = myimage;
        }
    }
}

需注意在開頭要using System.Windows.Media.Imaging;

BitmapImage myimage = new BitmapImage(new Uri("/changeIMG;component/Images/images.jpg", UriKind.Relative));

image1.Source = myimage;

這兩行就是換圖的關鍵了,changeIMG是我的專案名稱,後面是資料夾跟圖片檔名

動態換圖就是這麼簡單而以


如有錯誤 歡迎指正