Xamarin.ios NSURLConnectionLegate从未在monotouch中被呼叫?

Xamarin.ios NSURLConnectionLegate从未在monotouch中被呼叫?,xamarin.ios,Xamarin.ios,我最近使用NSUrlConnection从url下载图片异步,以避免阻塞ui线程。 因此,我编写了以下代码: NSMutableUrlRequest req=new NSMutableUrlRequest(new NSUrl(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20); req["User-Agent"]=_UserAgent; USUrlConnection.FromRequest(req, new Escoz

我最近使用NSUrlConnection从url下载图片异步,以避免阻塞ui线程。 因此,我编写了以下代码:

 NSMutableUrlRequest req=new NSMutableUrlRequest(new NSUrl(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20);
req["User-Agent"]=_UserAgent;
USUrlConnection.FromRequest(req, new EscozUrlDelegate("picture",(result)=>
                           {
                                   Console.WriteLine(result);
                                   .....
                               });
但是我必须说,委托中的代码永远不会被调用。实际上,没有调用inside EscozUrlDelegate的重写方法。在NSUrlConnection.FromRequest之后不会发生任何事情

以下是EscozUrlDelegate的代码:

public class EscozUrlDelegate : NSUrlConnectionDelegate {
    Action<string> callback;
    Action _failure;
    NSMutableData data;
    string _name;

    public EscozUrlDelegate(string name, Action<string> success) {
        _name = name;
        callback = success;
        data = new NSMutableData();
    }

    public EscozUrlDelegate(string name, Action<string> success, Action failure) {
        _name = name;
        callback = success;
        _failure = failure;
        data = new NSMutableData();
    }

    public override void ReceivedData (NSUrlConnection connection, NSData d)
    {
        data.AppendData(d);
    }

    public override bool CanAuthenticateAgainstProtectionSpace (NSUrlConnection connection, NSUrlProtectionSpace protectionSpace)
    {
        return true;
    }

    bool showError = true;

    public override void ReceivedAuthenticationChallenge (NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
    {
        if (challenge.PreviousFailureCount>0){
            showError = false;
            challenge.Sender.CancelAuthenticationChallenge(challenge);
            Application.AuthenticationFailure();
            return;
        }

        if (challenge.ProtectionSpace.AuthenticationMethod=="NSURLAuthenticationMethodServerTrust")
            challenge.Sender.UseCredentials(NSUrlCredential.FromTrust(challenge.ProtectionSpace.ServerTrust), challenge);

        if (challenge.ProtectionSpace.AuthenticationMethod=="NSURLAuthenticationMethodDefault" &&
                Application.Account!=null && Application.Account.Login!=null && Application.Account.Password!=null) {
            challenge.Sender.UseCredentials(NSUrlCredential.FromUserPasswordPersistance(
                      Application.Account.Login, Application.Account.Password, NSUrlCredentialPersistence.None), challenge);

        }
    }

    public override void FailedWithError (NSUrlConnection connection, NSError error)
    {
        UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
        if (showError)
            Application.ShowNetworkError(error.LocalizedDescription);

        if (_failure!=null)
            _failure();
    }

    public override void FinishedLoading (NSUrlConnection connection)
    {
        EscozUrlConnection.ConnectionEnded(_name);
        callback(data.ToString());
    }
}
公共类EscozUrlDelegate:nsurlconnectionelegate{
动作回调;
行动失败;
可变数据;
字符串\u名称;
公共EscozUrlDelegate(字符串名称,操作成功){
_名称=名称;
回调=成功;
数据=新的NSMutableData();
}
public EscozUrlDelegate(字符串名称、操作成功、操作失败){
_名称=名称;
回调=成功;
_失败=失败;
数据=新的NSMutableData();
}
公共覆盖无效接收数据(NSUrlConnection连接,NSD数据)
{
数据。追加数据(d);
}
public override bool CanAuthenticateAgainstProtectionSpace(NSUrlConnection connection,NSUrlProtectionSpace protectionSpace)
{
返回true;
}
bool-showError=true;
public override void ReceivedAuthenticationChallenge(NSUrlConnection connection,NSUrlAuthenticationChallenge)
{
如果(质询.先前失败计数>0){
ROR=错误;
challenge.Sender.CancelAuthenticationChallenge(质询);
Application.AuthenticationFailure();
返回;
}
if(challenge.ProtectionSpace.AuthenticationMethod==“NSURLAuthenticationMethodServerTrust”)
challenge.Sender.useCidentifications(NSUrlCredential.FromTrust(challenge.ProtectionSpace.ServerTrust),challenge);
if(challenge.ProtectionSpace.AuthenticationMethod==“NSURLAuthenticationMethodDefault”&&
Application.Account!=null&&Application.Account.Login!=null&&Application.Account.Password!=null){
challenge.Sender.UseCredentials(NSUrlCredential.FromUserPasswordPersistance(
Application.Account.Login、Application.Account.Password、NSUrlCredentialPersistence.None)、challenge);
}
}
公共覆盖无效失败(NSUrlConnection连接,NSError错误)
{
UIApplication.SharedApplication.NetworkActivityIndicatorVisible=false;
如果(错误)
Application.ShowNetworkError(error.LocalizedDescription);
如果(_failure!=null)
_失败();
}
公共覆盖无效完成加载(NSUrlConnection)
{
EscozUrlConnection.ConnectionEnded(_name);
回调(data.ToString());
}
}

请帮助我解决此问题。

要使用代理,您需要如下初始化连接:

NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(value),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,20);
req["User-Agent"]=_UserAgent;
var mydelegate = new EscozUrlDelegate("picture",(result)=>
                           { Console.WriteLine(result); });

new NSUrlConnection(request, mydelegate, true);

如果它不起作用,请告诉我。

我也有同样的问题,当我移动我的
NSUrlConnection.FromRequest(request,this)
然后相应的ReceivedData回调立即开始工作


我的代码是NSUrlConnectionDataDelegate实现的一部分。

我也尝试过,但仍然没有成功。在Escoz的Helper类中是这样的,但它不起作用,所以我将其更改为NSUrlConnection.FromRequest,但仍然没有发生任何事情。以及其他信息。我在线程池中调用了它,所以它不在主线程中,不知道这是否造成了问题,但我需要在主线程之外的其他线程中运行它。我还尝试了NSUrlConnection(request、mydelegate、false),然后是con.Start,仍然是一样的问题解决了,NSUrlConnection必须在主线程内使用。