我正在使用Xamarin Studio中的C#
制作应用程序的基本笔记,并且一直在线学习。
问题是提供的代码示例包含大量重复代码,这使得更新应用程序越来越难。
我在下面标注了与两个非常相似的类EditNoteViewController.cs
和AddNoteViewController.cs
相关的相关代码,如何将代码提取出来以便我只能调用一次?
或者我正在考虑将它全部提取到一个类中,并根据用户是创建还是更新注释来使条件流呈现不同的属性。
EditNoteController.cs
using System;
using UIKit;
namespace NoteTaker.iOS
{
public class EditNoteViewController : UIViewController
{
Note note;
public EditNoteViewController (Note _note)
{
note = _note;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255, 0, 255); // Repeated code. Also used in MainViewController.cs
this.Title = "Edit Note";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };
this.NavigationController.NavigationBar.TintColor = UIColor.White;
this.View.BackgroundColor = UIColor.White;
var titleEntryBox = new UITextField () {
Frame = new CoreGraphics.CGRect (0, 100, View.Bounds.Width, 45), // Repeated code
BackgroundColor = UIColor.LightGray,
TextColor = UIColor.Black,
Text = note.title
};
var descriptionLabel = new UILabel () {
Frame = new CoreGraphics.CGRect (10, 180, 250, 35),
Text = "Description",
};
var descriptionEntryBox = new UITextView () {
Frame = new CoreGraphics.CGRect (0, 220, View.Bounds.Width, 100), // Repeated code
BackgroundColor = UIColor.LightGray,
TextColor = UIColor.Black,
Text = note.description
};
var updateButton = new UIButton () {
Frame = new CoreGraphics.CGRect (10, 340, 120, 45)
}; // Repeated code
updateButton.SetTitle ("Update", UIControlState.Normal);
updateButton.BackgroundColor = UIColor.FromRGB (255, 0, 255);
updateButton.SetTitleColor (UIColor.White, UIControlState.Normal);
this.View.Add (titleEntryBox);
this.View.Add (descriptionLabel);
this.View.Add (descriptionEntryBox);
this.View.Add (updateButton);
updateButton.TouchUpInside += (sender, e) => {
if (titleEntryBox.Text.Length < 4)
return;
var noteToUpdate = new Note () {
ID = note.ID,
title = titleEntryBox.Text,
description = descriptionEntryBox.Text,
dateCreated = DateTime.Now
};
Database.updateNote (noteToUpdate);
this.NavigationController.PopViewController (true);
};
}
}
}
AddNoteViewController.cs
using System;
using UIKit;
namespace NoteTaker.iOS
{
public class AddNoteViewController : UIViewController
{
public AddNoteViewController ()
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255, 0, 255);
this.Title = "New Note";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };
this.NavigationController.NavigationBar.TintColor = UIColor.White;
this.View.BackgroundColor = UIColor.White;
var titleEntryBox = new UITextField () {
Frame = new CoreGraphics.CGRect (0, 100, View.Bounds.Width, 45),
BackgroundColor = UIColor.LightGray,
Placeholder = "Enter title...",
TextColor = UIColor.Black
};
var descriptionLabel = new UILabel () {
Frame = new CoreGraphics.CGRect (10, 180, 250, 35),
Text = "Enter description below"
};
var descriptionEntryBox = new UITextView () {
Frame = new CoreGraphics.CGRect (0, 220, View.Bounds.Width, 100),
BackgroundColor = UIColor.LightGray,
TextColor = UIColor.Black
};
var saveButton = new UIButton () {
Frame = new CoreGraphics.CGRect (10, 340, 120, 45)
};
saveButton.SetTitle ("Save Note", UIControlState.Normal);
saveButton.BackgroundColor = UIColor.FromRGB (255, 0, 255);
saveButton.SetTitleColor (UIColor.White, UIControlState.Normal);
this.View.Add (titleEntryBox);
this.View.Add (descriptionLabel);
this.View.Add (descriptionEntryBox);
this.View.Add (saveButton);
saveButton.TouchUpInside += (sender, e) => {
if (titleEntryBox.Text.Length < 4)
return;
var noteToSave = new Note () {
title = titleEntryBox.Text,
description = descriptionEntryBox.Text,
dateCreated = DateTime.Now
};
Database.InsertNote (noteToSave);
titleEntryBox.Text = "";
descriptionEntryBox.Text = "";
};
}
}
}
答案 0 :(得分:1)
有很多不同的方法可以解决这样的问题;这只是一个简单的例子 - 将null传递给控制器将导致它“添加”,否则它将充当“编辑”页面
public class NoteViewController : UIViewController
{
Note note;
public NoteViewController (Note _note)
{
note = _note;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255, 0, 255); // Repeated code. Also used in MainViewController.cs
this.Title = note == null ? "Add Note" : "Edit Note";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };
this.NavigationController.NavigationBar.TintColor = UIColor.White;
this.View.BackgroundColor = UIColor.White;
var titleEntryBox = new UITextField () {
Frame = new CoreGraphics.CGRect (0, 100, View.Bounds.Width, 45), // Repeated code
BackgroundColor = UIColor.LightGray,
TextColor = UIColor.Black,
Text = note == null ? string.Empty : note.title
};
var descriptionLabel = new UILabel () {
Frame = new CoreGraphics.CGRect (10, 180, 250, 35),
Text = "Description",
};
var descriptionEntryBox = new UITextView () {
Frame = new CoreGraphics.CGRect (0, 220, View.Bounds.Width, 100), // Repeated code
BackgroundColor = UIColor.LightGray,
TextColor = UIColor.Black,
Text = note == null ? string.Empty : note.description
};
var button = new UIButton () {
Frame = new CoreGraphics.CGRect (10, 340, 120, 45)
}; // Repeated code
if (note == null) {
button.SetTitle ("Add", UIControlState.Normal);
} else {
button.SetTitle ("Update", UIControlState.Normal);
}
button.BackgroundColor = UIColor.FromRGB (255, 0, 255);
button.SetTitleColor (UIColor.White, UIControlState.Normal);
this.View.Add (titleEntryBox);
this.View.Add (descriptionLabel);
this.View.Add (descriptionEntryBox);
this.View.Add (button);
button.TouchUpInside += (sender, e) => {
if (note == null) {
var noteToSave = new Note () {
title = titleEntryBox.Text,
description = descriptionEntryBox.Text,
dateCreated = DateTime.Now
};
Database.InsertNote (noteToSave);
titleEntryBox.Text = "";
descriptionEntryBox.Text = "";
} else {
if (titleEntryBox.Text.Length < 4)
return;
var noteToUpdate = new Note () {
ID = note.ID,
title = titleEntryBox.Text,
description = descriptionEntryBox.Text,
dateCreated = DateTime.Now
};
Database.updateNote (noteToUpdate);
this.NavigationController.PopViewController (true);
}
};
}
}