c# - 如何声明全局变量

标签 c# ios uitableview variables xamarin

我正在开发一个应用程序,我需要像全局变量一样声明一个字符串数组以在某些方法中使用它,但问题是我无法分配它。

我的代码:

        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

方法二:

按照以下步骤在应用程序设置中存储您的变量:

解决方案资源管理器 => 你的项目 => 属性 => 设置

现在您可以设置变量的名称、类型、范围和值。我附上截图。

enter image description here

Documentation for Application Setting


就我个人而言,我更喜欢将我的全局变量存储在我的应用程序设置中,但那只是我。

编辑:

关注这个link ,我认为它会解决你的问题。基本上,您在这里看到的是重写静态方法,但您不能那样做,但幸运的是,该链接已解决此问题。让我知道这是否有帮助。

关于c# - 如何声明全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686089/

相关文章:

c# - 如何在单元测试期间覆盖 IQueryable 的 Contains 方法?

c# - 具有唯一键和值的组数据

ios - 2个后台进程队列

ios - 将 Cloud Code 与 Parse Server 和 Heroku 结合使用

ios - 识别长按 UITableViewCell 的真实索引路径

c# - 如何在 ASP.Net 中发送状态代码 500 并仍然写入响应?

c# - 定时器速度问题WPF

ios - 一个 UIButton 上的两个控件事件

ios - 有没有一种快速设置 UITableViewCell 样式的方法?

ios - UITableView 部分阴影