简体   繁体   English

CodeGo.net>如何从一个变量在其他类的类的类外部访问?

[英]c# - How to access a variable from outside its class in a method in some other class?

I am a beginner in c# programming and I am developing windows phone application after reading some tutorials. 我是C#编程的初学者,在阅读了一些教程之后,我正在开发Windows Phone应用程序。

My idea is when the user clicks a button in a windows page, some other button in other windows phone page must change color from red to green. 我的想法是,当用户单击Windows页面中的按钮时,其他Windows Phone页面中的其他按钮必须将颜色从红色更改为绿色。

Pardon me if I am too Basic. 如果我太基础,请原谅我。

This I have defined in a page named "IndexPage.xaml" 我已经在名为“ IndexPage.xaml”的页面中定义了

        <Button x:Name="One_green"
            Content="1"             
            Background="Green"
            Click="One_Click"
         />

        <Button x:Name="One_red"
            Content="1"             
            Background="Red"
            Click="One_Click"                       
         />

Now I see red color button in my window as green button is hidden in the back. 现在我在窗口中看到红色按钮,因为绿色按钮隐藏在背面。

Now, the following code is from another windows phone page "1.xaml" 现在,以下代码来自另一个Windows Phone页面“ 1.xaml”

        <Button Content="GO" Click="Button_Click"/>

Now when the user clicks the "GO" Button I want the button to change to red to green in "IndexPage.xaml". 现在,当用户单击“ GO”按钮时,我希望该按钮在“ IndexPage.xaml”中变为红色到绿色。 So I tried a code something like this in "1.xaml.cs" 所以我在“ 1.xaml.cs”中尝试了类似这样的代码

        private void Button_Click(object sender, RoutedEventArgs e)
        {  
        One_red.Visibility = Visibility.Collapsed;
        One_green.Visibility = Visibility.Visible;
        }

But I am not able to access the "One_red" or "One_green" button in the above code. 但是我无法访问上面代码中的“ One_red”或“ One_green”按钮。 Please shed me directions. 请给我指示。

Also I want that code to execute only once. 我也希望该代码仅执行一次。 (ie) when the IndexPage.xaml loads again I want that button to be green always. (即)当IndexPage.xaml再次加载时,我希望该按钮始终为绿色。

Thank you very much in advance. 提前非常感谢您。

Please tell me if some other details are required. 请告诉我是否需要其他详细信息。

You could define a public or internal static variable inside the "Index.xaml" class specifying what button will show on load until otherwise specified. 您可以在“ Index.xaml”类中定义一个公共或内部静态变量,指定在加载时将显示什么按钮,除非另行指定。 This variable could be accessed outside the class, and possibly even outside the project depending on the modifier chosen. 该变量可以在类之外访问,甚至可以在项目外部访问,具体取决于所选的修饰符。 The constructor of the "Index.xaml" class could have code to reset it to the default to ensure it only happens on the next creation of the page. “ Index.xaml”类的构造函数可以使用代码将其重置为默认值,以确保仅在下一次创建页面时发生。 If you aren't creating a new page everytime, you would have to put the default resetters in a method called when you want to bring it to foreground. 如果您不是每次都创建一个新页面,则当您要将默认重置器放到前台时,必须将其放置在称为的方法中。

It seems to me that you are trying to learn, rather than having a SPEC to follow and implement. 在我看来,您正在尝试学习,而不是遵循和实施SPEC。

Because of that, and because you are starting with C# in 2014 (almost 2015), it will be quite beneficial for you to jump straight to data-binding declarative over imperative, going MVVM (MVVx) over MVC (MVx). 因此,由于您从2014年(几乎2015年)开始使用C#,直接对数据绑定声明式进行强制性转换,而对MVC(MVx)进行MVVM(MVVx)转换将是非常有益的。

XAML was designed around this pattern. XAML是围绕此模式设计的。 It's the natural way of doing things in XAML, a perfect fit and the perfect platform to learn the pattern. 这是XAML中的一种自然的处理方式,是一种完美的选择,也是学习该模式的理想平台。

It requires lots of learning, thinking, and re-learning, but it will open your eyes to modern programming techniques. 它需要大量的学习,思考和重新学习,但是它将使您对现代编程技术大开眼界。

That said... there are too many ways of doing what you asked for, and while none are exactly wrong, there are 2 current trends in .Net/C#/MsTech which IMO are NOT a waste of your time: 就是说...有太多方法可以满足您的要求,尽管没有一种是完全错误的,但是.Net / C#/ MsTech中有2种当前趋势,而IMO并不浪费您的时间:

Functional Reactive Programming and OOP/MVVx (the x is for whatever). 功能性无功编程OOP / MVVx (x用于任何用途)。

Examples are ReactiveUI, Reactive Extensions, PRISM, Caliburn.Micro and many more. 例如ReactiveUI,Reactive Extensions,PRISM,Caliburn.Micro等。 They can be combined, the same way you can combine traditional event-driven/event callbacks with MVVM and/or Reactive Programming. 它们可以组合在一起,就像您将传统的事件驱动/事件回调与MVVM和/或响应式编程相结合一样。 However, I would advise against it. 但是,我建议不要这样做。

I'll start with the most documented way. 我将从最有据可查的方式开始。

Look at Data binding for Windows Phone 8 . 查看Windows Phone 8的数据绑定 It was the first result when I googled "windows phone 8 xaml data binding," and deals with Colors and controls. 这是我搜索“ Windows Phone 8 xaml数据绑定”并处理颜色和控件时的第一个结果。

If you follow that example and add a resource to your application, you are done. 如果您遵循该示例并将资源添加到您的应用程序,那么您就完成了。

Of course, you can still use event => onClick + static class to hold the value in between View instances, but if I was right on the assumption that you are trying to learn, I wouldn't go that route. 当然,您仍然可以使用event => onClick + static类在View实例之间保存值,但是如果我正确地假设您正在尝试学习,那么我就不会走这条路。

Sorry if I drifted. 对不起,如果我不满意。 :) :)

You may not be able to access the button click event because it is private, you may need to make it protected or public, the default access specifier would probably be ok as well. 您可能无法访问按钮单击事件,因为它是私有的,您可能需要将其设置为受保护的或公开的,默认访问说明符也可能没问题。

public void Button_Click(object sender, RoutedEventArgs e)

or default would be: 或默认值为:

void Button_Click(object sender, RoutedEventArgs e)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM