سؤال

Previously there the bing translator was easily accessible with the SOAP interface. Now it has been migrated to Windows Azure. I have registered in the Azure marketplace for 10000 letters per month (free). How can I translate text through the translator api, for windows phone in C#? Please help. I am not sure how to use the BeginExecute and EndExecute for queries.

I have downloaded and added the TranslatorContainer.cs to my project. For now I am just trying to get the Languages with the GetLanguagesForTranslation method. This is the code which I have written.

public partial class PhonePage1 : PhoneApplicationPage
{

    public PhonePage1()
    {
        InitializeComponent();

        Translator transInstance = new Translator();

    }
    class Translator
    {
        private Uri service_root;
        private TranslatorContainer context;

        public Translator()
        {
            service_root = new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/");
            context = new TranslatorContainer(service_root);
            context.Credentials = new NetworkCredential("ID","...........");
            var query = context.GetLanguagesForTranslation();
            query.BeginExecute(OnQueryComplete, query);
        }

        public void OnQueryComplete(IAsyncResult result)
        {
            var query = result as DataServiceQuery<Language>;

            string langstring = "";
            foreach (Language lang in query.EndExecute(result))
            {
                langstring += lang.Code + "\n";
            }
            MessageBox.Show(langstring);
        }
    }
}

In OnQueryComplete() the query is null even after the assignment. The result has the Properties IsCompleted as true, and statusCode is OK. I am not able to figure out how to go about this. Please help. Thank you

هل كانت مفيدة؟

المحلول

With help from Bing Translator team I got it working in my Silverlight Application:

  1. UseDefaultCredentials needs to be turned off on the proxy

  2. On the async callback, you were casting the result to a DSQ, but it’s the result’s AsyncState that needs to be casted. See below.

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var serviceUri = new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/");
        var accountKey = "**********************"; // 
        var tcode = new Microsoft.TranslatorContainer(serviceUri);
    
        tcode.Credentials = new NetworkCredential(accountKey, accountKey);
        tcode.UseDefaultCredentials = false;
        var query = tcode.GetLanguagesForTranslation();
        query.BeginExecute(OnQueryComplete, query);
    }
    
    public void OnQueryComplete(IAsyncResult result)
    {
        var query = (DataServiceQuery<Microsoft.Language>)result.AsyncState;
        var enumerableLanguages = query.EndExecute(result);
        string langstring = "";
        foreach (Microsoft.Language lang in enumerableLanguages)
        {
            langstring += lang.Code + "\n";
        }
        MessageBox.Show(langstring);
    }
    

This way you can use BeginExecute() and BeginEnd() to get Async results.

نصائح أخرى

I had exact same problem and I was suggested that the issue may be related with the how the Async results are return internally when calling GetLanguagesForTranslation, however I did not dig further and just used Execute() to get the list of Language as below:

var serviceUri = new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/");
var accountKey = "***********************"; // 
var tcode = new TranslatorContainer(serviceUri);
tcode.Credentials = new NetworkCredential(accountKey, accountKey);
var languages = tcode.GetLanguagesForTranslation().Execute().ToArray(); 
foreach (var i in languages)
{
    Console.WriteLine(i.Code);
}

Not sure if that is what you are looking for but it worked in my case well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top