【问题标题】:can't access static method of a simple cs file in xaml.cs file无法访问 xaml.cs 文件中简单 cs 文件的静态方法
【发布时间】:2014-05-20 18:11:28
【问题描述】:

我正在为我的学期项目创建一个应用程序。在这个项目中,我有一个简单的类文件和一些 xaml 页面。在一个 xaml 页面中,代码检查字符串的值,并根据该值运行倒数计时器。如果字符串的值等于某个字符串,那么它应该从简单类中调用一个方法,其中字符串的值被更改,然后它应该导航到下一个 xaml 页面。

当我调用该函数时,应用程序会中断。没有错误或任何东西,它只是打破了。我不知道为什么。我已经在其他 xaml 文件中调用了同一类文件的其他函数,它们运行良好,但在这里我遇到了麻烦。我想这与计时器有关。

xaml.cs:

namespace TrafficGuru
{
    public partial class Page2 : PhoneApplicationPage
    {
        DispatcherTimer countDownTimer;
        int check;

        public Page2()
        {
            InitializeComponent();
            tbl.Text = global.str;
            if (global.str == "YOUR LIGHT IS ON FOR : ")
            {
                check = 0;
            }
            if (global.str == "YOUR LIGHT WILL BE ON IN:")
            {
                check = 1;
            }
            if (global.str == "YOUR LIGHT WILL NOT BE ON FOR UNTIL ATLEAST " + global.x * 15 + " MORE SECS.YOU WILL GET AN UPDATE IN:")
            {
                check = 2;
            }
            countDownTimer = new DispatcherTimer();
            countDownTimer.Interval = new TimeSpan(0, 0, 0, 1);
            countDownTimer.Tick += new EventHandler(countDownTimerEvent);
            countDownTimer.Start();




            test.Content = "" + "seconds remaining";
        }
        int count = global.cdt;

        void test_Click(object sender, EventArgs e) { }
        void countDownTimerEvent(object sender, EventArgs e)
        {
            test.Content = count + " Seconds";


            if (count > 0)
            {
                count--;
            }
            else if (count == 0)
            {
              if (check == 0)
                {
                    test.Content = "STOP!!";


                }
                else if (check == 1)
                {
                    test.Content = "GO!!!";


                }

                if(check==2)
                {
                  string x= global.rego();//the method i m trying to call its public and static





                    NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.RelativeOrAbsolute));*/


                }
               }
            }



    }

}

globalx 类代码::

if (x == 1)
                    {
                        str = "YOUR LIGHT WILL BE ON IN:";
                    }

                    cdt = x*15;

                    l[m].resetcount();
                    a[m] = 0;
                    l[m].setlight("RED");
                    c = 9;

                }

            if (l[0].getcount() == 0 || s > 0)
            {
                l[0].Createcar();
            }
            a[0] += l[0].getcount();
            t[0] = l[0].gettime();
            if (l[1].getcount() == 0 || s > 0)
            {
                l[1].Createcar();
            }
            a[1] += l[1].getcount();
            t[1] = l[1].gettime();
            if (l[2].getcount() == 0 || s > 0)
            {
                l[2].Createcar();
            }
            a[2] = l[2].getcount();
            t[2] = l[2].gettime();

            if (l[3].getcount() == 0 || s > 0)
            {
                l[3].Createcar();
            }
            a[3] += l[3].getcount();
            t[3] = l[3].gettime();
            s++;
            if (s % 2 != 0)
            {
                var now1 = DateTime.Now;
                tv = (now1 - begin).TotalMilliseconds;
                tym = Convert.ToInt32(tv);
                int[] pkl = { 0, 0 };
                maxi(a, t, s, tym, ref pkl);
                m = pkl[0];
            }
            else
            {
                var now1 = DateTime.Now;
                tv = (now1 - begin).TotalMilliseconds;
                tym = Convert.ToInt32(tv);
                int[] pkj = { 0, 0 };
                maxi(a, t, s, tym, ref pkj);
                q = pkj[0];
            }
            return str;
        }

使用 GO 和 REGO 完成全球课程

公共静态类 globalx { 公共静态int n; 公共静态整数; 公共静态int m; 公共静态int q; 公共静态int c; 公共静态int f; 公共静态车道[] l = 新车道[4]; 公共静态双电视; 公共静态int tym; 公共静态 int[] a = { 0, 0, 0, 0 }; 公共静态 int[] t = new int[4]; 公共静态字符串 str="asdf"; 公共静态int cdt; //倒计时 公共静态int x; 公共静态日期时间开始;

    public static void start()
    {

        begin = DateTime.Now;

        for (int i = 0; i < 4; i++)
        { l[i] = new lane(); }



        var now = DateTime.Now;
        tv = (now - begin).TotalMilliseconds;
        tym = Convert.ToInt32(tv);
        l[0].Createcar();
        a[0] += l[0].getcount();
        t[0] = l[0].gettime();

        l[1].Createcar();
        a[1] += l[1].getcount();
        t[1] = l[1].gettime();

        l[2].Createcar();
        a[2] += l[2].getcount();
        t[2] = l[2].gettime();

        l[3].Createcar();
        a[3] += l[3].getcount();
        t[3] = l[3].gettime();
        now = DateTime.Now;
        tv = (now - begin).TotalMilliseconds;
        tym = Convert.ToInt32(tv);
        int[] r = { 88, 99 };
        maxi(a, t, s, tym, ref r);
        m = r[0];
        q = r[1];
        c = 0;
        f = 9;
    }
    public static string go()
   {

            l[m].setlight("GREEN");
            var now = DateTime.Now;
            tv = (now - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            l[m].settime(tym);
            if (n == m + 1)
            {
                c = 0;
                str = "YOUR LIGHT IS ON FOR : ";
                cdt = 15;

                l[m].resetcount();
                a[m] = 0;
                l[m].setlight("RED");

            }
            if (n == q + 1)
            {
                c = 9;
                str = "YOUR LIGHT WILL BE ON IN:";
                cdt = 15;
                                    l[m].resetcount();
                a[m] = 0;
                l[m].setlight("RED");
            }
            if (n != m + 1 && n != q + 1)
            {
                x = m + 1 - n;
                if (x < 0)
                {
                    x = 0 - x;
                }

                str = "YOUR LIGHT WILL NOT BE ON FOR UNTIL ATLEAST " + x * 15 + " MORE SECS.YOU WILL GET AN UPDATE IN:";
                if (x == 1)
                {
                    str = "YOUR LIGHT WILL BE ON IN:";
                }

                cdt = x*15;
                a[m] = 0;
                l[m].setlight("RED");
                c = 9;

            }

        if (l[0].getcount() == 0 || s > 0)
        {
            l[0].Createcar();
        }
        a[0] += l[0].getcount();
        t[0] = l[0].gettime();
        if (l[1].getcount() == 0 || s > 0)
        {
            l[1].Createcar();
        }
        a[1] += l[1].getcount();
        t[1] = l[1].gettime();
        if (l[2].getcount() == 0 || s > 0)
        {
            l[2].Createcar();
        }
        a[2] = l[2].getcount();
        t[2] = l[2].gettime();

        if (l[3].getcount() == 0 || s > 0)
        {
            l[3].Createcar();
        }
        a[3] += l[3].getcount();
        t[3] = l[3].gettime();
        s++;
        if (s % 2 != 0)
        {
            var now1 = DateTime.Now;
            tv = (now1 - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            int[] pkl = { 0, 0 };
            maxi(a, t, s, tym, ref pkl);
            m = pkl[0];
        }
        else
        {
            var now1 = DateTime.Now;
            tv = (now1 - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            int[] pkj = { 0, 0 };
            maxi(a, t, s, tym, ref pkj);
            q = pkj[0];
        }
        return str;
    }
    public static string rego()

    {

        {

            l[q].setlight("GREEN");
            var now = DateTime.Now;
            tv = (now - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            l[q].settime(tym);

            if (n == q + 1)
            {
                f = 0;
                str = "YOUR LIGHT IS ON FOR : ";
                cdt=15;
            }
            if (n == m + 1)
            {

                str = "YOUR LIGHT WILL BE ON IN:";
                cdt=15;
                l[q].resetcount();
                a[q] = 0;
                l[q].setlight("RED");
                f = 9;
            }
            if (n != m + 1 && n != q + 1)
            {
                x = m + 1 - n;
                if (x < 0)
                {
                    x = 0 - x;
                }

                str = "YOUR LIGHT WILL NOT BE ON FOR UNTIL ATLEAST " + x * 15 + " MORE SECS.YOU WILL GET AN UPDATE IN:";
                if (x == 1)
                {
                    str = "YOUR LIGHT WILL BE ON IN:";
                }
                cdt = x * 15;

                a[q] = 0;
                l[q].setlight("RED");
                f = 9;
            }
        }

        if (l[0].getcount() == 0 || s > 0)
        {
            l[0].Createcar();
        }
        a[0] += l[0].getcount();
        t[0] = l[0].gettime();
        if (l[1].getcount() == 0 || s > 0)
        {
            l[1].Createcar();
        }
        a[1] += l[1].getcount();
        t[1] = l[1].gettime();
        if (l[2].getcount() == 0 || s > 0)
        {
            l[2].Createcar();
        }
        a[2] = l[2].getcount();
        t[2] = l[2].gettime();

        if (l[3].getcount() == 0 || s > 0)
        {
            l[3].Createcar();
        }
        a[3] += l[3].getcount();
        t[3] = l[3].gettime();
        s++;
        if (s % 2 != 0)
        {
            var now = DateTime.Now;
            tv = (now - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            int[] pkl = { 0, 0 };
            maxi(a, t, s, tym, ref pkl);
            m = pkl[0];
        }
        else
        {
            var now = DateTime.Now;
            tv = (now - begin).TotalMilliseconds;
            tym = Convert.ToInt32(tv);
            int[] pkj = { 0, 0 };
            maxi(a, t, s, tym, ref pkj);
            q = pkj[0];
        }
        return str;

    }

【问题讨论】:

  • 你真的有一个叫做global的类型吗?我会先更改它,以便它遵循 .NET 命名约定并且不与上下文关键字冲突。 (我还会修正你所有的方法和属性名称以符合 .NET 命名约定。)这不会修正你的错误,但它至少会让你的代码更容易被其他人阅读。
  • 我是这方面的初学者,所以我为草率道歉。 global 也是一个公共静态类,go() 是一个公共静态方法。我根本没有收到错误。只是应用程序中断了。请帮我修复它
  • 能否请你添加全局代码,我们可以看到它的作用,它是如何定义的,甚至可以测试它。
  • 是rego()还是go()??在您提供的代码中是 rego(),在您的评论中是 go()。
  • rego 和 go 都做同样的事情。我不能调用其中任何一个。我已根据您的建议将类名更改为 globalx。这是代码。不要介意其中其他类的对象。它们工作正常。告诉我如何使这个函数工作::

标签: class windows-phone-8 methods static


【解决方案1】:

用 try catch 包围你的代码,看看信息是什么。

  try

    {
       // Your Code
    }

    catch (Exception ex)
    {

    Debug.writeline(ex.Message);

    }

【讨论】:

  • thanx.i 试过了。它说索引超出了数组的范围。我不明白。你呢?如果是,请告诉我如何解决它
  • thanx alot:-) 我终于弄明白了。函数 go() 中的数组存在问题。修复了它,现在它可以工作了。非常感谢你和其他所有人
  • 如果这对您有帮助,请接受答案(点击对勾图标)
猜你喜欢
  • 2013-10-01
  • 2020-11-26
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多