nav-left cat-right
cat-right

Windows Phone 7 – Call Phone Number from Hyp...

When developing a Windows Phone 7 application with Silverlight, you may want to dial a phone number. To help you accomplish this, Microsoft provides the “Microsoft.Phone.Tasks” assembly. When you reference this component, you can do things like:

  • Compose an email (EmailComposeTask)
  • Prompt the user to choose an email address (EmailAddressChooserTask)
  • Send a text message (SmsComposeTask)
  • Perform a search (SearchTask)
  • Place a phone call (PhoneCallTask)

There are more options available in the Microsoft.Phone.Tasks namespace. Still, notice that each of these options ends with the word “Task”.  The term “Launcher” is also sometimes used. Regardless of the word, these items interact with the phone’s operating system to do the heavy lifting for you. When you begin a task, it is given the focus and your application gets sent to the background.  To demonstrate,  imagine that you wanted to call your favorite local restaurant through a HyperlinkButton.  As an example, we will add a HyperlinkButton to the inital XAML loaded by Visual Studio when you start a new Windows Phone 7 Silverlight application. The updated XAML will look like the following:

<phoneNavigation:PhoneApplicationPage
  x:Class="Sample.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;
  assembly=Microsoft.Phone.Controls.Navigation"
  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="800"
  FontFamily="{StaticResource PhoneFontFamilyNormal}"
  FontSize="{StaticResource PhoneFontSizeNormal}"
  Foreground="{StaticResource PhoneForegroundBrush}">

  <Grid x:Name="LayoutRoot"
    Background="{StaticResource PhoneBackgroundBrush}">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitleGrid is the name of the application and page title-->
    <Grid x:Name="TitleGrid" Grid.Row="0">
      <TextBlock Text="SAMPLE" x:Name="textBlockPageTitle"
        Style="{StaticResource PhoneTextPageTitle1Style}"/>
      <TextBlock Text="phonecalltask" x:Name="textBlockListTitle"
        Style="{StaticResource PhoneTextPageTitle2Style}"/>
    </Grid>

    <!--ContentGrid is empty. Place new content here-->
    <Grid x:Name="ContentGrid" Grid.Row="1">
      <HyperlinkButton x:Name="myHyperlinkButton" Content="Call Restaurant"
        FontSize="22" Click="myHyperlinkButton_Click" />
    </Grid>
</Grid>
</phoneNavigation:PhoneApplicationPage>

This XAML will generate an application that looks like the one shown to the right. To make it actually call the restaurant though, we will listen for the user to click the HyperlinkButton. When they click the link, we are going to execute the following code:

private void myHyperlinkButton_Click(
  object sender, RoutedEventArgs e)
{
  PhoneCallTask task =
    new PhoneCallTask();
  task.PhoneNumber = "123-456-7890";
  task.Show();
}

When this event handler is executed, the desired phone number (in this case “123-456-7890″) will be sent to the Windows Phone 7 dialer application. This application is launched when the Show() method is executed. This method is defined in the ITask interface, which is implemented by all of the tasks mentioned earlier. At the time of this post though, the Show() method is the only method available in this interface. Because of this fact, you cannot automatically dial a phone number at this time. Instead, the phone user will actually need to click the “call” button that is shown here:

Image 1
Dialer Application This is just a quick introduction to tasks within Silverlight for Windows Phone. The big thing to recognize is that tasks basically start up an application associated with the Phone OS and sends yours to the background. Either way, this post will be updated if the APIs are updated to allow automatic dialing from an application. I hope you found this post valuable. What else would you like to know about Windows Phone 7?

The “ValidateXaml” task failed unexpec...

So, you found that code example that helps you solve the problem you have been beating your head against the wall for. You most likely downloaded a .zip file and extracted its contents onto your local machine. You then open the solution in your favorite flavor of Visual Studio. When you attempt to compile the sample, you get a compile-time that reads something like :

Error    3    The “ValidateXaml” task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly ‘file:///C:\MyDirectory\MyApplication\MyApplication\Bin\Debug\Assembly.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0×80131515)
File name: ‘file:///C:\MyDirectory\MyApplication\MyApplication\Bin\Debug\Assembly.dll’ System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)

Whoa! This looked a bit intimidating the first time I saw it. The good news is, it’s an easy fix. The key information related to this error begins with the text “System.NotSupportedException“. What this basically goes on to say is that the offending component  has been blocked by Windows for security reasons. To overcome this, open Windows Explorer and navigate to the directory where the offending component exists. One there right click on the component and choose “Properties”. For the sake of a visualization, this is what this looks like:

Properties

When you click the “Properties” option, a dialog window will appear. Towards the bottom of this dialog, you will see the “security” information which says: “This file came from another computer and might be blocked to help protect this computer”. To the right of this text is a button that says “Unblock”. This is the button we are looking for as highlighted here:

Security

Simply click the “Unblock” button and rebuild your application. You may have to do this several times if there are multiple assemblies that were downloaded along with the code sample. Regardless, I hope you have found this post valuable. If you would like to learn more about developing applications with Silverlight, I highly recommend Silverlight in Action by Pete Brown. The first version of this book series was written by Chad Campbell (me) and John Stockton.