2017-08-17 107 views
1

我在我的網站項目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類?在添加課程之前,一切正常。我錯過了什麼?

回答

0

Switch方法與爲aspx頁面供電的類不在一個類中。如果你想轉換成某種實用方法,你應該創建一個新的類,也許Erwin在一個完全獨立的文件(Erwin.cs)中,並且有一個switch方法,它是公共的,可能是靜態的。我還注意到,switch不會返回任何內容,所以不知道你用它來完成什麼。

您的按鈕的OnClick處理程序應位於代碼後面 - 即在Translator2類中。在此處理程序中,您可以撥打Erwin.Switch()

+0

嗨,我不能將此類添加到當前aspx.cs文件?我以爲我們可以在一個aspx.cs文件中創建多個類? –

+0

我想使用從,來自Erwin類的字符串,以便能夠在課堂上使用它Translator –

+0

你*可以*在這裏有這個類,但你應該問爲什麼。如果它僅用於此頁面,請將其功能添加到頁面中。如果它被其他頁面使用,則將它放在單獨的文檔中是有意義的。你會發現每個類有一個.cs文件是合理的。 – Jonathan

相關問題