如何使用微软翻译API过的Windows Azure,Windows Phone的?
previously那里的必应翻译是与SOAP接口方便。现在,它已被迁移到Windows Azure。我已经注册了Azure的市场上为每月(免费)10000号。我怎么能翻译成C#通过翻译API文本,为Windows手机?请帮忙。我不知道如何使用BeginExecute和EndExecute进行查询。
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.
我已经下载并添加TranslatorContainer.cs到我的项目。现在我只是想用GetLanguagesForTranslation方法来获取语言。这是我写的code。
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);
}
}
}
在OnQueryComplete()的查询
即使在转让之后为空。结果有属性IsCompleted为真,状态code是确定。
我无法弄清楚如何去这件事。请帮忙。
谢谢
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
与必应翻译团队帮助我得到了它在我的Silverlight应用程序的工作:
With help from Bing Translator team I got it working in my Silverlight Application:
-
UseDefaultCredentials需要关闭在代理
UseDefaultCredentials needs to be turned off on the proxy
在异步回调,你是铸造,将得到的DSQ,但它的结果的AsyncState需要进行铸造。见下文。
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);
}
此方式,可以使用BeginExecute()和BeginEnd()来获得异步的结果。
This way you can use BeginExecute() and BeginEnd() to get Async results.