ios - Xamarin 中的 NSUrlSession
<p><p>我已经开始学习 Xamarin 并创建了从服务器检索数据的小教程。我使用了 NSUrlSession 机制。下面是我的代码。我不确定为什么会显示 NSUrlSession 警告。请帮助我理解和解决此警告。</p>
<p><strong>警告 CS0618:“NSUrlSession.FromConfiguration(NSUrlSessionConfiguration, NSUrlSessionDelegate, NSOperationQueue)”已过时:“使用带有 <code>INSUrlSessionDelegate</code> 参数的重载。” (CS0618)(示例应用)</strong></p>
<pre><code> 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
{
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>NSUrlSessionDelegate</code> 实现了 <code>INSUrlSessionDelegate</code> 协议(protocol),所以你可以转换它:</p>
<pre><code>session = NSUrlSession.FromConfiguration(myConfig, (new MyNSUrlSessionDelegate() as INSUrlSessionDelegate), new NSOperationQueue());
</code></pre>
<p>或者:</p>
<p>改变你的 <code>UrlSessionDelegate</code> 继承自 <code>NSObject</code> 然后实现 <code>INSUrlSessionDelegate</code> 协议(protocol):</p>
<pre><code>public class MySessionDelegate : NSObject, INSUrlSessionDelegate
{
~~~ implement the protocols that you need
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Xamarin 中的 NSUrlSession,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46550270/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46550270/
</a>
</p>
页:
[1]