无法在 Xamarin 上转换 UIKit.UIViewController iOS

本文关键字:UIKit UIViewController iOS 转换 Xamarin | 更新日期: 2023-09-27 18:31:52

我正在尝试通过将一些参数提取到辅助类中来提高我的应用程序的代码重用性和可读性,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 );
        }
    }
}

无法在 Xamarin 上转换 UIKit.UIViewController iOS

想通了,用户错误像往常一样。代码很好,按预期工作。问题在于这种方法,我调用了一个变量,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

更正后,代码现在可以正常工作