如何声明全局变量

0 c# variables uitableview ios 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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我需要使用“ uuids.Lenght ”来获取行数,并在方法NumberOfSections上使用。如果有人有另一个有效的想法,我会同意

我无法定义变量first,second或uuid的值,因为我的值是services.uuid和services.peripheral.uuid,而且我无法声明服务

抢劫代码中的注释。

Sye*_*idi 6

我认为您需要的是静态类,也可以将变量存储在应用程序设置中。

方法1:

public static class GlobalVariables
{
    public static string Global1 = "Hello";
    public static string Global2 = "World";
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以像这样在应用程序中的任何位置访问这些变量:

GlobalVariables.Global1
Run Code Online (Sandbox Code Playgroud)

方法2:

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

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

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

在此处输入图片说明

申请设置文件


我个人更喜欢将全局变量存储在应用程序设置中,仅此而已。

编辑:

点击此链接,我认为它将解决您的问题。基本上,您在这里看到的是覆盖静态方法,但您不能这样做,但是幸运的是,链接可以解决此问题。让我知道是否有帮助。