1

我试图弄清楚为什么这段代码在 Visual Studio for Mac 的调试模式下崩溃。

如果没有设置断点,它运行正常,但是当单步执行时,应用程序崩溃并显示以下错误消息:

2017-10-23 10:26:11.137 Refit20171023[50460:17803570] error: * Assertion at /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/external/mono/mono/mini/debugger-agent.c:4714, condition `array->len == 1' not met
[...]
Got a SIGABRT while executing native code. This usually indicates...

应用程序在“this.username = user.Login;”上崩溃 线。

    public async override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        var gh = RestService.For<IGitHubApi>("https://api.github.com");
        var user = await gh.GetUser("octocat");
        txtUsername.Text = user.Login;
    }

将 user.Login 分配给 txtUsername.Text 或变量字符串时,它会崩溃。这是完整的 .cs 文件

using System;
using UIKit;
using Refit;
using System.Threading.Tasks;


namespace mySampleApp
{
    public class User
    {
        public string Login { get; set; }
    }

    [Headers("User-Agent: Refit Integration Tests")]
    public interface IGitHubApi
    {
        [Get("/users/{username}")]
        Task<User> GetUser(string userName);
    }

    public partial class ViewController : UIViewController
    {
        string username;

        protected ViewController(IntPtr handle) : base(handle)
        {
        }

        public async override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var gh = RestService.For<IGitHubApi>("https://api.github.com");
            var user = await gh.GetUser("octocat");
            this.username = user.Login;
        }


        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
        }
    }
}

但是,如果我通过属性分配 user.Login,则不会发生崩溃。

此代码运行并调试正常。

using System;
using UIKit;
using Refit;
using System.Threading.Tasks;


namespace mySampleApp
{
    public class User
    {
        public string Login { get; set; }
    }

    [Headers("User-Agent: Refit Integration Tests")]
    public interface IGitHubApi
    {
        [Get("/users/{username}")]
        Task<User> GetUser(string userName);
    }

    public partial class ViewController : UIViewController
    {
        string username;

        public string pUsername
        {
            get { return username; }
            set {
                username = value;
                // UITextField txtUsername
                txtUsername.Text = value;
            }
        }

        protected ViewController(IntPtr handle) : base(handle)
        {
        }

        public async override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var gh = RestService.For<IGitHubApi>("https://api.github.com");
            var user = await gh.GetUser("octocat");
            this.pUsername = user.Login;
        }


        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
        }
    }
}

为什么会这样?

4

0 回答 0