2014-04-27 66 views
1

我請求數據從服務器和我得到響應後,我顯示它們在網格和列表框-TextBlock(就像一張桌子)。直到這裏,每件事情都沒關係,我完成了顯示函數之後,我必須調用新的URL和wishlized新的JSON數據來更新我的網格表值例如: - 我的小應用程序請求登錄第一次登錄成功後,我請求新的URL檢索(JSON數組)的項目與出售價格和購買價格==>在這裏我畫我的網格與這個數據像表,因爲我告訴你之前我最終需要請求新的URL檢索只是在服務器上更改了新價格的項目===>我不知道如何在我的搜索網格textblock表來更新所需的行,請幫助我(請檢查我的代碼下面他們告訴我有一個INPC和for循環的錯誤,因爲當我請求第二個URL兩次新數據不是你在我的表pdated --->請諮詢)INPC和綁定數據在運行時窗口電話8


這在我的代碼,只有第二個URL調用我不知道如何實現第三個電話,如何搜索到我的表在運行時: -

public ObservableCollection<Data> DataReceivedCollection { get; set; } 

private void FireRequest2() 
{ 

    var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/getallsymbols?AccountID=1122336675")) as HttpWebRequest; 
    request.Method = "GET"; 
    request.CookieContainer = cookieJar; 
    request.BeginGetResponse(ar => 
    { 
     HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState; 
     using (var response = (HttpWebResponse)req2.EndGetResponse(ar)) 
     { 
      using (Stream stream = response.GetResponseStream()) 
      { 
       using (var reader = new StreamReader(stream)) 
       { 
        var outerRoot1 = JsonConvert.DeserializeObject<OuterRootObject1>(reader.ReadToEnd()); 
        JArray jsonArray = JArray.Parse(outerRoot1.d); 
        JToken jsonArray_Item = jsonArray.First; 
        while (jsonArray_Item != null) 
        { 

         string Name = jsonArray_Item.Value<string>("Name"); 
         string Bid = jsonArray_Item.Value<string>("Bid"); 
         string Ask = jsonArray_Item.Value<string>("Ask"); 
         string ID = jsonArray_Item.Value<string>("ID"); 



         DataReceivedCollection = new ObservableCollection<Data>(); 


         DispatchInvoke(() => 
         { 
          myList.ItemsSource = DataReceivedCollection; 
          // and to add data you do it like this: 
          DataReceivedCollection.Add(new Data() { symid = ID, textFirst = Name, textSecond = Bid, textThird = Ask }); 


         } 
); 

         //Be careful, you take the next from the current item, not from the JArray object. 
         jsonArray_Item = jsonArray_Item.Next; 
        } 



       } 
      } 

     } 

    }, request); 
} 

這裏是我的XAML: -

<Grid Background="#FFC9DC97" x:Name="ContentPanel" Grid.Row="1" Margin="12,140,12,0"> 
    <ListBox Name="myList" Background="#FFC9DC97"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="1*"/> 
         <ColumnDefinition Width="1*"/> 
         <ColumnDefinition Width="1*"/> 
         <ColumnDefinition Width="1*"/> 
        </Grid.ColumnDefinitions> 
        <TextBlock x:Name="ide" Text="{Binding symid}" Grid.Column="3" HorizontalAlignment="Center"/> 
        <TextBlock Text="{Binding textFirst}" Grid.Column="0" HorizontalAlignment="Left" Foreground="#FF1C69D8"/> 
        <TextBlock Text="{Binding textSecond}" Grid.Column="1" HorizontalAlignment="Center"/> 
        <TextBlock Text="{Binding textThird}" Grid.Column="2" HorizontalAlignment="Right"/> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

這裏是INotifyPropertyChanged的類

public class Data : INotifyPropertyChanged 
{ 
    private string _textFirst; 
    public string textFirst 
    { 
     [DebuggerStepThrough] 
     get { return _textFirst; } 
     [DebuggerStepThrough] 
     set 
     { 
      if (value != _textFirst) 
      { 
       _textFirst = value; 
       OnPropertyChanged("textFirst"); 
      } 
     } 
    } 
    private string _textSecond; 
    public string textSecond 
    { 
     [DebuggerStepThrough] 
     get { return _textSecond; } 
     [DebuggerStepThrough] 
     set 
     { 
      if (value != _textSecond) 
      { 
       _textSecond = value; 
       OnPropertyChanged("textSecond"); 
      } 
     } 
    } 

    private string _textThird; 
    public string textThird 
    { 
     [DebuggerStepThrough] 
     get { return _textThird; } 
     [DebuggerStepThrough] 
     set 
     { 
      if (value != _textThird) 
      { 
       _textThird = value; 
       OnPropertyChanged("textThird"); 
      } 
     } 
    } 

    private string _symid; 
    public string symid 
    { 
     [DebuggerStepThrough] 
     get { return _symid; } 
     [DebuggerStepThrough] 
     set 
     { 
      if (value != _symid) 
      { 
       _symid = value; 
       OnPropertyChanged("symid"); 
      } 
     } 
    } 
    #region INotifyPropertyChanged Implementation 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string name) 
    { 
     var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null); 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
    #endregion 
} 

請幫我

+0

簡化你的問題,沒有人會回答它,因爲它是。 – Aybe

+1

@Aybe,這是一個「俄羅斯娃娃」的問題。他最後的5個問題都在同一個申請中,每個答案之後,他都會陷入下一個問題。每個問題都不一樣,但隨着他的進步,它們會依次出現。閱讀他的歷史。而這:http://meta.stackexchange.com/questions/188625/etiquette-for-russian-doll-questions –

+1

+1他正在學習艱難的方式:D – Aybe

回答

2

在這個片段...

    DataReceivedCollection = new ObservableCollection<Data>(); 


        DispatchInvoke(() => 
        { 
         myList.ItemsSource = DataReceivedCollection; 
         // and to add data you do it like this: 
         DataReceivedCollection.Add(new Data() { symid = ID, textFirst = Name, textSecond = Bid, textThird = Ask }); 


        } 

你被重新初始化DataReceivedCollection銷燬所有以前的數據。所以它是空的。然後在調度程序線程中綁定到它,然後添加到它。並且你在每次傳遞或者(jsonArray_Item != null)循環中重複整個事情。

可觀察的收集和綁定應在初始化時設置爲一次。不是每次你經過一個循環。如果你想將集合設置爲空,使用`DataReceivedCollection.Clear();

將這些行一次性初始化...

DataReceivedCollection = new ObservableCollection<Data>(); 
myList.ItemsSource = DataReceivedCollection; 

而且,由於你是派遣「添加」(這是正確的),你邀請一個閉合的問題,即變量可能在分派器線程執行之前超出範圍。

將這些線在調度程序線程...

   string Name = jsonArray_Item.Value<string>("Name"); 
       string Bid = jsonArray_Item.Value<string>("Bid"); 
       string Ask = jsonArray_Item.Value<string>("Ask"); 
       string ID = jsonArray_Item.Value<string>("ID"); 

你實現INPC的看起來不錯。此時,您的邏輯流程需要調整。

+0

加里我在WPF聊天室裏你在那裏 – Jordan

+0

那裏現在,在那裏見到你 –