简体   繁体   English

该名称在当前上下文中不存在。 不上课

[英]The name does not exist in the current context. Not seeing class

I am very new to C# coding, so this may be very simple. 我对C#编码非常陌生,因此这可能非常简单。 In my Site.Master.cs, I have the following code: 在我的Site.Master.cs中,我有以下代码:

GetLoggedInUserProperties();
lblLoginUser.Text = string.Format("Welcome {0}", Session[SessionVars.UserName]);

In a class file, I have put the following in a Public Class: 在一个类文件中,我将以下内容放入了公共类:

void GetLoggedInUserProperties()
    {
        string sLoginId = Program.ExtractUserName(HttpContext.Current.Request.ServerVariables["LOGON_USER"]);
        HttpContext.Current.Session[SessionVars.LoginId] = sLoginId;

        VerifyInAD(sLoginId);
    }

There are no errors in the class file, but my Site.Master.cs cannot find the code in the class file. 类文件中没有错误,但是我的Site.Master.cs无法在类文件中找到代码。

I am sure there is a better way to do this, so feel free to let me know. 我相信有更好的方法可以做到这一点,请随时告诉我。 Also, the lblLoginUser does not seem to work either. 另外,lblLoginUser似乎也不起作用。 It has the same error. 它具有相同的错误。 I have tried recreating the label and deleting the designer file (which never came back). 我尝试过重新创建标签并删除设计器文件(从未恢复过)。 Not sure if this is related or not. 不知道这是否相关。

Thank you. 谢谢。

You need to make your methods public or protected to be visible from the markup. 您需要publicprotected您的方法,以使其从标记中可见。 So if you want to reference GetLoggedInUserProperties() from the markup, you'll need to change your method declaration to something like this. 因此,如果您想从标记中引用GetLoggedInUserProperties() ,则需要将方法声明更改为如下所示。

protected void GetLoggedInUserProperties()
{
}

Make your method public 公开您的方法

Here your class 这是你的课

public class ClassName
 {
       public void GetLoggedInUserProperties()
       {

       }

 }

public MainWindow()
{
                InitializeComponent();
                ClassName instance = new ClassName();
                instance.GetLoggedInUserProperties();

}

Or make your class static and call your method : 或者将您的类static并调用您的方法:

  public static class ClassName
   {
       public static void GetLoggedInUserProperties()
       {

       }

    }



 public MainWindow()
  {
                InitializeComponent();
                ClassName.GetLoggedInUserProperties();

   }

暂无
暂无

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

相关问题 名称“…”在当前上下文中不存在。 错误 - The name “…” does not exist in the current context. error “当前上下文中不存在名称‘DisplayAlert’。” Xamarin C# - “The name ”DisplayAlert“ does not exist in current context.” Xamarin C# 当前上下文中不存在名称 userInput。 为什么? - The name userInput does not exist in the current context. Why? 在此上下文中不存在名称“ e”。 - The name “e” does not exist in this context. 当前上下文中不存在名称“ABC...”。 c#SQL控制台程序 - The name "ABC..." does not exist in the current context. c# SQL Console program 如何在 C# 中的静态类中定义/设置变量? 获取错误“名称 [namespace.static_class.member] 在当前上下文中不存在。” - How to define/set variables in a static class in C#? Getting error 'The name [namespace.static_class.member] does not exist in the current context.' Xamarin Forms“...DisplayAlert 在当前上下文中不存在。” - Xamarin Forms “…DisplayAlert does not exist in the current context.” 当前上下文中不存在“会话”。 我该如何使用它? - 'Session' does not exist in the current context. How can I use it? C#error1:当前上下文中不存在名称“DateTimeStyles”。 error2:找不到类型或命名空间名称“CultureInfo” - C# error1:The name 'DateTimeStyles' does not exist in the current context. error2:The type or namespace name 'CultureInfo' could not be found 每次我尝试测试运行时都会遇到错误当前上下文中不存在名称“播放器”。 我找不到解决办法 - every time i try to test run i run in to an error The name 'Player' does not exist in the current context. i cant find a solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM