[英]UWP (Universal Windows Platform) Web Service in Visual Studio 2015 C#
I'm pretty much new at StackOverflow and UWP. 我在StackOverflow和UWP上都是新手。 Ive being programming in C# for like 2 years almost but never did UWP before. 我已经在C#中编程差不多两年了,但之前从未做过UWP。
Since UWP doesn't admit database direct connection, I was trying to create a web service. 由于UWP不承认数据库直接连接,我试图创建一个Web服务。 Now, I being playing with the "Hello World" by default in Visual Studio´s web service file and trying to make a reference in the UWP app but I cant make a simple textblock to display what the method does in the web service (returns a "Hello World"). 现在,我默认在Visual Studio的Web服务文件中使用“Hello World”并尝试在UWP应用程序中进行引用但是我不能创建一个简单的文本块来显示该方法在Web服务中的作用(返回一个“你好世界”)。 So here is the code: 所以这是代码:
1) Web Service by default 1)默认为Web服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService
{
[WebService(Namespace = "http://tempuri.org/",Name ="Transfer")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
2) UWP App 2)UWP App
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace UWPWSApp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
WebServiceTransfer.TransferSoapClient tsc = new WebServiceTransfer.TransferSoapClient();
}
}
}
So the problem gets here, because I see a "HelloWorldAsync()", here is a screen: 所以问题就在这里,因为我看到了一个“HelloWorldAsync()”,这是一个屏幕:
I've already tried with the Async method but doesn't seem to be the one that displays "Hello World". 我已经尝试过使用Async方法,但似乎不是显示“Hello World”的方法。
First of all, tsc.HelloWorldAsync()
method is a asynchronous method: 首先, tsc.HelloWorldAsync()
方法是一个异步方法:
When you use this method, you will need use await operator, and you must include the "async" keyword in the method declaration in which you use this await operator. 使用此方法时,需要使用await运算符,并且必须在使用此await运算符的方法声明中包含“async”关键字。
public MainPage()
{
this.InitializeComponent();
}
This is constructor of MainPage, it initializes a new instance of the MainPage class. 这是MainPage的构造函数,它初始化MainPage类的新实例。 It's not a method and constructor can't not be used with await operator, so you can for example using the Loaded event here. 它不是方法,构造函数不能与await运算符一起使用,因此您可以在此处使用Loaded事件 。
Secondly, I saw your code like this: 其次,我看到你的代码是这样的:
textBlock.Text = tsc.HelloWorldAsync();
The Text
property of a TextBlock
should be a string, but tsc.HelloWorldAsync()
returns a HelloWorldResponse
type as you can see in my picture up, the type here is not match, so you can code it like this: TextBlock
的Text
属性应该是一个字符串,但是tsc.HelloWorldAsync()
返回一个HelloWorldResponse
类型,你可以在我的图片中看到,这里的类型不匹配,所以你可以这样编码:
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
WebServiceTransfer.TransferSoapClient tsc = new WebServiceTransfer.TransferSoapClient();
var response = await tsc.HelloWorldAsync();
textBlock.Text = response.Body.HelloWorldResult;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.