我在我的網站項目Translator2.aspx和Translator2.aspx.cs中有2個文件。我想添加新的類(Erwin:Translator2)到我的Translator2.aspx.cs文件。然而這是錯誤這樣的:無法將類添加到aspx.cs文件
嚴重性代碼說明項目文件的線路抑制狀態
錯誤「translator2_aspx」不包含「開關」 並沒有擴展方法「開關」接受一個類型的第一個參數的定義 'translator2_aspx' 可以找到(是否缺少using指令 或程序集引用?)
C:\用戶\ erwin.surya \文檔\ Visual Studio的 2017年\網站已\的WebSite1 \ Translator2.aspx 33
這裏是我的Translator2.aspx代碼:
<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Translator2.aspx.cs" MasterPageFile="~/Site.Master" Inherits="Translator2" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="English" CssClass="col-md-2 control-label">English</asp:Label>
<div class="col-md-10">
<asp:TextBox Height="79px" TextMode="MultiLine" Width="452px" runat="server" ID="English" CssClass="form-control" />
</div> </div>
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="Submit" Height="39px" Width="100px" Text="Translate" CssClass="btn btn-default" />
</div>
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="Switch" Height="39px" Width="100px" Text="Switch" CssClass="btn btn-default" />
</div>
<br /><br /><br />
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Japanese" CssClass="col-md-2 control-label">Japanese</asp:Label>
<div class="col-md-10">
<asp:TextBox Height="79px" TextMode="MultiLine" Width="452px" runat="server" ID="Japanese" CssClass="form-control" />
</div></div>
<br /><br />
<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
<p class="text-danger">
<asp:Literal runat="server" ID="FailureText" />
</p>
</asp:PlaceHolder>
</asp:Content>
這裏是我的Translator2.aspx.cs代碼:
partial class Translator2 : Page
{
public async void Submit(object sender, EventArgs e)
{
string key = "92fcf1387f844a8";
var authTokenSource = new AzureAuthToken(key.Trim());
string authToken;
try
{
authToken = await authTokenSource.GetAccessTokenAsync();
}
catch (HttpRequestException)
{
if (authTokenSource.RequestStatusCode == HttpStatusCode.Unauthorized)
{
Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid.");
return;
}
if (authTokenSource.RequestStatusCode == HttpStatusCode.Forbidden)
{
Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded.");
return;
}
throw;
}
string output = "";
string text = English.Text;
string uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + HttpUtility.UrlEncode(text) + "&from=" + "en" + "&to=" + "ja";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add("Authorization", authToken);
using (WebResponse response = httpWebRequest.GetResponse())
using (Stream stream = response.GetResponseStream())
{
DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
string translation = (string)dcs.ReadObject(stream);
Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "ja");
Console.WriteLine(translation);
output = translation;
}
Japanese.Text = output;
}
class Erwin : Translator2
{
string from, to;
public void SwapStrings(string s1, string s2)
{
string temp = s1;
s1 = s2;
s2 = temp;
from = s1;
to = s2;
testing.Text = from;
testing1.Text = to;
}
protected void Switch(object sender, EventArgs e)
{
string str1 = testing.Text;
string str2 = testing1.Text;
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);
SwapStrings(str1, str2);
}
}
你有任何想法,爲什麼我不能添加歐文:Translato2類?在添加課程之前,一切正常。我錯過了什麼?
嗨,我不能將此類添加到當前aspx.cs文件?我以爲我們可以在一個aspx.cs文件中創建多個類? –
我想使用從,來自Erwin類的字符串,以便能夠在課堂上使用它Translator –
你*可以*在這裏有這個類,但你應該問爲什麼。如果它僅用於此頁面,請將其功能添加到頁面中。如果它被其他頁面使用,則將它放在單獨的文檔中是有意義的。你會發現每個類有一個.cs文件是合理的。 – Jonathan