好的,所以我有一个IEnumerable
集合,我将其x:Bind
放入ListView
.
当应用程序运行时,只要此IEnumerable
列表更改,我listview
也希望更新。即使在使用时也并非如此INotifyPropertyChanged
,因此我决定将其转换IEnumerable
为ObservableCollection
.
我发现存在的演员是:
myCollection = new ObservableCollection<object>(the_list);
这在不同的问题中可以正常工作,但在我的情况下,我是 x:Bind this to thelistview
并且每次我使用上述代码运行该方法时,都会创建一个新引用并且绑定将不起作用(它不起作用) . 那是对的吗 ?如果是,我该如何解决这个问题?如果没有,我做错了什么?谢谢你。
目前我的代码如下所示:
public ObservableCollection<IMessage> MessageList;
private async Task SetMessages()
{
MessageList = new ObservableCollection<IMessage>(await channel.GetMessagesAsync(NumOfMessages).Flatten());
}
什么对我有用:
完全感谢 Marian Dolinský 的回答,现在一切正常。我提出了一些我的代码以使其更清楚我所做的是:
class ChatViewModel : INotifyPropertyChanged
{
//Number of messages to have in list at once.
private int NumOfMessages = 20;
//Called when a user clicks on a channel.
public async Task SelectChannel(object sender, ItemClickEventArgs e)
{
//Set the channel to clicked item
channel = (SocketTextChannel)e.ClickedItem;
//Receive the messages in the list.
IEnumerable<IMessage> data = await channel.GetMessagesAsync(NumOfMessages).Flatten();
//Clear the observablecollection from any possible previous messages (if for example you select another channel).
messageList.Clear();
//Loop and add all messages to the observablecollection
foreach (var item in data)
{
messageList.Add(item);
}
}
//The event handler when a message is received.
private async Task Message_Received(SocketMessage arg)
{
//Calls to add message only if the message received is of interest.
if (arg.Channel == channel)
{
//Sets the thread equal to the UI thread.
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
//Adds the new message, removes the oldest to keep always 20.
messageList.Add(arg);
messageList.Remove(messageList[0]);
});
}
}
private ObservableCollection<IMessage> messageList = new ObservableCollection<IMessage>();
public ObservableCollection<IMessage> MessageList
{
get
{
return messageList;
}
}
}
这是 XAML 中的代码:
<Page.DataContext>
<vm:ChatViewModel x:Name="ChatViewModel"/>
</Page.DataContext>
<ListView VerticalContentAlignment="Bottom" ItemsSource="{x:Bind ChatViewModel.MessageList, Mode=OneWay}" SelectionMode="None" ItemTemplate="{StaticResource MessageListDataTemplate}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0,0,5,5"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
再次感谢你的帮助 :)