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

本文关键字:隐藏 Web 窗体 控件 代码 访问 Xamarin | 更新日期: 2023-09-27 18:21:10

我在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为我做的那样

错误CS0103:当前上下文中不存在名称xxxx。

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

  • 如果没有,请"全部重建",然后再次检查

如果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>

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