Usman ur Rehman Ahmed's blog

Windows Phone 7 ResolveHostNameAsync

The class DeviceNetworkInformation packaged within namespace Microsoft.Phone.Net.NetworkInformation provides two overloaded methods to resolve hostname asynchronously.

The overloads differ such that former allows you to specify the network interface using which hostname needs be resolved where as earlier will use the first avialable network interface in the list (If both WiFi and Ethernet are available then my understanding is that Ethernet will take precedence)

Here is a simple app demonstrating how to retrieve IPs against hostname.

Resolve-hostname
Note: Since call to method (ResolveHostNameAsync) is asynchronous in nature, the foreground thread won’t let you interact with the UI interface in the callback method  nameResolutionCallback(NameResolutionResult result). That's why you will see that interaction with UI is managed by call to dispatcher. However a more sophisticated approach will be to execute this call on the background thread.

Code behind

        private void btnResolveHostName_Click(object sender, RoutedEventArgs e)
        {
            if(!string.IsNullOrEmpty(txtHostName.Text))
            {
                DnsEndPoint endPoint = new DnsEndPoint(txtHostName.Text, 80);
                Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.ResolveHostNameAsync(endPoint, nameResolutionCallback, new object());
            }
        }

        public void nameResolutionCallback(NameResolutionResult result)
        {
            if (result.NetworkErrorCode == NetworkError.Success)
            {
                List<string> items = new List<string>();
                items.Add(string.Format("Network Interface Type : {0}", result.NetworkInterface.InterfaceType));

                foreach (IPEndPoint ipEndPoint in result.IPEndPoints)
                {
                    items.Add(string.Format("Resvoeld IP : {0}:{1}", ipEndPoint.Address.ToString(), ipEndPoint.Port.ToString()));
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    lstInfo.ItemsSource = items;
                    txtHostName.Focus();
                });
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(result.NetworkErrorCode.ToString(), "Error", MessageBoxButton.OK);
                    txtHostName.Focus();
                });
            }
        }

XAML

    <!--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="Resolve Hostname" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Name="txtHostName" Width="300" Margin="6,-5,150,623" Text="google.com"></TextBox>
            <Button Name="btnResolveHostName" Margin="300,-5,-5,623" Click="btnResolveHostName_Click">Resolve</Button>
            <ListBox Name="lstInfo" Margin="12,79,6,39" BorderThickness="1" BorderBrush="Azure"></ListBox>
        </Grid>
        </Grid>

 

Text Styles for Windows Phone 7

In Windows Phone 7 it is extremelly simple to achieve mutiple levels (nested text items) of display by means of a List Box. This is often a requirements and such a scenario is shown below,

Nested-list-box
The trick here is to set style of each TextBlock control uniquely and set each item style exclusively.  Following eleven styles are possible to be applied on TextBlock using {StaticResources} markup extension as part of Windows Phone 7 default Text Styles (Theme resoruces),

Various-text-block-styles
XAML Code

                    <ListBox x:Name="SearchListBox" Margin="0,112,-12,0" Height="522">

                        <TextBlock Text="Item 1" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextBlockBase}"/>

                        <TextBlock Text="Item 2" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>

                        <TextBlock Text="Item 3" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextTitle1Style}"/>

                        <TextBlock Text="Item 4" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextTitle2Style}"/>

                        <TextBlock Text="Item 5" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextTitle3Style}"/>

                        <TextBlock Text="Item 6" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextLargeStyle}"/>

                        <TextBlock Text="Item 7" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

                        <TextBlock Text="Item 8" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextGroupHeaderStyle}"/>

                        <TextBlock Text="Item 9" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSmallStyle}"/>

                        <TextBlock Text="Item 10" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextContrastStyle}"/>

                        <TextBlock Text="Item11" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextAccentStyle}"/>

                    </ListBox>

Ref: http://msdn.microsoft.com/en-us/library/ff769552%28v=vs.92%29.aspx

(See Text Styles section)

11
To Posterous, Love Metalab