简体   繁体   中英

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.

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.

Pardon me if I am too Basic.

This I have defined in a page named "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"

        <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". So I tried a code something like this in "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. 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.

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. 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. 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.

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).

XAML was designed around this pattern. It's the natural way of doing things in XAML, a perfect fit and the perfect platform to learn the pattern.

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:

Functional Reactive Programming and OOP/MVVx (the x is for whatever).

Examples are ReactiveUI, Reactive Extensions, PRISM, Caliburn.Micro and many more. They can be combined, the same way you can combine traditional event-driven/event callbacks with MVVM and/or Reactive Programming. However, I would advise against it.

I'll start with the most documented way.

Look at Data binding for Windows Phone 8 . It was the first result when I googled "windows phone 8 xaml data binding," and deals with Colors and controls.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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