我无法在 Xamarin 中访问代码隐藏的 Web 窗体控件

I cannot access Web Form controls on code-behind in Xamarin

我在 Xamarin IDE 上创建了新的 ASP.NET (C#) 项目。我的默认页面有一些 asp 控件,例如标签、文本框和按钮。当我尝试从代码隐藏中访问它们时出现此错误:

error CS0103: The name xxxx does not exist in the current context.

这是我尝试访问它们的方式:

usernameTXT.Text = "John";

这是我的代码隐藏:

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace advise
{

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            usernameTXT.Text = "John";
        }
    }
}

这是我在 aspx 页面中的控件,它看起来像:

<asp:TextBox id="usernameTXT" runat="server" CssClass="control"></asp:TextBox>

值得一提的是xxx.aspx.designer.cs:

using System;
using System.Web;
using System.Web.UI;

namespace advise
{

    public partial class Default
    {

    }
}

请帮忙!我试图搜索这个问题但没有任何帮助。这是一件奇怪的事情,因为代码隐藏自动与 Web 窗体相关联,就像 Xamarin 为我做的那样。

error CS0103: The name xxxx does not exist in the current context.

"protected System.Web.UI.WebControls.TextBox usernameTXT;" 是否存在于自动生成的 XXXX.aspx.designer.cs 文件中?

  • 如果没有,"Rebuild All"再检查一次。

如果 XXXX.aspx.designer.cs(XXXX 是您的页面名称,Default 在您的例子中)没有自动更新以匹配您的 asp: 定义,那么您在你的 aspx/xml 文件。

删除(或注释掉)导致编译错误的定义和 Run 项目,生成的 Web 浏览器页面将向您显示解析错误以及有关错误发生位置的信息 .aspx 文件:

示例:

System.Web.Compilation.ParseException
Expecting </asp:TextBox> System.Web.UI.WebControls.TextBoxControlBuilder

Description: Error parsing a resource required to service this request. Review your source file and modify it to fix this error.
Details: Expecting </asp:TextBox> System.Web.UI.WebControls.TextBoxControlBuilder
Error origin: Parser
Error source file: /Users/administrator/Documents/Code/Xamarin/aspnet/aspnet/Default.aspx
Exception stack trace:
   at System.Web.Compilation.AspGenerator.Parse (System.IO.TextReader reader, System.String filename, Boolean doInitParser) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/AspGenerator.cs:line 627
   at System.Web.Compilation.GenericBuildProvider`1[TParser].Parse () in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 87
   at System.Web.Compilation.GenericBuildProvider`1[TParser].GenerateCode () in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 102
   at System.Web.Compilation.GenericBuildProvider`1[TParser].GenerateCode (System.Web.Compilation.AssemblyBuilder assemblyBuilder) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 121
   at System.Web.Compilation.BuildManager.GenerateAssembly (System.Web.Compilation.AssemblyBuilder abuilder, System.Web.Compilation.BuildProviderGroup group, System.Web.VirtualPath vp, Boolean debug) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs:line 778
Error source context:
Error lines: 11, 11
9:      <asp:Button id="button1" runat="server" Text="Click me!" OnClick="button1Clicked" />
10:         <asp:TextBox id="usernameTXT" runat="server" CssClass="control">foobar</asp:TextBox>
11:         <asp:TextBox id="userneTXT" runat="server" CssClass="control">foobar</asp:TextBoxz>
12:     </form>
13: </body>