VARIABLES WHICH NEED TO BE LIVE ACROSS THE APPLICATION IN WPF

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
WPF 
WPF
 1175  View(s)
Ratings:
Rate this:
I am developing a WPF application. I need some variables/information not to destroy until user closes that application. Which method is best? Static Class with static variables? Moreover what is the best practice in this scenario?
  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Variables which need to be live across the application in WPF

    Hi Royce,


    You can also create static class and reference it in xaml like this:

    namespace MyNamespace

    {

        public static class Globals

        {

            public static double SomeVariable { get { return 1.0; } }

        }

    }

    Then access it from xaml like this:

    <UserControl Width="{x:Static globals:Globals.SomeVariable}" />

    where globals is defined at top top of your xaml like following:

    <Window x:Class="MyNamespace.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:globals="clr-namespace:MyNamespace">

    </Window>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!