No more annual renewal fee for Windows developer accounts

Standard

Windows Dev Center requires only a one-time registration payment, which grants developers the ability to submit apps to both the Windows Store and Windows Phone Store, with no annual renewal fee.

Lifetime Dev Center Subscriptions

Lifetime Dev Center Subscriptions

And this is applicable for the existing developers as well. You can find more details about it from Account subscription management page.

Account subscription management

Account subscription management

You can get more details from Windows Blog

How to change the startup page on a WP7 application

Standard

Sometimes you may need to change the startup page of Windows Phone application. By default it will be pointing to MainPage.xaml. You can modify the WMAppManifest.xml to change the default page. In the WMAppManifest.xml you will find the following.(You can get the WMAppManifest.xml by expanding the properties node in the Project Explorer window.)

<Tasks>
  <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>

Happy Programming :)

How to handle hardware back button in Windows Phone

Standard

If you want to display an exit confirmation from your Windows Phone app, if user press the hardware back button, here is the code snippet. (It is not a recommended practice, as per Windows Phone market place certification requirements, if user pressing the back button from the application’s first page, application should exit)

protected override void OnBackKeyPress(CancelEventArgs e)
{
    if (MessageBox.Show("Are you sure want to exit?", "Confirm", 
        MessageBoxButton.OKCancel) != MessageBoxResult.OK)
    {
        e.Cancel = true;
    }

    base.OnBackKeyPress(e);
}

It will display a message box like this, if user press the back button.

WP7 Exit confirmation

WP7 Exit confirmation

Clicking on OK will exit the application.

Happy Coding.

How to implement text to speech in WP7 using BING API

Standard

In WP8, Microsoft introduced libraries for both Text to Speech and Speech to Text without an active internet connection. But this feature is not available in WP7. I found some article implementing Text to Speech using BING API, but seems it is no more working. Here is one implementation using BING REST API, which helps to integrate text to speech in in you WP7 applications.

The following code requires Client Id and Client Secret, which you can get from https://datamarket.azure.com/account, Customer ID is the Client Id, Primary Account Key is Client Secret. The Text to Speech work flow is like this.

Bing Text to Speech - Workflow

The Auth token will expire after 10 minutes, so you need to renew it on every 10 minutes.

And here is code snippet.

public class Speech
{
    private const string DatamarketAccessUri 
        = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
    private const int RenewDuration = 9;
    private AccessToken _bingAccessToken;
    private string _clientId;
    private string _clientSecret;
    private Timer _tokenRenewTimer;

    public Speech(string clientId, string clientSecret)
    {
        _clientId = clientId;
        _clientSecret = clientSecret;
        _tokenRenewTimer = new Timer(OnTokenExpiredCallback,
            this, TimeSpan.FromMinutes(RenewDuration), TimeSpan.FromMilliseconds(-1));
    }

    private void OnTokenExpiredCallback(object stateInfo)
    {
        InitOrRefresh();
    }

    public void Initialize()
    {
        InitOrRefresh();
    }

    private void InitOrRefresh()
    {
        var requestData = 
            string.Format(@"grant_type=client_credentials&client_id={0}
            &client_secret={1}&scope=http://api.microsofttranslator.com",
            HttpUtility.UrlEncode(_clientId), HttpUtility.UrlEncode(_clientSecret));
        var webClient = new WebClient();
        webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        webClient.Credentials = new NetworkCredential(_clientId, _clientSecret);
        webClient.UploadStringCompleted += WebClientUploadStringCompleted;
        webClient.UploadStringAsync(new Uri(DatamarketAccessUri), "POST", requestData);
    }

    private void WebClientUploadStringCompleted
        (object sender, UploadStringCompletedEventArgs e)
    {
        var serializer = new DataContractJsonSerializer(typeof(AccessToken));
        using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result)))
        {
            _bingAccessToken = serializer.ReadObject(memoryStream) as AccessToken;
        }
    }

    public void Speak(string text)
    {
        var SpeakUri = 
            string.Format(@"http://api.microsofttranslator.com/v2/Http.svc/Speak?
            text={0}&language={1}&format={2}&options={3}",
            text, "en", HttpUtility.UrlEncode("audio/wav"), "MinSize");
        var webClient = new WebClient();
        webClient.Headers["Authorization"] = 
            string.Format("Bearer {0}", _bingAccessToken.Accesstoken);
        webClient.OpenReadCompleted += WebClientOpenReadCompleted;
        webClient.OpenReadAsync(new Uri(SpeakUri));
    }

    private void WebClientOpenReadCompleted
        (object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            var soundEffect = SoundEffect.FromStream(e.Result);
            var soundInstance = soundEffect.CreateInstance();
            FrameworkDispatcher.Update();
            soundInstance.Play();
        }
    }
}

And here is the Access Token class.

[DataContract]
public class AccessToken
{
    [DataMember(Name = "access_token")]
    public string Accesstoken { get; set; }
    [DataMember(Name = "token_type")]
    public string TokenType { get; set; }
    [DataMember(Name = "expires_in")]
    public string ExpiresIn { get; set; }
    [DataMember(Name = "scope")]
    public string Scope { get; set; }
}

And you can consume the code like this in WP7.

public partial class MainPage : PhoneApplicationPage
{
    private Speech _speech;
    public MainPage()
    {
        InitializeComponent();
        _speech = new Speech("CustomerId", "Primary Account Key");
        Dispatcher.BeginInvoke(_speech.Initialize);
    }

    private void SpeakButton_Click(object sender, RoutedEventArgs e)
    {
        _speech.Speak(SpeakText.Text);
    }
}

You can use this code in Winforms/Console application also, but you may need to change WebClientOpenReadCompleted() method. You may need to use the SoundPlayer class instead of using XNA Framework classes.

Happy Programming