我正在开发一个应用程序,我需要像全局变量一样声明一个字符串数组以在某些方法中使用它,但问题是我无法分配它。
我的代码:
protected class ServiceTableSource : UITableViewSource
{
**string first = null;
string second = null;
protected string[] uuids;**
protected const string cellID = "BleServiceCell";
public event EventHandler<ServiceSelectedEventArgs> ServiceSelected = delegate {};
public List<CBService> Services {
get { return this._services; }
set { this._services = value; }
}
protected List<CBService> _services = new List<CBService> ();
public override int NumberOfSections (UITableView tableView)
{
return 1;
}
**public override int RowsInSection (UITableView tableview, int section)
{
return uuids.Length; <---- HERE'S THE PROBLEM!!!!
}**
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellID);
**CBService service = this._services [indexPath.Row];** <--HERE'S ANOTHER PROBLEM!!!! I CAN`T REMOVE THE [INDEXPATH.ROW] BECOUSE IS A LIST.
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID);
}
**first = service.UUID.ToString();
second = service.Peripheral.Identifier.ToString();
uuids = new string[]{first, second};**
cell.DetailTextLabel.Text = "UUID:" + uuids[indexPath.Row];
return cell;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
**CBService service = this._services [indexPath.Row];** HERE'S ANOTHER PROBLEM!!!!
this.ServiceSelected (this, new ServiceSelectedEventArgs (service));
tableView.DeselectRow (indexPath, true);
}
public class ServiceSelectedEventArgs : EventArgs
{
public CBService Service {
get { return this._service; }
set { this._service = value; }
}
protected CBService _service;
public ServiceSelectedEventArgs (CBService service)
{
this._service = service;
}
}
}
我需要使用“uuids.Lenght”来获取行数并在方法 NumberOfSections 上使用。如果有人有其他可行的想法,我会同意 kkk
我无法定义变量 first、second 或 uuid 的值,因为我的值是 services.uuid 和 services.peripheral.uuid,而且我无法声明服务
掠夺代码中的注释。
最佳答案
我认为你需要的是一个静态类,你也可以在你的应用程序设置中存储你的变量。
方法一:
public static class GlobalVariables
{
public static string Global1 = "Hello";
public static string Global2 = "World";
}
现在您可以像这样在应用程序的任何位置访问这些变量:
GlobalVariables.Global1
方法二:
按照以下步骤在应用程序设置中存储您的变量:
解决方案资源管理器 => 你的项目 => 属性 => 设置
现在您可以设置变量的名称、类型、范围和值。我附上截图。
Documentation for Application Setting
就我个人而言,我更喜欢将我的全局变量存储在我的应用程序设置中,但那只是我。
编辑:
关注这个link ,我认为它会解决你的问题。基本上,您在这里看到的是重写静态方法,但您不能那样做,但幸运的是,该链接已解决此问题。让我知道这是否有帮助。
关于c# - 如何声明全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686089/