我想访问一个变量值,该值在按钮单击时在main函数内初始化。请先检查我的代码......
{
public string user { get; private set; }
public Dashboard(String loggedinUser)
{
InitializeComponent();
var user = loggedinUser;
f1(user);
// WORKING WELL WHEN I'M CALLING f1() FROM HERE.
}
private void f1(String usr)
{
Dock_MainPanel.Children.Clear();
Passuc psuc = new Passuc(usr);
Dock_MainPanel.Children.Add(psuc);
}
private void dash_nav_pw_Click(object sender, RoutedEventArgs e)
{
// USER VALUE DROPPING HERE. HENCE NULL VALUE IS PASSED TO FUNCTION
f1(user);
}
我想访问存储在仪表板功能中的用户值,并在下次调用dash_nav_pw_Click()中的函数 f1 时传递。一旦分配给用户变量,如何使其成为全局变量和值应该是静态的?
答案 0 :(得分:0)
将var user = loggedinUser;
更改为user = loggedinUser;
第一个在函数内部创建一个新变量,后者写入已存在的外部变量。
答案 1 :(得分:-1)
由于用户是您班级的公共财产,因此已经可以访问。不要将其设置为仪表板内的局部变量,只需将值赋给属性即可。您没有提供有关如何使用此值的信息,但您可以直接从属性中获取值。
{
public string user { get; private set; }
public Dashboard(String loggedinUser)
{
InitializeComponent();
//set the value of the user property here
user = loggedinUser;
f1(user);
}
private void f1(String usr)
{
Dock_MainPanel.Children.Clear();
Passuc psuc = new Passuc(usr);
Dock_MainPanel.Children.Add(psuc);
}
private void dash_nav_pw_Click(object sender, RoutedEventArgs e)
{
//Once the user property value was set by Dashboard, you can get it here
f1(user);
}