2

我想通過restsharp將用戶的數據傳輸回服務器。以下是我用過的代碼。我收到一個錯誤,「名稱'InitializeComponent'在當前上下文中不存在」在用戶和服務器之間傳遞數據

我該如何調試?有沒有邏輯錯誤?

'using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using RestSharp; 
using Newtonsoft.Json.Linq; 
using Newtonsoft.Json; 
using System.Text.RegularExpressions; 
using System.Collections.Generic; 
using System.Linq; 


namespace Miser_sApp 
{ 
    public class RestSharp 
    { 
     public partial class SearchResults : PhoneApplicationPage 
     { 
      public static float _latitude, _longitude; 
      public static DateTime _date, _time; 
      public static string _name, _details; 
      public static int _number, _amount, _uid; 
      RestClient client; 
      RestRequest request; 

      int index; 
      connect Connect; 
      public SearchResults() 
      { 
       InitializeComponent(); 
       load(); 

      } 
      private RestClient InitializeX() 
      { 
       var client = new RestClient("//localhost" + Panorama1.username1 +    "&pro_type=" + Panorama1.number1 + "&pro_name=" + Panorama1.details1 + "&lat=" + Panorama1.lat1 + "&lon=" + Panorama1.lon1); 
       return client; 
       // request = new RestRequest(Method.GET); 
      } 
      public void load() 
      { 
       client = InitializeX(); 
       //MessageBox.Show("InternalInitialize Done"); 
       request = new RestRequest(Method.POST); 

       client.ExecuteAsync<connect>(request, (response) => 
       { 
        // MessageBox.Show(response.Data.ToString()); 
        Connect = response.Data; 
        if (response.Data == null || Connect.last == null || Connect == null) 
        { 
         MessageBox.Show("NO ITEMS FOUND"); 
         NavigationService.Navigate(new Uri("/Personal.xaml", UriKind.Relative)); 

        } 
        // name1.Items.Add(rootObject3.Details[0].ProductName); 
        //results.Items.Clear(); 
        //int i = 0; 
        else 
         foreach (var row3 in Connect.last) 
         { 
          results.Items.Add("NAME-" + row3.Name + '\n' + "Details-" + row3.Details + '\n' + "Date-" + row3.Date + '\n' + "Amount-" + row3.Amount + '\n' + "userId-" + row3.Uid + '\n' + "---------------------------------------------------------------"); 
         } 
       }); 

      } 




      private void Button_Click(object sender, RoutedEventArgs e) 
      { 

       Konnect d = Connect.last[index]; 
       _name = d.Name; 
       _date = d.Date; 
       _time = d.Time; 
       _uid = d.Uid; 
       _amount = d.Amount; 
       _details = d.Details; 
       _latitude = d.Latitude; 
       _longitude = d.Longitude; 

       NavigationService.Navigate(new Uri("/Panaroma1.xaml", UriKind.Relative)); 


      } 

      private void results_SelectionChanged(object sender, SelectionChangedEventArgs e) 
      { 
       index = results.SelectedIndex; 
      } 

     } 
     public class Konnect 
     { 
      public string Name { get; set; } 
      public DateTime Date { get; set; } 
      public DateTime Time { get; set; } 
      public int Uid { get; set; } 
      public String Details { get; set; } 
      public int Amount { get; set; } 
      public float Latitude { get; set; } 
      public float Longitude { get; set; } 
     } 

     public class connect 
     { 
      public List<Konnect> last { get; set; } 
     } 

    } 
} 

回答

0

這意味着InitializeComponent();方法不存在。在下面的代碼中。這可能是一個參考問題。這也可能意味着設計器文件出現問題,因爲它需要Designer支持。當您運行調試器時,您可以嘗試進入該方法。此外,錯誤發生在哪裏?這是運行時錯誤還是在您嘗試編譯時發生?如果這是一個編譯錯誤,則意味着該方法不存在,並且您需要創建該方法或刪除該方法中的引用。

public SearchResults() 
{ 
    InitializeComponent(); 
    load(); 

} 

編輯:

你都可能丟失這部分在designer.cs文件。請注意,這是基於勝利表單應用程序。你將需要研究這個方法對於你想要繼承的類的位置。即PhoneApplicationPage類

#region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.Text = "Form1"; 
    } 
    #endregion 

我剛創建了一個新的應用程序,它自動爲我生成了這個。你必須刪除它或其他東西。

+0

在編譯期間發生該錯誤。你用什麼方法不存在?當我們創建一個新類時,這是由Visual Studio自己創建的。 – nik

+0

這意味着.designer.cs文件或其他東西有問題。您正確的視覺工作室爲您生成該方法。我可以建議的是創建一個新的應用程序,並在.designer.cs文件下看看它是一種方法。看看我上面的編輯。 –

+0

designer.cs文件位於何處? – nik

相關問題