非托管IP中的堆栈溢出 - Xamarion iOS

时间:2016-01-27 15:58:35

标签: c# ios visual-studio xamarin

我只是在Xamarin中为iOS制作一个小应用程序,但在Visual Studio 2015中,当我运行应用程序时,我一直收到此错误:

  

critical:非托管堆栈溢出:IP:0x2e8785,错误地址:   0xbff07ffc

对我正在做的事情有正确的背景。我是以编程方式将UIControls添加到我的主视图控制器中,该控制器工作得很好,但代码到处都是。因此,我决定将混乱重新组织成一个可以显示uicontrols的新类。但当我完成所有操作并运行应用程序时,我就开始收到错误。

任何人都可以帮助我或指出我正确的方向吗? 如果您需要查看该代码,请告诉我,我会将其添加进去。

感谢。

*更新*

DrawMenu.cs

namespace DrawingComponents
{
    class DrawMenu
    {
        UserInterfaceControls uicontrols;


        const int X_BTN = 100;
        const int Y_BTN = 100;
        const int PADDING = 5;

        float xMainPortrait; // Main views width
        float yMainPortrait; // Main views height

        float xSub; // Subviews width which is inrelation to the Mainviews dimensions.
        float ySub; // Subviews height which is inrelation to the Mainview demensions.

        float widthMainPortrait = 205;
        float heightMainPortrait = 310;

        public void PortraitNormal(UIView view)
        {
            xMainPortrait = ((float)view.Bounds.Size.Width - widthMainPortrait) / 2;
            yMainPortrait = ((float)view.Bounds.Size.Height - heightMainPortrait) / 2;

            // Draw the frame
            var mainFrame = new CGRect(xMainPortrait, yMainPortrait, widthMainPortrait, heightMainPortrait);
            uicontrols.mainview = new UIView(mainFrame);


            // Get the x and y coordinants of the mainview
            xSub = (float)uicontrols.mainview.Bounds.X;
            ySub = (float)uicontrols.mainview.Bounds.Y;

            // Get center of the mainview and last button
            float xCenter = ((float)uicontrols.mainview.Bounds.Size.Width / 2) - (X_BTN / 2);
            float yCenter = (Y_BTN * 2) + (PADDING * 2);

            // This will draw each component.
            uicontrols.btn_MySchedule.Frame = new CGRect(xSub, ySub, X_BTN, Y_BTN);
            uicontrols.btn_MyCareer.Frame = new CGRect(xSub + X_BTN + PADDING, ySub, X_BTN, Y_BTN);
            uicontrols.btn_MySocial.Frame = new CGRect(xSub, ySub + Y_BTN + PADDING, X_BTN, Y_BTN);
            uicontrols.btn_Feedback.Frame = new CGRect(xSub + X_BTN + PADDING, ySub + Y_BTN + PADDING, X_BTN, Y_BTN);
            uicontrols.btn_MyDetails.Frame = new CGRect(xCenter, yCenter, X_BTN, Y_BTN);


            uicontrols.mainview.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;

            uicontrols.btn_MySchedule.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
            uicontrols.btn_MyCareer.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
            uicontrols.btn_MySocial.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
            uicontrols.btn_Feedback.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
            uicontrols.btn_MyDetails.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;


            view.Add(uicontrols.mainview);

            uicontrols.mainview.AddSubview(uicontrols.btn_MySchedule);
            uicontrols.mainview.AddSubview(uicontrols.btn_MyCareer);
            uicontrols.mainview.AddSubview(uicontrols.btn_MySocial);
            uicontrols.mainview.AddSubview(uicontrols.btn_Feedback);
            uicontrols.mainview.AddSubview(uicontrols.btn_MyDetails);
        }

    }
}

MainViewController.cs

namespace DrawingComponents
{
    struct UserInterfaceControls
    {
        public UIView mainview;

        public UIButton btn_MySchedule;
        public UIButton btn_MyCareer;
        public UIButton btn_MySocial;
        public UIButton btn_Feedback;
        public UIButton btn_MyDetails;
    }


    class MainViewController : UIViewController
    {
        UserInterfaceControls uicontrols;
        DrawMenu menu = new DrawMenu();


        public override void ViewDidLoad()
        {

            base.ViewDidLoad();


            // Instantiate menu buttons
            uicontrols.btn_MySchedule = new UIButton();
            uicontrols.btn_MyCareer = new UIButton();
            uicontrols.btn_MySocial = new UIButton();
            uicontrols.btn_Feedback = new UIButton();
            uicontrols.btn_MyDetails = new UIButton();

            // Set the image of each button
            uicontrols.btn_MySchedule.SetImage(UIImage.FromFile("images/myschedule.png"), UIControlState.Normal);
            uicontrols.btn_MyCareer.SetImage(UIImage.FromFile("images/mycareer.png"), UIControlState.Normal);
            uicontrols.btn_MySocial.SetImage(UIImage.FromFile("images/mysocial.png"), UIControlState.Normal);
            uicontrols.btn_Feedback.SetImage(UIImage.FromFile("images/feedback.png"), UIControlState.Normal);
            uicontrols.btn_MyDetails.SetImage(UIImage.FromFile("images/mydetails.png"), UIControlState.Normal);

            PositionControls(InterfaceOrientation);
        }

        public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
        {
            switch(toInterfaceOrientation)
            {
                case UIInterfaceOrientation.LandscapeLeft:
                case UIInterfaceOrientation.LandscapeRight:
                case UIInterfaceOrientation.Portrait:
                case UIInterfaceOrientation.PortraitUpsideDown:
                default:
                    return true;
            }
        }

        public override void WillAnimateRotation(UIInterfaceOrientation toInterfaceOrientation, double duration)
        {
            base.WillAnimateRotation(toInterfaceOrientation, duration);

            PositionControls(toInterfaceOrientation);
        }

        protected void PositionControls(UIInterfaceOrientation toInterfaceOrientation)
        {
            switch(toInterfaceOrientation)
            {
                case UIInterfaceOrientation.LandscapeLeft:
                case UIInterfaceOrientation.LandscapeRight:
                    if (View.Bounds.Size.Width > 480)
                    {
                        // Add code here
                    }
                    else
                    {
                         // Add code here

                    }
                    break;
                case UIInterfaceOrientation.Portrait:
                case UIInterfaceOrientation.PortraitUpsideDown:
                    if (View.Bounds.Width > 414)
                    {
                        // Add code here
                    }
                    else
                    {
                        menu.PortraitNormal(View);
                    }
                break;
        }
        }
    }
}

0 个答案:

没有答案