简体   繁体   English

如何传递给泛型并将泛型传递给另一个类?

[英]How to pass to a Generic and Pass from a Generic to another class?

When I log into my application, I pass the cUserEntity class which holds all the details of the logged in user, including UserID and Username, to the Dashboard form. 登录我的应用程序时,我将cUserEntity类传递到cUserEntity该类包含已登录用户的所有详细信息,包括UserID和Username。 From here, I can continue to pass the details around from class to class and form to form. 从这里开始,我可以继续在每个班级和每个窗体之间传递详细信息。 For example (Ignore the generic bit in this example) : 例如(忽略此示例中的通用位)

Login: 登录:

 xamlDashboard Manager = new xamlDashboard(_loggedInUser);
 Generic Gen = new Generic(_loggedInUser);
 Manager.Show();

Dashboard: 仪表板:

cUserEntity _loggedInUser;

  public xamlDashboard(cUserEntity loggedInUser)
    {

        InitializeComponent();

        _loggedInUser = loggedInUser;
    }

However, I have a Generic.Xaml page which creates a button at the top of every window. 但是,我有一个Generic.Xaml页面,该页面在每个窗口的顶部创建一个按钮。 Behind the Generic.xaml is a Generic class, which holds the click_event for the created button. 后面Generic.xaml是一个通用类,其保持click_event为创建的按钮。 The click_event opens a new window to another form. click_event将打开一个新窗口,可打开另一个表单。

Now, I need that other form to have the logged in user details, and to do that, I assume I need to pass to the Generic.Xaml and then pass from there to the new form via the click_event . 现在,我需要其他表单来具有已登录的用户详细信息,并且为此,我假定我需要传递给Generic.Xaml ,然后通过click_event从那里传递给新表单。 However, as I've read up and noticed, it doesn't seem to be possible as you can't pass a type to a Generic during runtime. 但是,正如我阅读并注意到的那样,这似乎是不可能的,因为您无法在运行时将类型传递给Generic。

What I hoped to achieved (which failed) : 我希望达到的目标(失败了)

public partial class Generic
{
    cUserEntity _loggedInUser;

    public Generic(cUserEntity loggedInUser)
    {
        _loggedInUser = loggedInUser;
    }

    private void btnHelp_Click(object sender, RoutedEventArgs e)
    {
        xamlHelp help = new xamlHelp(_loggedInUser);
        help.Show();
    }
}

Therefore, what is the best and most efficient method to be able to do this, and would appreciate examples, if possible. 因此,什么是能够做到这一点的最好,最有效的方法,如果可能的话,我们将欣赏一些示例。

It would be alot simpler to create a singleton object to store your logged in user... 创建一个单例对象来存储您登录的用户会更简单...

public class UserAccount
{
   private static User _currentUser;

   private UserAccount() {}

   public static User CurrentUser
   {
      set
      {
         _currentUser = value;
      }

      get 
      {
         return _currentUser;
      }
   }
}

Then after login you would do this... 然后,登录后您将执行此操作...

// Set the current User
UserAccount.CurrentUser = user;

Then in any class you need the currently logged in user... you could do... 然后,在任何课程中,您都需要当前登录的用户...您可以这样做...

var user = UserAccount.CurrentUser;

Obviously you would need to implement your own business rules around this but the concept is what I am trying to get across here, a static single instance of the user that can be accessed from anywhere. 显然,您需要围绕此实现自己的业务规则,但是这个概念是我想要在此处介绍的内容,可以从任何地方访问该用户的静态单个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM