這篇文章給大家介紹如何在C#中使用Image控件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
首先是View層,比較簡單:
然后我們再來看看Model層也很簡單。
public class LTEModel : BaseModel { private string _imageSource = null; public string ImgSource { get { return _imageSource; } set { if (value != _imageSource) { _imageSource = value; FirePropertyChanged("ImgSource"); } } } }
然后就是重要的轉(zhuǎn)換器:
public class StringToImageSourceConverter:IValueConverter { #region Converter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string path = (string)value; if (!string.IsNullOrEmpty(path)) { return new BitmapImage(new Uri(path, UriKind.Absolute)); } else { return null; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } #endregion }
然后就是重要的轉(zhuǎn)換器:
public class StringToImageSourceConverter:IValueConverter { #region Converter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string path = (string)value; if (!string.IsNullOrEmpty(path)) { return new BitmapImage(new Uri(path, UriKind.Absolute)); } else { return null; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } #endregion }
轉(zhuǎn)換器返回的是Object類型,實(shí)際返回的是一個(gè)BitmapImage對象。所以我們在寫程序綁定的時(shí)候一定要弄清綁定的目標(biāo)和對象之間的關(guān)系,這個(gè)是非常重要的。
下面就是在ViewModel層中來添加綁定,并更新數(shù)據(jù)源,這里使用的是一個(gè)定時(shí)器來定時(shí)更新數(shù)據(jù)源:
public class LTEViewModel : NotifyObject { private DispatcherTimer myDispatcher = null; private Random random = new Random(); public LTEViewModel() { GetImageSource(); InitTimer(); } private LTEModel _lteModel = null; public LTEModel LTEModel { get { if (_lteModel == null) { _lteModel = new LTEModel(); } return _lteModel; } set { if (value != _lteModel) { _lteModel = value; FirePropertyChanged("LTEModel"); } } } private BaseModel _baseModel = null; public BaseModel BaseModelInstance { get { if (_baseModel == null) { _baseModel = new BaseModel() { Title = "分地區(qū)LTE分布", Time = DateTime.Now.ToString() }; } return _baseModel; } set { if (value != _baseModel) { _baseModel = value; FirePropertyChanged("BaseModelInstance"); } } } private ListimgList = new List (); private void GetImageSource() { //通過程序集來讀取相應(yīng)的資源的路徑 string assemblyLocation = this.GetType().Assembly.Location; string assLocation = assemblyLocation.Substring(0, assemblyLocation.LastIndexOf("\\")); string[] img_files = Directory.GetFiles(string.Format("{0}\\Images", assLocation), "*.JPG"); foreach (string img_path in img_files) { imgList.Add(img_path); } } private void InitTimer() { myDispatcher = new DispatcherTimer(); myDispatcher.Tick += new EventHandler(Timer_Tick); myDispatcher.Interval = TimeSpan.FromMilliseconds(1000); myDispatcher.Start(); } private void Timer_Tick(object sender, EventArgs e) { int imageIndex = 0; if (imgList.Count > 0 && LTEModel != null) { imageIndex = random.Next(0, imgList.Count); LTEModel.ImgSource = imgList[imageIndex]; } if (_baseModel != null) { _baseModel.Time = DateTime.Now.ToString(); } } }
然后就是實(shí)例化一個(gè)ViewModel對象綁定到前臺(tái)中,這個(gè)思路其實(shí)是相當(dāng)明確的。
其實(shí)在我們的很多時(shí)候,我們并不知道我們需要綁定什么圖片,或者說根據(jù)數(shù)據(jù)類型來綁定圖片,這個(gè)在定義數(shù)據(jù)模板的時(shí)候經(jīng)常使用到,下面就介紹一下,根據(jù)類型來綁定相應(yīng)的圖片。然后通過定義
public enum DeviceType { SheXiangJi, KaKou, DianZiJingCha, MingJin }
這種類型,通過不同的類型來綁定到不同的圖片,這個(gè)也是一個(gè)非常重要的應(yīng)用,我們一定要注意使用的方法,這里只是簡單介紹一下。
另外和Image很類似的就是
用法也差不多,同樣可以通過綁定的方式來添加圖片,不過在使用的時(shí)候還是需要注意一下就是設(shè)置當(dāng)前圖片的生成操作為Resource。
關(guān)于如何在C#中使用Image控件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。