我想在我的iOS应用中使用C#Ping Class来检查网络连接。但我一直收到这个错误:InvalidOperationException,我正在使用Xamarin.ios,任何人都可以帮助我,谢谢! 这是我的代码:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var resultScreen = myScreen; // the results monitor
IPAddress myIp; // the IP address
IPAddress.TryParse(myField.Text, out myIp); // copying address from the UITextField to myIp
pingButton.TouchUpInside += (object sender, EventArgs e) => {
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
var reply = pingSender.Send(myIp.toString(), timeout, buffer,options); //the problem is here
if (reply.Status == IPStatus.Success) {
resultScreen.Text = reply.Address.ToString();
}
};
}
例外细节是:
System.InvalidOperationException:由于对象的当前状态,操作无效。 在/ Users / builder / data / lanes /中的System.Net.NetworkInformation.Ping.Send(System.Net.IPAddress地址,Int32超时,System.Byte []缓冲区,System.Net.NetworkInformation.PingOptions选项)[0x00051] 2922 / 7b89851d /源极/ maccore / _build /库/框架/ Xamarin.iOS.framework /版本/ GIT中/ SRC /单声道/ MCS /类/系统/ System.Net.NetworkInformation / Ping.cs:227 在/ Users / builder / data / lanes / 2922 /中的System.Net.NetworkInformation.Ping.Send(System.String hostNameOrAddress,Int32 timeout,System.Byte [] buffer,System.Net.NetworkInformation.PingOptions options)[0x00007] 7b89851d /源极/ maccore / _build /库/框架/ Xamarin.iOS.framework /版本/ GIT中/ SRC /单声道/ MCS /类/系统/ System.Net.NetworkInformation / Ping.cs:203 at mytest.viewController2 + c__AnonStorey0。<> m__1(System.Object sender,System.EventArgs e)[0x00056] /Users/Aminzz/Projects/mytest/mytest/viewController2.cs:44 在/Users/builder/data/lanes/2922/7b89851d/source/maccore/src/UIKit/UIControl.cs:38中的UIKit.UIControlEventProxy.Activated()[0x00007] at at(wrapper managed-to-native)UIKit.UIApplication:UIApplicationMain(int,string [],intptr,intptr) 在UIKit.UIApplication.Main(System.String [] args,IntPtr principal,IntPtr delegate)[0x00005] in /Users/builder/data/lanes/2922/7b89851d/source/maccore/src/UIKit/UIApplication.cs:79 在/ Users / builder / data / lanes / 2922 / 7b89851d / source / maccore / src / UIKit / UIApplication中的UIKit.UIApplication.Main(System.String [] args,System.String principalClassName,System.String delegateClassName)[0x0001c]的.cs:62 at mytest.Application.Main(System.String [] args)[0x00008] in /Users/Aminzz/Projects/mytest/mytest/Main.cs:12