在XAML Metro应用程序中进行数据绑定时的ComException

时间:2012-02-01 00:07:53

标签: c# xaml data-binding microsoft-metro

我在将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个字符串简化了PlayList,但仍有相同的问题。

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; }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是由于未在主线程上设置UI元素。我用

修复了它
PlayListView.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) => 
{ 
    PlayListView.DataContext = Playlists; 
}, PlayListView, null);