Web服务asmx Xamarin.Form不起作用

时间:2017-05-16 10:18:00

标签: c# .net visual-studio web-services xamarin.forms

我的应用程序Android在执行我的代码时显示此错误:“对象引用未设置对象的实例”。 好像“e.Result”中没有任何内容。但是,我的网络服务运作良好

 public MainPage()
        {
            InitializeComponent();


            Please.GardaSoapClient Ws = new Please.GardaSoapClient(new BasicHttpBinding(),
                        new EndpointAddress("http://webgarda20170508110006.azurewebsites.net/Garda.asmx"));
            Ws.HelloCompleted += Ws_HelloCompleted;
            Ws.HelloAsync();

            }

        private void Ws_HelloCompleted(object sender, Please.HelloCompletedEventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () => {
                    string error = null;
                     if (e.Error != null)
                         error = e.Error.Message;
                     else if (e.Cancelled)
                         error = "Cancelled";

                     if (!string.IsNullOrEmpty(error))
                     {
                         await DisplayAlert("Error", error, "OK", "Cancel"); **//error => "Object reference not set an instance of an object"**
                     }
                     else 
             `enter code here`       {

                test.Text = e.Result;
               }
            });
        }
    } 
你能帮帮我吗? :) 感谢

1 个答案:

答案 0 :(得分:0)

由于目标方法是事件处理程序,因此您可以利用异步事件处理程序将事件arg e保留在范围内。

private async void Ws_HelloCompleted(object sender, Please.HelloCompletedEventArgs e) {
    string error = null;
    if (e.Error != null)
        error = e.Error.Message;
    else if (e.Cancelled)
        error = "Cancelled";

    if (!string.IsNullOrEmpty(error)) {
        await DisplayAlert("Error", error, "OK", "Cancel");
    } else {
        test.Text = e.Result;
    }
}