Xamarin cross-plattform XAML表单应用程序中的C#静态私有成员 [英] C# static private members in xamarin cross-plattform XAML forms app

查看:83
本文介绍了Xamarin cross-plattform XAML表单应用程序中的C#静态私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年没有编码后,我重新开始编程c#,从一个XAML.Forms Cross-Platform-App开始。 C#对我来说是新的,但是对于C和C ++,我有多年的经验。



我担心的是不编码和测试我的代码,这是不寻常和意外的行为我的环境。在我的更复杂的代码遇到同样的问题后,我将问题简化为非常简单的事情。

After years without coding I restart programming c# for my own, starting with a XAML.Forms Cross-Platform-App. C# is new for me, but with C and C++ I have years of experience.

What me worries is not coding and testing my code, it's the unusual and unexpected behavior of my environment. After having the same problem with my more complex code, I reduced the problem to very simple stuff like this.

public class Tester
    {
        public static Tester _ttester = null;
        private static bool finit = false;

        public Tester()
        {   
            if (!finit)             // first time?
            {
                finit = true;       // first round done
                _ttester = new Tester();   // initiate a special global object for general use 
            }
        }
    }



调用这样的代码


With calling the code like this

static Tester tst = null;
tst = new Tester()


我的应用代码开头的


该应用程序在Tester构造函数中挂起,永远不会终止。



我尝试过:



如果我使用调试器,在我的Tester构造函数的开头设置一个断点,我注意到我无法查看finit本地静态var。 无法评估表达。在程序中它似乎是相同的。使用new Tester,构造函数被递归调用。但是,finit会员应该保护第二个测试人员第三个。但调试器显示它不会发生这种情况。一次又一次地调用Tester构造函数而不会终止。可能是一个非常聪明的优化器正在杀死我的编码。我不知道并需要一些帮助。



在一个小的consoleApp中做同样的事情就像预期的那样工作。但在我更复杂的跨平台环境中,它不起作用。可能是因为将App分为两部分。一般App部分和平台特定代码。以前我可以查看机器代码并查看编译器生成的内容。我今天没有办法看到类似的东西。

提前谢谢。


at the beginning of my App code.
The app hangs with in "Tester" constructor never terminating.

What I have tried:

If I am using the debugger, setting a Breakpoint at the beginning of my Tester Constructor, I notice that I am not able to view "finit" local static var. "Expression can not be evaluated". And within the program it seems to be the same. With "new Tester" the constructor is called recursively. But the "finit" Member should protect the second Tester calling a third one. But the debugger shows it does not happen like that. The "Tester" constructor is called again and again without ever terminating. May be a very clever optimizer is killing my coding. I do not have any idea and need some help.

Making the same in a small "consoleApp" anything works as expected. But in my more complex Cross-Platform Environment it does not work. May be because of having divided the App in two parts. The general App part and the platform specific code. In former times I could look at the machine code and see what the compiler has generated. I did not found a way to see something like that today.
Thanks in advance.

推荐答案

我不知道你的具体问题....

但你可以使用静态构造函数只运行一次代码吗?

不确定在静态构造函数中调用类构造函数是个好主意。但无论如何,这是怎么回事



解决方案1 ​​

I do not know about your particular issue....
But you could use static constructor to run that code only once perhaps?
Not sure it's a good idea to call class constructor in a static constructor... but anyway, here is how

Solution 1
public class Tester
{
  readonly static Tester cTest = new Tester();
}





解决方案2



Solution 2

public class Tester
{
  readonly static Tester cTest;

  static Tester()
  {
    cTest = new Tester();
  }
}


感谢您的建议。我真的用静态构造函数做到了。但因为egualy不工作我使用普通构造函数来保持尽可能正常。



睡了一晚我的电脑处于睡眠模式以下早上每件事都像预期的那样。我只给了Tester构造函数一个类型为(int i)的参数,并根据这个改变了调用。我想看看我是否可以在调试时查看这种类型的表达式。但经过这一小小的改变,一切都很好。重建不可能有太大变化,因为我做了很多微小的改动以获得解决方案。



之后我删除了附加参数,在早上添加,它工作正常。



我真的不知道发生了什么。今天,虽然任何事情都是自动运作的,但解决这些问题很复杂。
Thank you for your suggestion. I really did it with a static constructor. But because egualy not working I used a "normal" constructor to keep things as "normal" as possible.

After sleeping one night having my computer in sleep mode the following morning every thing works like expected. I only gave the Tester constructor a parameter of type (int i) and changed the calls according to this. I wanted to see if I could view into this type of expression while debugging. But after this little change, anything was fine. The rebuilding could not have changed much, because I have made a lot of little changes ever to get some solution.

After that I removed the additional parameter, added in the morning, it works fine.

I really have no idea what happened. Today, while anything works automatically, its complicated to fix such problems.


这篇关于Xamarin cross-plattform XAML表单应用程序中的C#静态私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆