Previous Tip  |  Next Tip  |  Index (recent)   |  Design Tips   | [Bill's Home]

305. My first XAML design

I've been a big fan of .NET since 1.1, and every new version brings amazing features. The new .NET 3.0 is one of the best so far, with amazing effects in a few lines of code [Click here for Presentation 1][Click here for Presentation 2].

Note: To view these your need the .NET 3.0 framework installed [Click here for install]

It uses XAML for its graphics and animation, for example the PictureViewer starts with:

and then:

and then:

All done with these lines of C# code:

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace PhotoBook
{
public partial class PhotoBookViewer
{
public PhotoBookViewer()
{
this.InitializeComponent();
}

private void OnExit(object sender, RoutedEventArgs e)
{
this.Close();
}
}

}

and:

using System.Collections.ObjectModel;
using System;
using System.IO;
using System.Windows;

namespace PhotoBook
{
/// <summary>
/// Data Model that holds the images of the pages.
/// </summary>
public class ImageCollection
{
private ObservableCollection<string> images = new ObservableCollection<string>();
private string location;

public ImageCollection()
{
}

#region Public Attributes

public ObservableCollection<string> Images
{
get { return this.images; }
}

public string Location
{
get { return this.location; }
set
{
this.location = value;
this.Reload();
}
}

public string FirstImage
{
get
{
if (this.Images.Count != 0)
{
return this.Images[0];
}
else
{
return "";
}
}
}

#endregion

private void Reload()
{
this.images.Clear();

if (Directory.Exists(this.location))
{
DirectoryInfo dir = new DirectoryInfo(this.location);
foreach (FileInfo file in dir.GetFiles("*.*", SearchOption.AllDirectories))
{
if (this.IsImage(file))
this.images.Add(file.FullName);
}
}
}

private bool IsImage(FileInfo file)
{
String ext = file.Extension.ToLower();
if (ext == ".jpg" || ext == ".gif" || ext == ".png")
return true;
return false;
}
}
}