Windows Phone TextBlock content related task launch.

Recently one of my friends asked some help to solve an easy problem. His app has a few TextBlock, each text block contains a phone number or an e-mail address. He wanted to launch phone call or e-mail based on TextBlock’s text.
The solution is easy, attach DoubleTap event to the TextBlock:

<StackPanel>
    <TextBlock DoubleTap="TextBlock_DoubleTap_1" >example@test.com</TextBlock>
    <TextBlock DoubleTap="TextBlock_DoubleTap_1">+123456789</TextBlock>
</StackPanel>

Then add a few line of code inside the created event handler:

if (sender is TextBlock)
{
    string content = ((TextBlock)sender).Text;
    if (!string.IsNullOrEmpty(content))
    {
        if (content.Contains("@"))
        {
            EmailComposeTask ect = new EmailComposeTask();
            ect.To = content;
            ect.Subject = "Greetings!";
            ect.Body = "Message from my application";
            ect.Show();
        }
        else
        {
            PhoneCallTask pct = new PhoneCallTask();
            pct.PhoneNumber = content;
            // pct.DisplayName = "Call from application";
            pct.Show();                        
        } 
    }

    e.Handled = true;
}
else
    e.Handled = false;

Well, the validation is bit dummy, but will serve the educational purpose.

Advertisements