Click here to Skip to main content
15,886,664 members
Articles / Mobile Apps / Windows Phone 7

Exposing asynchronous features to client code: Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
16 Jan 2011CPOL3 min read 41.2K   639   15  
How to use types implementing the IAsyncResult interface on Windows Phone 7.
<phone:PhoneApplicationPage
    x:Class="BonusBits.CodeSamples.WP7.ThreadingPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:local="clr-namespace:BonusBits.CodeSamples.WP7.Design.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignWidth="480"
    d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"
    Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <local:BooleanToVisibilityConverter
            x:Key="booleanToVisibilityConverter" />
    </phone:PhoneApplicationPage.Resources>
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid
        x:Name="LayoutRoot"
        Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="*" />
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel
            x:Name="TitlePanel"
            Grid.Row="0"
            Margin="12,17,0,28">
            <TextBlock
                x:Name="ApplicationTitle"
                Text="BonusBits.CodeSamples.WP7"
                Style="{StaticResource PhoneTextNormalStyle}" />
            <TextBlock
                x:Name="PageTitle"
                Text="Threading"
                Margin="9,-7,0,0"
                Style="{StaticResource PhoneTextTitle1Style}" />
        </StackPanel>
        <!--ContentPanel - place additional content here-->
        <Grid
            x:Name="ContentPanel"
            Grid.Row="1"
            Margin="12,0,12,0">
            <Button
                IsEnabled="{Binding Path=CanStart}"
                Height="72"
                HorizontalAlignment="Left"
                Margin="0,0,0,6"
                Name="Start"
                VerticalAlignment="Bottom"
                Width="160">
                <Button.Content>
                    <StackPanel
                        Orientation="Horizontal">
                        <TextBlock
                            Text="Start"
                            Margin="0,0,15,0" />
                        <Rectangle
                            Name="rectangle"
                            Fill="Gray"
                            Visibility="{Binding Path=CanExecuteWithSyncIO,
                                            Converter={StaticResource booleanToVisibilityConverter},
                                            ConverterParameter=!}"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Width="15"
                            Height="15">
                            <Rectangle.RenderTransform>
                                <RotateTransform
                                    Angle="0"
                                    CenterX="7.5"
                                    CenterY="7.5" />
                            </Rectangle.RenderTransform>
                            <Rectangle.Triggers>
                                <EventTrigger
                                    RoutedEvent="Rectangle.Loaded">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                                Storyboard.TargetName="rectangle"
                                                Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
                                                From="0"
                                                To="360"
                                                Duration="0:0:5"
                                                RepeatBehavior="Forever" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Rectangle.Triggers>
                        </Rectangle>
                    </StackPanel>
                </Button.Content>
            </Button>
            <RadioButton
                Content="Sync I/O"
                Height="72"
                HorizontalAlignment="Left"
                Margin="12,6,0,0"
                Name="CanExecuteWithSyncIO"
                VerticalAlignment="Top" />
            <RadioButton
                Content="Delegate's BeginInvoke"
                Height="72"
                HorizontalAlignment="Left"
                Margin="12,84,0,0"
                Name="CanExecuteWithDelegateBeginInvoke"
                VerticalAlignment="Top" />
            <RadioButton
                Content="IAsyncResult"
                Height="72"
                HorizontalAlignment="Left"
                Margin="12,0,0,373"
                Name="CanExecuteWithIAsyncResult"
                VerticalAlignment="Bottom" />
            <RadioButton
                Content="AsyncEnumerator"
                Height="72"
                HorizontalAlignment="Left"
                Margin="13,240,0,0"
                Name="CanExecuteWithAsyncEnumerator"
                VerticalAlignment="Top"
                IsChecked="True" />
            <TextBlock
                Height="34"
                HorizontalAlignment="Left"
                Margin="161,547,0,0"
                Name="Status"
                Text="{Binding Path=Status}"
                VerticalAlignment="Top"
                Width="276"
                TextWrapping="Wrap" />
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions