简体   繁体   中英

Web Service asmx Xamarin.Form doesn't work

My application Android display this error : "Object reference not set an instance of an object" when I execute my code. It's as if there was nothing in "e.Result". However, My webservice works well

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

Can you help me ? :) Thanks

As the target method is an event handler, you can Take advantage of the async event handler to keep the event arg e in scope.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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