繁体   English   中英

Visual Studio 2015中的UWP(通用Windows平台)Web服务C#

[英]UWP (Universal Windows Platform) Web Service in Visual Studio 2015 C#

我在StackOverflow和UWP上都是新手。 我已经在C#中编程差不多两年了,但之前从未做过UWP。

由于UWP不承认数据库直接连接,我试图创建一个Web服务。 现在,我默认在Visual Studio的Web服务文件中使用“Hello World”并尝试在UWP应用程序中进行引用但是我不能创建一个简单的文本块来显示该方法在Web服务中的作用(返回一个“你好世界”)。 所以这是代码:

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

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();
        }
    }
}

所以问题就在这里,因为我看到了一个“HelloWorldAsync()”,这是一个屏幕:

抓屏

我已经尝试过使用Async方法,但似乎不是显示“Hello World”的方法。

首先, tsc.HelloWorldAsync()方法是一个异步方法:

在此输入图像描述

使用此方法时,需要使用await运算符,并且必须在使用此await运算符的方法声明中包含“async”关键字。

public MainPage()
{
    this.InitializeComponent();
}

这是MainPage的构造函数,它初始化MainPage类的新实例。 它不是方法,构造函数不能与await运算符一起使用,因此您可以在此处使用Loaded事件

其次,我看到你的代码是这样的:

textBlock.Text = tsc.HelloWorldAsync();

TextBlockText属性应该是一个字符串,但是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM