using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Browser;
using System.Threading;
using System.Windows.Media.Imaging;
namespace UsingThreadpoolInSilverlight2
{
public partial class Page : UserControl
{
private const int MAX_IMAGES = 5;
private delegate void NewImagesHandler(int imageIndex);
// Constructor
public Page()
{
InitializeComponent();
// assign events to the button clicks
btnStart.Click += new RoutedEventHandler(BtnStart_Click);
for (int i = 1; i <= MAX_IMAGES; i++)
{
Button btn = (Button)this.LayoutRoot.FindName("btn" + i.ToString());
btn.Click += new RoutedEventHandler(Btn_Click);
}
}
// Click event handlers for buttons and images
private void Btn_Click(object sender, RoutedEventArgs e)
{
ResetImage();
Button btn = (Button)sender;
string imageIndex = btn.Name.Substring(3, 1);
Image img = (Image)imagePlaceholder.FindName("img" + imageIndex);
img.Visibility = Visibility.Visible;
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
ResetControls();
LoadResources();
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
string browserWindowOptions = "";
HtmlPage.Window.Navigate(new Uri("http://www.google.com"), "_blank", browserWindowOptions);
}
// Set visibility of all the images to collapsed
private void ResetImage()
{
for (int i = 1; i <= this.imagePlaceholder.Children.Count; i++)
{
Image img = (Image)this.LayoutRoot.FindName("img" + i.ToString());
img.Visibility = Visibility.Collapsed;
}
}
private void ResetControls()
{
while (imagePlaceholder.Children.Count > 0)
{
imagePlaceholder.Children.RemoveAt(0);
}
txtStatus.Text = "";
for (int i = 1; i <= MAX_IMAGES; i++)
{
Button btn = (Button)this.LayoutRoot.FindName("btn" + i.ToString());
btn.IsEnabled = false;
}
}
// Used for loading images in background in parallel
private void LoadResources()
{
for (int i = 1; i <= MAX_IMAGES; i++)
{
int imageIndex = i;
// Load rest of the images in background
ThreadPool.QueueUserWorkItem(delegate(object notUsed)
{
LoadImage(imageIndex);
});
}
}
private void LoadImage(int imageIndex)
{
// Simulate some delay
System.Threading.Thread.Sleep(imageIndex * 1000);
// Update UI
NewImagesHandler newImageHandler = new NewImagesHandler(AddNewImage);
object[] args = { imageIndex };
this.Dispatcher.BeginInvoke(newImageHandler, args);
}
private void AddNewImage(int imageIndex)
{
// Find text box. Assumes text box is named txt[1..n]
TextBox txt = (TextBox)this.LayoutRoot.FindName("txt" + imageIndex.ToString());
// Get image url from text box
string url = txt.Text.Contains("http://") ? txt.Text : "http://" + txt.Text;
Uri imageUrl = new Uri(url);
// Show only first image visible
Visibility isImageVisible = imageIndex.Equals(1) ? Visibility.Visible : Visibility.Collapsed;
// Add images to placeholder
Image image = new Image();
image.Name = "img" + imageIndex;
image.Width = 400;
image.Height = 137;
image.Stretch = Stretch.Uniform;
image.Source = new BitmapImage(imageUrl);
image.MouseLeftButtonDown += new MouseButtonEventHandler(Image_MouseLeftButtonDown);
image.Visibility = isImageVisible;
imagePlaceholder.Children.Add(image);
// Find Button. Assumes Button is named btn[1..n]
// Then Enable button
Button btn = (Button)this.LayoutRoot.FindName("btn" + imageIndex.ToString());
btn.IsEnabled = true;
// Update Status to Status Label
txtStatus.Text += "Loaded" + Environment.NewLine;
}
}
}
|