我有一個自定義的UserControl和一個Image控件。我試圖從web [網絡服務器]獲取圖像,並在我的控件中顯示它,使用調度程序計時器刷新源。下面是代碼:獲取Web圖像並在圖像控件中顯示時間間隔後 - 不工作
void StartSourceRefresh()
{
if (timeinterval < 1) timeinterval = 1;
tmrRefresh.Tick += new EventHandler(dispatcherTimer_Tick);
tmrRefresh.Interval = new TimeSpan(0, 0, timeinterval); //in hour-minute-second
tmrRefresh.Start();
}
public void ChangeImageSource(string newSource)
{
//newSource = "http://192.168.1.3/abc/imagetobeshown.png"
WebImg.Source = null;
if (newSource.Trim() == "")
WebImg.Source = new BitmapImage(new Uri(@imagePlaceholder, UriKind.Absolute));
else
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(@newSource, UriKind.Absolute);
image.EndInit();
WebImg.Source = image;
}
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
ChangeImageSource(txtImgSrc.Text.Trim());
}
的問題是圖像不會改變。它展示了第一次獲得的同一個。計時器運行良好。但圖像不會改變。我在這裏做錯了什麼?
編輯:網絡源獲取一定的時間間隔後刷新,所以必須獲取相同的源
參見[?是什麼在C#中的變量名@字符的使用/含義](https://stackoverflow.com/questions/91817/whats-的使用含義的最字符合可變名稱功能於c)中。 – Clemens