无法在Xamarin上转换UIKit.UIViewController iOS

时间:2016-01-26 13:13:32

标签: c# ios uiviewcontroller

我试图通过将一些参数提取到帮助程序类UIHelper.cs来增加我的应用程序的代码重用和可读性。

我遇到的问题是,在尝试提取设置布局的方法时,我遇到了这些错误。 仍然无法解决,如果有人可以分享给可能知道自己很棒的人。

/Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/UIHelper.cs(30,30): Error CS1502:
The best overloaded method match for `UIKit.UITableView.UITableView(Foundation.NSCoder)' has some invalid arguments (CS1502) (NoteTaker.iOS)

/Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/UIHelper.cs(47,47): Error CS1503:
Argument `#1' cannot convert `UIKit.UIViewController' expression to type `Foundation.NSCoder' (CS1503) (NoteTaker.iOS)

我从原始控制器传回this,因此该方法具有正确的引用。我认为控制器必须是UIViewController

的对象

因此我不明白为什么这是错误的类型。 提前谢谢。

UIHelper.cs

using System;
using UIKit;

namespace NoteTaker.iOS
{
    public class UIHelper : UIViewController
    {

        UIViewController controller;

        public UIHelper ()
        {

        }

        public UIColor SetNavBarRGB ()
        {
            UIColor uiColour = UIColor.FromRGB (255, 0, 255);
            return uiColour;
        }

        public UIStringAttributes NavBarForeGroundColour ()
        {
            UIStringAttributes navBarForeGroundColour = new UIStringAttributes () { ForegroundColor = UIColor.White };
            return navBarForeGroundColour;
        }

        public UITableView TableLayoutHelper (UIViewController controller)
        {

            UITableView tableLayout = new UITableView (controller) { // Error appears here
                Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom,
                    controller.View.Bounds.Width,
                    controller.View.Bounds.Height - controller.NavigationController.NavigationBar.Frame.Height),
            };
            return tableLayout;
        }
    }
}

MainViewController.cs

using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
using System.Collections.Generic;

namespace NoteTaker.iOS
{
    partial class MainViewController : UIViewController
    {
        List<Note> notes;
        UITableView table;
        UIHelper uiHelper;


        public MainViewController (IntPtr handle) : base (handle)
        {
            notes = new List<Note> ();
            uiHelper = new UIHelper();
        }

        public override void ViewDidLoad ()

        {
            base.ViewDidLoad ();

            this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB();
            this.Title = "Notes";
            this.NavigationController.NavigationBar.TitleTextAttributes = uiHelper.NavBarForeGroundColour ();

            table = uiHelper.TableLayoutHelper (this); // Method called here
            this.View.Add (table);

            var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
            addButton.TintColor = UIColor.White;

            this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[] { addButton };
            addButton.Clicked += (sender, e) => {
                this.NavigationController.PushViewController (new NoteViewController(), true);
            };

            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource (notes, this);
        }

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource(notes, this );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

按照惯例计算出用户错误。代码很好,按预期工作。 问题出在这个方法中,我有一个变量调用this而不是传递给方法的controller参数。

public UITableView TableLayoutHelper (UIViewController controller)
        {

            UITableView tableLayout = new UITableView (controller) { // Error appears here
                Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom,
                    controller.View.Bounds.Width,
                    controller.View.Bounds.Height - controller.NavigationController.NavigationBar.Frame.Height),
            };
            return tableLayout;
        }

this.NavigationController.NavigationBar.Frame.Bottom

应该是,

controller.NavigationController.NavigationBar.Frame.Bottom

更正了,代码现在可以正常运行