In the following code I'm declaring application level static
variable and then accessing it from MainWindow.xaml.cs
. Should using static variable here be avoided and instead instantiate the App class
in the MainWindow.xaml.cs
code below first and use the variable there as var app = Application.Current as App; app.myVariable = "Some Value";
. I've read at some places (such as here) that generally
we should avoid using static variables.
App.xaml.cs:
public partial class App : Application
{
private static string myVarialbe {get; set;}
...........
}
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
..........
private void btnUserUpdate_Click(object sender, RoutedEventArgs e)
{
App.myVariable = "Some value";
......
}
}