我已经开始学习 Xamarin 并创建了从服务器检索数据的小教程。我使用了 NSUrlSession 机制。下面是我的代码。我不确定为什么会显示 NSUrlSession 警告。请帮助我理解和解决此警告。
警告 CS0618:“NSUrlSession.FromConfiguration(NSUrlSessionConfiguration, NSUrlSessionDelegate, NSOperationQueue)”已过时:“使用带有 INSUrlSessionDelegate 参数的重载。” (CS0618)(示例应用)
public void getData() {
NSUrl url = new NSUrl("some url");
NSUrlRequest request = new NSUrlRequest(url);
NSUrlSession session = null;
NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
session = NSUrlSession.FromConfiguration(myConfig, new MySessionDelegate (), new NSOperationQueue ());
NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
});
task.Resume();
}
public class MySessionDelegate : NSUrlSessionDelegate, INSUrlSessionDelegate
{
}
Best Answer-推荐答案 strong>
NSUrlSessionDelegate 实现了 INSUrlSessionDelegate 协议(protocol),所以你可以转换它:
session = NSUrlSession.FromConfiguration(myConfig, (new MyNSUrlSessionDelegate() as INSUrlSessionDelegate), new NSOperationQueue());
或者:
改变你的 UrlSessionDelegate 继承自 NSObject 然后实现 INSUrlSessionDelegate 协议(protocol):
public class MySessionDelegate : NSObject, INSUrlSessionDelegate
{
~~~ implement the protocols that you need
}
关于ios - Xamarin 中的 NSUrlSession,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/46550270/
|