简体   繁体   English

XAML Metro应用程序中的数据绑定时出现ComException

[英]ComException while Data Binding in XAML Metro app

I am getting a COM Exception while binding ListView to a List items. 将ListView绑定到列表项时出现COM异常。 The Exception is being thrown from the 2nd line here. 从第二行抛出异常。 "A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in app.exe “ app.exe中发生了类型'System.Runtime.InteropServices.COMException'的第一次机会异常

Additional information: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))" 附加信息:灾难性故障(HRESULT的异常:0x8000FFFF(E_UNEXPECTED))

Playlists = content.getPlayLists();
PlayListView.DataContext = Playlists;

XAML declaring the binding 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>

I simplified the PlayList by just having 2 strings but still has the same issue. 我仅用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; }
    }
}

The problem is due to not setting the UI element on main thread. 问题是由于未在主线程上设置UI元素。 I fixed it using 我用修复

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM