如何在Windows Phone 7中调用webservice方法?

3

我写了以下代码来连接Web服务。

WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://www.Webservices.asmx"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);

void wc_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
{
    Debug.WriteLine("Web service says: " + e.Result);
    using (var reader = new StringReader(e.Result))
    {
        String str = reader.ReadToEnd();
    }
}

通过使用上述代码,可以获取字符串结果。但我想在HTMLVisualizer中获取结果,以便我知道有哪些方法可以使用该Web服务,然后我就可以轻松访问特定的方法。请告诉我如何在Windows Phone 7中调用Web服务方法?在Web服务中,我有5个Web方法。如何获取并调用特定的Web方法?感谢您提前告诉我。

1
可能是如何在Windows Phone 7中连接XML Web服务?的重复问题。 - Richard Szalay
2个回答

5
@venkateswara,您是在谈论获取已知WebReference方法列表以便在代码中调用哪一个吗?当您将WebReference添加到您的WP7项目中时,您看不到已知方法调用的内容吗?由于您将在VS中开发WP7应用程序,我无法理解您为什么要这样做。即使您没有拥有webservice,您也需要从VS连接到它以便将其引用添加到您的项目中。
在VS2010中添加WebReference的屏幕如下所示,右侧列出了操作enter image description here 添加后,您可以使用ObjectBrowser了解应该如何调用方法。
如果我漏掉了您问题的任何内容,请告诉我。

嗨,Jason,我做了相同的步骤。在调用方法时,我在对象资源管理器中没有找到该方法,但是在添加引用时我看到了该方法。该方法名为“Genericmethod()”,但在对象资源管理器中我看到的是“GenericWebMethodAsync()”。我在对象资源管理器中没有找到我的方法“Genericmethod()”,它返回数据集。请告诉我我做错了什么。 - Sravanti
1
我相信在WP7中,你只能异步调用方法,因此当你添加引用时生成的代理仅创建异步调用。这就是为什么你只能在对象浏览器中看到xxxxxAsync()方法。希望这可以帮助你。 - Jason James

2

@Jason James

第一步:

您必须添加参考服务,就像Jason James提供的非常详细的说明一样。

第二步:

您可以打开Functions应用程序中的App.xaml.cs文件。

public Apps()
{
    // Global handler for uncaught exceptions. 
            UnhandledException += Application_UnhandledException;

            // Show graphics profiling information while debugging.
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // Display the current frame rate counters.
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // Show the areas of the app that are being redrawn in each frame.
                //Application.Current.Host.Settings.EnableRedrawRegions = true;

                // Enable non-production analysis visualization mode, 
                // which shows areas of a page that are being GPU accelerated with a colored overlay.
                //Application.Current.Host.Settings.EnableCacheVisualization = true;
             }
           // You can declare objects here that you will use
           //Examlpe: NameservicesReferent.(Function that returns services) = new NameservicesReferent.(Function that returns services)();

Ws_Function = 新的Nameservices.ServiceSoapClient();

}

步骤三:
在Mainpage.xaml.cs文件中。
GlobalVariables.Ws_advertise.getLinkAdvertiseIndexCompleted += new EventHandler<advertise.getLinkAdvertiseIndexCompletedEventArgs>(Ws_advertise_getLinkAdvertiseIndexCompleted);
GlobalVariables.***NameWedservise***.getLinkAdvertiseIndexAsync("**parameters to be passed**");

第四步:

void Ws_advertise_getLinkAdvertiseIndexCompleted(object sender, advertise.getLinkAdvertiseIndexCompletedEventArgs e)
        {
             //function returns the results to you, the example here is an array
           string[] array = null;
           try

           {
              array = e.result;
              if(array != null)

           }
           cath(exception ex)
           {

           }
           finally
           {
             array = null;
GlobalVariables.Ws_advertise.getLinkAdvertiseIndexCompleted -= new EventHandler<advertise.getLinkAdvertiseIndexCompletedEventArgs>(Ws_advertise_getLinkAdvertiseIndexCompleted);
           }
         }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接