XAML Metro应用程序中的数据绑定时出现ComException
本文关键字:定时 ComException 绑定 数据 Metro 应用程序 XAML | 更新日期: 2023-09-27 18:22:00
将ListView绑定到List项时出现COM异常。异常是从这里的第二行抛出的。"在app.exe 中发生类型为"System.Runtime.InteropServices.COMException"的首次机会异常
附加信息:灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED))"
Playlists = content.getPlayLists();
PlayListView.DataContext = Playlists;
XAML声明绑定
<ListView x:Name="PlayListView" ItemsSource="{Binding ElementName=Playlists}" Background="AntiqueWhite" SelectionChanged="PlayListView_SelectionChanged" HorizontalAlignment="Left" Height="364" Margin="56,268,0,0" VerticalAlignment="Top" Width="308">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我简化了播放列表,只使用了2个字符串,但仍然存在相同的问题。
public class PlayList
{
private string playListName;
private string description;
private DateTime dateTimeCreated;
private int numTracks;
private List<Track> tracks;
private string id;
public string Name
{
get { return playListName; }
set { playListName = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public DateTime CreatedDate
{
get { return dateTimeCreated; }
set { dateTimeCreated = value; }
}
public int NumberOfTracks
{
get { return numTracks; }
set { numTracks = value; }
}
public List<Track> Tracks
{
get { return tracks; }
set { tracks = value; }
}
public String Id
{
get { return id; }
set { id = value; }
}
}
问题是由于没有在主线程上设置UI元素。我用修复了它
PlayListView.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) =>
{
PlayListView.DataContext = Playlists;
}, PlayListView, null);