简体   繁体   English

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

[英]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()”,这是一个屏幕:

ScreenCapture 抓屏

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: 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.

相关问题 将通用Windows C#类库引用添加到通用Windows C ++ DLL项目Visual Studio 2015 - Adding a Universal Windows C# Class Library reference to a Universal Windows C++ DLL project Visual Studio 2015 通用Windows平台(UWP)应用程序的C#服务器/客户端应用程序 - C# Server / Client Application for Universal Windows Platform (UWP) apps Visual Studio 2015 - C#Windows Universal App缺少程序集引用 - Visual Studio 2015 - C# Windows Universal App missing assembly reference 尝试在Visual Studio 2015,c#,通用Windows应用程序中删除SQLite数据库中的条目,没有错误,但没有删除 - Trying to delete entries from SQLite db in Visual Studio 2015, c#, Universal Windows App, no errors, but not deletes 创建和使用Web服务以使用Visual Studio 2015和C#调用方法 - Creating and consuming a web service to call a method with Visual Studio 2015 and c# 通用Windows平台(UWP)C#应用程序-如何在应用程序中运行独立的python - Universal Windows Platform (UWP) C# app - How to run stand alone python inside your application C#、Visual Studio 2015、 - C#, Visual Studio 2015, 在Visual Studio 2015中是否有ac#windows form应用程序? - Is there a c# windows form Application in visual Studio 2015? Visual Studio 2015-新的Blank Universal App C#的存储菜单已禁用/丢失 - Visual Studio 2015 - Store menu disabled/missing for new Blank Universal App C# 永久加载Visual Studio 2015通用Windows XAML设计器表面 - Visual Studio 2015 Universal Windows XAML designer surface loading forever
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM