C# Xamarin 避免代码重复
C# Xamarin avoiding code repetition
我正在 Xamarin Studio 中使用 C#
制作一个基本的笔记应用程序,并且一直在学习在线课程。
问题是提供的代码示例包含大量重复代码,这使得更新应用程序变得越来越困难。
我在下面标记了与两个非常相似的classes,EditNoteViewController.cs
和AddNoteViewController.cs
有关的相关代码,我如何将代码提取出来以便我可以调用它一次?
或者,我正在考虑将其全部提取到一个 class 中,并根据用户是在创建还是更新笔记,使用条件流程呈现不同的属性。
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 = "";
};
}
}
}
有很多不同的方法可以解决这样的问题;这只是一个简单的示例 - 将 null 传递到控制器将导致它成为 "Add",否则它将充当 "Edit" 页面
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);
}
};
}
}
我正在 Xamarin Studio 中使用 C#
制作一个基本的笔记应用程序,并且一直在学习在线课程。
问题是提供的代码示例包含大量重复代码,这使得更新应用程序变得越来越困难。
我在下面标记了与两个非常相似的classes,EditNoteViewController.cs
和AddNoteViewController.cs
有关的相关代码,我如何将代码提取出来以便我可以调用它一次?
或者,我正在考虑将其全部提取到一个 class 中,并根据用户是在创建还是更新笔记,使用条件流程呈现不同的属性。
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 = "";
};
}
}
}
有很多不同的方法可以解决这样的问题;这只是一个简单的示例 - 将 null 传递到控制器将导致它成为 "Add",否则它将充当 "Edit" 页面
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);
}
};
}
}