我正在尝试在我的应用中设置全局变量,例如颜色。即导航栏,我必须在每个视图控制器中单独设置的颜色。
我创建了一个帮助器类UIHelper.cs
来设置这些值,然后在其他地方调用它们。
在我的MainViewController.cs
中,我正在创建UIHelper
的实例并在此行中调用它:
this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB();
但这会引发System.NullReferenceException
。
System.NullReferenceException: Object reference not set to an instance of an object
at NoteTaker.iOS.MainViewController.ViewDidLoad () [0x00018] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/MainViewController_.cs:28
at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:77
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:61
at NoteTaker.iOS.Application.Main (System.String[] args) [0x00013] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/Main.cs:17
MainViewController.cs
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 uiHelper = new UIHelper();
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255,0,255);
this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB(); // Exception being thrown here
this.Title = "Notes";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };
table = new UITableView () {
Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom, this.View.Bounds.Width, this.View.Bounds.Height - this.NavigationController.NavigationBar.Frame.Height),
};
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 );
}
}
}
UIHelper.cs
using System;
using UIKit;
namespace NoteTaker.iOS
{
public class UIHelper : UIInputViewController
{
public UIHelper ()
{
}
public UIColor SetNavBarRGB ()
{
var uiColour = UIColor.FromRGB (255, 0, 255);
return uiColour;
}
}
}
答案 0 :(得分:2)
你有冲突:
您将uiHelper
声明为类定义中的字段:
partial class MainViewController : UIViewController
{
List<Note> notes;
UITableView table;
UIHelper uiHelper;
然后在构造函数中声明另一个具有相同名称的变量:
public MainViewController (IntPtr handle) : base (handle)
{
notes = new List<Note> ();
UIHelper uiHelper = new UIHelper();
}
这会导致仅在构造函数的范围内声明和分配本地uiHelper
,但永远不会触及类“uiHelper
”。因此它在ViewLoad()
方法中为空。只需删除构造函数中的类型,就可以了:
public MainViewController (IntPtr handle) : base (handle)
{
notes = new List<Note> ();
uiHelper = new UIHelper();
}
答案 1 :(得分:1)
您的MainViewController构造函数(MainViewController (IntPtr handle)
)是否被调用?
您是否可以尝试检查null
uiHelper
变量,如果它为null,则从UIHelper类初始化新实例。
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
If(uiHelper == null)
{
uiHelper = new UIHelper();
}
//this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255,0,255);
this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB(); // Exception being thrown here
this.Title = "Notes";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };
table = new UITableView () {
Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom, this.View.Bounds.Width, this.View.Bounds.Height - this.NavigationController.NavigationBar.Frame.Height),
};
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);
}