【问题标题】:How to declare global variable如何声明全局变量
【发布时间】:2014-10-30 10:04:22
【问题描述】:

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

我的代码:

        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 或 uuids 的值,因为我的值是 services.uuid 和 services.peripheral.uuid,我无法声明服务

掠夺代码中的 cmets。

【问题讨论】:

  • 你的意思是静态变量吗?一个在类的所有实例中都相同的变量?
  • 是的,我试试,但我需要 GetCell 方法中的变量 NSIndexPath 的值,如果我将其声明为静态,则该值将为“null”

标签: c# ios uitableview variables xamarin


【解决方案1】:

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

方法一:

public static class GlobalVariables
{
    public static string Global1 = "Hello";
    public static string Global2 = "World";
}

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

GlobalVariables.Global1

方法二:

按照此将变量存储在应用程序设置中:

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

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

Documentation for Application Setting


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

编辑:

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

【讨论】:

  • 你也是对的。但是我正在使用 Xamarin,但我没有找到那种方式“解决方案资源管理器 => 您的项目 => 属性 => 设置”并且.. 是的,一个静态类,但我如何做一个静态类?它不会有帮助。我想要的值在 OVERRIDEN 方法 GetCell 中的局部变量 NSIndexPath indexPath 中。应用程序的工作顺序: 1 - 运行 NumberOfSections 方法 2 - 运行 RowsInSection 方法 3 - 然后,运行 GetCell 方法。所以我需要的值在 GetCell 方法运行时得到一个值,我需要它在方法 RowsInSection...
  • 其实我需要一种在 RowsInSection 方法中使用 NSIndexPath indexPath 的方法。这是另一种方式,但如何?对于应用程序工作,我需要知道每个部分的行数,我不知道,因为我从 BLE 设备获取服务,每个设备可以向我发送随机数量的服务,可能是 4、5、6 ...
【解决方案2】:

如果你在谈论静态变量,你可以使用它。变量 NumberOfWheels 将在汽车类的所有实例之间共享。

public class Automobile
{
    protected static int NumberOfWheels = 4;

    public override int getWheels() {
           return wheels;
    }
}

在此处查看此链接,例如: http://msdn.microsoft.com/en-US/en-en/library/98f28cdx.aspx#

【讨论】:

  • 是的,类似这样...但是方法“RowInSection”比 GetCell 先运行,所以我需要该值,然后才能获得值 oo 但我不知道如何分配变量,因为我想给他的价值不要被宣布。我不知道该怎么做...
  • 如果我可以声明:CBService service = this._services [indexPath.Row];在没有 [indexPath.Row] 的情况下,我可以做到,但如果我做到了,请显示反馈:方法 CBService is obsolete...blabla...try .ctor()
  • 您可以像更改任何其他类属性一样更改类的静态值。
  • No man if I use: protected static class ServiceTableSource : UITableViewSource 错误说静态类必须从对象派生
【解决方案3】:

我可能读错了,如果是这种情况,请告诉我,我将删除我的答案。在我看来,您希望在 ServiceTableSource 类的所有方面都可以使用某些数据(有关服务的信息)。我敢肯定你不一定在乎你是怎么得到它的,你只是想也许一个全局变量会有所帮助?

我认为为了解决这个问题,您需要重新考虑使用ServiceTableSource 的方式。通常,当涉及使用“源”的应用程序时,“源”的实例一旦创建就不会改变。如果您需要根据添加的更多“服务”更新源,此时应创建ServiceTableSource 的新实例并将其分配给UITableView 对象的Source 属性。为了实现这一点,您需要在 ServiceTableSource 上创建一个构造函数,该构造函数接收“服务”集合并将它们保存到私有变量中,并执行从服务中获取数据和创建 string[] uuids 的所有逻辑。通过这样做,您可以访问为此类中的所有 overridden 方法创建实现所需的所有信息。

这是我对更新代码的看法。我在您的应用程序周围没有任何其他上下文,因此我无法真正对此进行测试。让我知道这是怎么回事,如果有问题,我可以帮助您解决这些问题。另外,我不确定你想对这个事件做什么。对此进行更多澄清也会有所帮助。

protected class ServiceTableSource : UITableViewSource
{
    protected string[] _uuids;

    protected List<CBService> _services = new List<CBService> ();

    protected ServiceTableSource(List<CBService> services)
    {
         _services = services;
         //TODO: Add your logic here to create your string[] _uuids
    }

    protected const string cellID = "BleServiceCell";

    public event EventHandler<ServiceSelectedEventArgs> ServiceSelected = delegate {};

    public override int NumberOfSections (UITableView tableView)
    {
        //TODO: Use _services and/or _uuids to determine the number of sections
        return 1;
    }

    public override int RowsInSection (UITableView tableview, int section)
    {
        return _uuids.Length;
    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellID);
        CBService service = this._services [indexPath.Row];


        if (cell == null) {
            cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID);
        }

        //TODO: Populate the cell with data from _services and/or _uuids.


        return cell;
    }

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        CBService service = this._services [indexPath.Row];

        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;
        }
    }
}

【讨论】:

  • 是的!我还没有尝试过,但我认为它会起作用..我会尽快给你答复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
相关资源
最近更新 更多