【. NET deep Breathing "application context (AppContext)

Source: Internet
Author: User

In. NET 4.6, a new class called Appcontext, this guy, the technology content is not high, but in the programming time can be easily used. The app context allows you to define an identity (represented by a string), and you can toggle the state while the application is running. Equivalent to a switch with two states--true or false.

In fact, we can also implement such a class, is to use a static dictionary to store, key is the switch identifier, value is a bool value. However, if the. NET library has this thing, it's a lot easier, at least we don't have to do it ourselves.

All members of the Appcontext class are static, and the inference above is not false. Call the Setswitch method to set a switch identity, as well as the status of the identity. You can then use the Trygetswitch method to retrieve the status of a switch's identity elsewhere in the code. If the state is open, execute the a code, execute the B code if the state is closed, and execute the C code if the status ID does not exist.

This will make me think of conditional compilation, the application context, really and conditional compilation has a similar place, is to set a global identifier, and then the code can be used throughout the identification of identifiers. However, it differs from conditional compilation. Conditional compilation is a part of the code does not participate in the compilation, once the condition is changed will be recompiled. And Appcontext is done in the code itself, all the code will participate in the compilation, just at the runtime to judge.

For example, if I have a k program, then I define a context identifier called color for K. When you click a button on the window, the code detects the color identity, fills the ellipse with red if the identity is open, and fills the ellipse with gray if the identity is off.

Take a look at the following XAML:

    <StackPanelMargin= " a">        <CheckBoxContent= "Apply context switch"Margin= "3,9"Checked= "onchecked"unchecked= "onunchecked" />        <ButtonMargin= "10,5"Content= "Fill Ellipse"Click= "OnClick" />        <EllipseWidth= " the"Height= "All"Name= "ELP"Stroke= "Black"strokethickness= "2" />    </StackPanel>


Let's use a checkbox to select whether the app context ID is turned on.

The following is the event code for the checkbox:

        Private void Onchecked (object  sender, RoutedEventArgs e)        {            appcontext.setswitch ("color  "true);        }         Private void Onunchecked (object  sender, RoutedEventArgs e)        {            appcontext.setswitch (" Color " false );        }

The above code is only responsible for setting the identity state of the app context.

The following code handles the event of a button:

        Private void OnClick (object  sender, RoutedEventArgs e)        {            bool  b;             if (Appcontext.trygetswitch ("color" out b))            {                = b? Colors.Red:Colors.Gray;            }             Else             {                = colors.transparent;            }        }


The Trygetswitch method can get the state of an identity, the state value is stored in an out parameter, and if an identity does not exist (not set), the entire method returns false. Note that the return value of the Trygetswitch method is not an identified state value, see the method prototype:

static bool Trygetswitch (string switchname, out bool isenabled);

The return value of the method only indicates whether the switch state can be successfully obtained, and the state of the switch is stored by the isenabled parameter, and the parameter direction is out.

The results after the run are as shown.

Finally, you have to tell everyone, the Mid-Autumn festival do not eat too much moon cakes, three high food.

【. NET deep Breathing "application context (AppContext)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.