我正在使用LongListSelector控件和ItemRealized事件,它的工作正常,但是當我更新分配給LongListSelector的ItemSource的列表時,LongListSelector中的項目不會更新。longlistselector中的自動更新
以下是我在XAML中使用的代碼。
<phone:LongListSelector SelectionChanged="listBoxGroups_SelectionChanged_1" Grid.Row="1" x:Name="longListSelector" ItemTemplate="{StaticResource groupsItemTemplate}" Margin="0,100,0,0">
</phone:LongListSelector>
在代碼後面。
public GroupsMainPage()
{
InitializeComponent();
longListSelector.ItemRealized += longListSelector_ItemRealized;
this.Loaded += GroupsMainPage_Loaded;
}
void longListSelector_ItemRealized(object sender, ItemRealizationEventArgs e)
{
if (!controller.IsLoading && longListSelector.ItemsSource != null && longListSelector.ItemsSource.Count >= 100)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
if ((e.Container.Content as Result).Equals(longListSelector.ItemsSource[longListSelector.ItemsSource.Count -7]))
{
connectToWebService();
}
}
}
}
從Web服務接收到響應後,我只需執行以下操作。
private void WebServiceSuccessAction(Object obj)
{
GroupModel.getInstance().GetGroupResponse((GroupResponse)obj);
if (GroupModel.getInstance().GetGroupResponse().Response.errorCode == 0)
{
controller = new GroupController();
if (fetchNumberofGroups <= 100)
{
this.ResultList = controller.GetGroupList();
longListSelector.ItemsSource = this.ResultList;
}
else
{
controller.addNewGroupData();
this.ResultList = controller.GetGroupList();
}
}
else
{
List<Error> error = ConfigModel.getInstance().getConfigResponse().Response.data.errors;
foreach (Error err in error)
{
if (err.code == GroupModel.getInstance().GetGroupResponse().Response.errorCode)
{
MessageBox.Show(err.text);
}
}
}
}
WP8上的所有WebRequest回調均在後臺線程上完成。爲了在Web請求之後更新UI,您需要在UI分派器線程上。 – JustinAngel