我正在开发WP8应用程序,在从正常页面导航回枢轴页面后,我遇到了绑定静态observablecollection的问题。
这样的场景我称之为webservice然后我将收到的数据添加到observablecollection之后,我将mylist.itemsource =我的observablecollection添加到我的枢轴内的列表框中,它的工作完美告诉我在导航时建立新页面的枢轴从新页面到我的数据透视页面,枢轴从头开始重新加载所有数据,以避免这个问题我制作标志来检查数据是否加载,所以当我使它工作正常但新数据添加到静态observablecollection时的问题它没有反映到UI(手机屏幕视图)。
请帮我解决这个问题和我的代码的这一部分: -
当应用程序从此处开始检查加载的数据时: -
public static ObservableCollection<DataTr> DataReceivedCollectionTr { get; set; }
private void load(object sender,RoutedEventArgs e)
{
if (!IsDataLoaded)
{
GetOpentPos1(accountID);
}
else
{
myListTrade.ItemsSource = DataReceivedCollectionTr;
}
}
这里是电话:
private void GetOpentPos (string accountID)
{
DataReceivedCollectionTr = new ObservableCollection<DataTr>();
myListTrade.ItemsSource = DataReceivedCollectionTr;
string result1;
try
{
var request = HttpWebRequest.Create(new Uri("http://MyIP/vertexweb10/webservice.svc/GetOpenPositions?AccountId="+accountID)) as HttpWebRequest;
request.Method = "GET";
if (request.Headers == null)
{
request.Headers = new WebHeaderCollection();
}
request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
request.CookieContainer = cookieJar2;
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 outerRoot4 = JsonConvert.DeserializeObject<OuterRootObject4>(reader.ReadToEnd());
JArray jsonArray = JArray.Parse(outerRoot4.d);
JToken jsonArray_Item = jsonArray.First;
DispatchInvoke(() =>
{
while (jsonArray_Item != null)
{
int PosID = jsonArray_Item.Value<int>("ID");
if (PosID > 0)
{
string SymbolNameTra = jsonArray_Item.Value<string>;
// Here I add the Data
DataReceivedCollectionTr.Add(new DataTr() { SymbolNameTr = SymbolNameTra});
}
jsonArray_Item = jsonArray_Item.Next;
}
});
}
}
}
}, request);
}
}
这是XAML代码: -
<Grid Background="#FF9AB5F9"
x:Name="TradePanel"
Grid.Row="1"
Margin="0,70,0,-5">
<ListBox Cursor="Hand"
BorderBrush="Black"
BorderThickness="1"
Name="myListTrade"
Background="#FF9AB5F9"
Margin="0,-17,0,321">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="25,25,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="0" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock x:Name="SymNameTr"
Text="{Binding SymbolNameTr}"
Grid.Row="0"
Grid.Column="0"
HorizontalAlignment="Left"
Foreground="White"
FontSize="18"
FontWeight="Bold"
Margin="5,5,0,0" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
如果需要任何其他代码,请帮助我告诉我,以便我可以编辑问题
答案 0 :(得分:1)
问题是你一直在打电话:
DataReceivedCollectionTr = new ObservableCollection<DataTr>();
myListTrade.ItemsSource = DataReceivedCollectionTr;
在多个地方。您为每个ListBox
分配名称而执行此操作,并且您已在多个位置执行此操作。这打破了绑定并重新分配,控件无法跟上它。相反,只需创建一个在创建控件时初始化的字段。
private static ObservableCollection<DataTr> _drCollTr = new ObservableCollection<DataTr>();
public static ObservableCollection<DataTr> DataReceivedCollectionTr
{
get
{
return _drCollTr;
}
}
然后调用DataRecievedCollectionTr
的{{1}}和.Add
函数来更新列表。最初设置ItemsSource后,您所要做的就是更改列表本身而不是控件的ItemsSource。
我知道这就是答案,因为我自己通过TeamViewer修复了你的代码:)