Windows Phone – Progress popup and AutoCompleteBox

Progress popup
During my development I would like to have a progress popup which shows a dialog telling the user that the program are on working process. The implementation below does not contain a progress bar if you want to can insert a progress bar and update it.

<Popup x:Name="ProgressPopup"
			Width="300"
			IsOpen="False" HorizontalAlignment="Center" VerticalAlignment="Top" d:LayoutOverrides="Width, HorizontalMargin" Margin="89,203,91,0">
	<Border BorderThickness="10"
				BorderBrush="Black"
				Background="DarkGray"
				Padding="30,30">
		<StackPanel>
			<TextBlock Foreground="White"
						FontWeight="Bold"
						FontSize="36"
						x:Name="txt"
						Text="Processing...">
						<TextBlock.Triggers>
							<EventTrigger RoutedEvent="TextBlock.Loaded">
								<BeginStoryboard>
									<Storyboard>
										<DoubleAnimation
											AutoReverse="True"
											Duration="0:0:1"
											From="1.0"
											RepeatBehavior="Forever"
											Storyboard.TargetName="txt"
											Storyboard.TargetProperty="Opacity"
											To="0.0"/>
									</Storyboard>
								</BeginStoryboard>
							</EventTrigger>
						</TextBlock.Triggers>
			</TextBlock>
		</StackPanel>
	</Border>
</Popup>

I also add an animation to make the opacity to 0.0 so that I can have a blinking text block. To call this popup, you can use following code to turn on and turn off popup.

this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = true));
this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = false));

AutoCompleteBox
The second component I would like to discuss in this post is AutoCompleteBox. If you are developers on Silverlight, you may know there is already a AutoCompleteBox control. However this control is not available on Windows Phone. So if you would like to use this control in Windows Phone, you can simply add reference to the library of this control. After installing Silverlight SDK, the library of AutoCompleteBox should be at “C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.Windows.Controls.Input.dll” (your installation path may vary from mine). Let’s add reference to add and add one instance in page

<phoneNavigation:PhoneApplicationPage
    ...
    xmlns:swci="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input"
    ...
    Loaded="PhoneApplicationPage_Loaded"
    >
...
<Grid x:Name="ContentGrid" Grid.Row="1">
	<swci:AutoCompleteBox x:Name="acbName" Margin="0,6,0,564"></swci:AutoCompleteBox>
</Grid>

Add database for AutoCompleteBox

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
	List<string> lstNames = new List<string>();
	lstNames.Add("Lewis");
	lstNames.Add("Whitcomb");
	lstNames.Add("Kadi");
	lstNames.Add("Woods");
	lstNames.Add("Shissler");
	lstNames.Add("Fleischer");
	lstNames.Add("Johnson");
	lstNames.Add("Schloen");
	lstNames.Add("Ritner");
	lstNames.Add("Pardee");
	acbName.ItemsSource = lstNames;
}

The AutoCompleteBox will work perfectly in Windows Phone

Windows Phone – Use REST WCF Service

Today when I play around with development in Windows Phone 7. I would like to check if I can access WCF REST Service which I discussed before in this post http://hintdesk.com/wcf-silverlight-access-wcf-rest-services-with-silverlight/ . To start with the development in Windows Phone 7 you need to download set of development tools here http://www.microsoft.com/downloads/details.aspx?FamilyID=2338b5d1-79d8-46af-b828-380b0f854203&displaylang=en . This set contains of
– Visual Studio 2010 Express for Windows Phone CTP
– Windows Phone Emulator CTP
– Silverlight for Windows Phone CTP
– XNA 4.0 Game Studio CTP
This packet is about 350 MB and can be installed parallel with Visual Studio 2008 SP1 and .Net Framework 3.5. When I try to move the example of the old post to Windows Phone 7, I think it must be very simple because the old example bases on Silverlight and Windows Phone 7 uses Silverlight as its default template and I am right. During the moving, I just need to copy and paste the code behind without needing to change anything. However some of components in normal Silverlight are not available, for example Label and DataGrid. The label can be easily replaced by a TextBlock but we need more work to make a control to replace DataGrid. I make a snapshot of available controls in image below

Continue reading Windows Phone – Use REST WCF Service

Windows Phone – Binding Converter

When I make a small application to read RSS feed on Windows Phone 7, I discover that in current version of windows phone 7 framework the attribute ValueConversionAttribute is not supported so I can not make a binding converter when working with XAML. Therefore I would like to make a reverse engineering to steal the source code of ValueConversionAttribute in .Net Framework 3.5 and use it in my application. Because I am lazy to invent a new example to demonstrate, I would like to use the example from this link http://learnwpf.com/Posts/Post.aspx?postId=05229e33-fcd4-44d5-9982-a002f2250a64 and adjust it so that it should work on Windows Phone 7 too.

Because the example is exactly same as the original one, I will tell which I changed to make it run on Windows Phone 7. First, the ObjectDataProvider is not provided in Windows Phone 7 therefore I must remove the DataContext of the ListBox and keep only databinding of ItemsSource and bind it with list of data objects

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
	ObservableCollection<SaleRecord> salesData = new ObservableCollection<SaleRecord>();
	salesData.Add(new SaleRecord(1001, System.DateTime.Now.Subtract(new TimeSpan(1, 1, 1)), 123.4m, 0.045f));
	salesData.Add(new SaleRecord(1002, System.DateTime.Now.Subtract(new TimeSpan(1, 0, 0)), 42m, 0.09f));
	salesData.Add(new SaleRecord(1003, System.DateTime.Now.Subtract(new TimeSpan(0, 58, 0)), 10.99m, 0.09f));

	lvData.ItemsSource = salesData;
}

and then remove the “sales” resource from resources section

<phoneNavigation:PhoneApplicationPage.Resources>
	<my:FormattingConverter x:Key="formatter" />
</phoneNavigation:PhoneApplicationPage.Resources>

As I told above, ValueConversionAttribute is not supported in Windows Phone 7. Therefore I must steal from .Net Framework 3.0 and it looks like following

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class ValueConversionAttribute : Attribute
{
	// Fields
	private Type _parameterType;
	private Type _sourceType;
	private Type _targetType;

	// Methods
	public ValueConversionAttribute(Type sourceType, Type targetType)
	{
		if (sourceType == null)
		{
			throw new ArgumentNullException("sourceType");
		}
		if (targetType == null)
		{
			throw new ArgumentNullException("targetType");
		}
		this._sourceType = sourceType;
		this._targetType = targetType;
	}

	public override int GetHashCode()
	{
		return (this._sourceType.GetHashCode() + this._targetType.GetHashCode());
	}

	// Properties
	public Type ParameterType
	{
		get
		{
			return this._parameterType;
		}
		set
		{
			this._parameterType = value;
		}
	}

	public Type SourceType
	{
		get
		{
			return this._sourceType;
		}
	}

	public Type TargetType
	{
		get
		{
			return this._targetType;
		}
	}
}

To use it just put it above your class

[ValueConversion(typeof(object), typeof(string))]
public class FormattingConverter : IValueConverter
{
	...
}

In the code of ValueConversionAttribute, I removed a override property. The absence of this property can cause program to crash (maybe). So use it on your risk and wait for a new version of framework for Windows Phone 7. The complete source code you can download here “Windows Phone Binding Converter

Windows Phone – Rss Reader software on Windows Phone emulator

This is the first software I write on Windows Phone 7. I have a lot of RSS feeds to read therefore I would like to have a RSS reader on Windows Phone 7 to manage all feeds(if I have one Windows Phone 7 in future ^^). This RSS reader provides some basic functions so that I can add, remove a feed. For each feed there are read and unread items. The unread items will be marked with Magenta, the read one will be marked with White. After finishing the beta version of this software, I would like to deploy it so that the others can use it on their emulator too but I find out until now there is no possibility to do that.

Continue reading Windows Phone – Rss Reader software on Windows Phone emulator

Windows Phone 7 – Simple database example

Today when I start to play around with developing on Windows Phone 7, I would like to write a first small database application because Windows Phone does not support SQL Server any more. Therefore I must use either LINQ over XML or store the database somewhere on server/Windows Azure and provide services so that the client can access and make query to get and update data. In this small example, I would like to work with LINQ To XML to create a database and update it.

My data object (data entity) is a class of Student with his first name, last name and email built by appending first name and last name as describing below

Continue reading Windows Phone 7 – Simple database example