会话或全局变量 c# wpf [英] Session or Global Variables c# wpf

查看:33
本文介绍了会话或全局变量 c# wpf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个应用程序,我正忙着为大学做.用户在 MainWindow.xaml 上输入数据,我想让他们在此窗口中输入的数据可以在整个应用程序中访问(姓名、姓氏等.我知道我可以将数据解析到每个单独的窗口中,但我总共有 13 个窗口.

Good day, I have an app that I'm busy doing for university. A user enters data on the MainWindow.xaml, I want to make the data that they entered on this window accessible throughout the application (Name, Surname etc. I know that I can parse the data into each individual window but I have 13 Windows total.

有没有办法在 WPF 中创建一个全局或会话变量,让我可以在任何地方访问该数据.

Is there a way that I can make a global or session variable in WPF that will allow me to access that data anywhere.

我四处寻找答案,但使用类等似乎是一种糟糕的方法,我似乎找不到可行的解决方案.

I have looked around for answers but using classes etc seems to be a bad way to go and I cant seem to find a solution that will work.

提前致谢.

推荐答案

有没有办法在 WPF 中创建一个全局或会话变量,让我可以在任何地方访问该数据.

Is there a way that I can make a global or session variable in WPF that will allow me to access that data anywhere.

创建一个具有静态属性的类:

Create a class with static properties:

public class ApplicationContext
{
    public static string Name { get; set; }
    public static string Surname { get; set; }
}

然后,您可以从应用程序中的任何其他类访问这些属性:

You can then access these properties from any other class in your application:

//set:
ApplicationContext.Name = "name...";
//get:
string name = ApplicationContext.Name;

如果您出于某种原因不想创建自己的类,也可以使用 Application.Current.Properties 集合:

You could also use the Application.Current.Properties collection if you don't want to create your own class for some reason:

//set;
Application.Current.Properties["Name"] = "name";
//get:
string name = Application.Current.Properties["Name"].ToString();

这篇关于会话或全局变量 c# wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆