Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting ItemsSource of derived ListBox throws "Catastrophic failure"

I'm developing for Windows 8 WinRT framework. The following sample code throws an exception:

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

Is this one more bug in the current WinRT framework (I'm using VS11 and Consumer Preview)? Has someone an idea how to solve this problem?

Btw: I have tested the same code with Windows Phone 7.5 Silverlight and it works without problems...

Thanks for your help.

public class MyListBox : ListBox
{

}

public sealed partial class BlankPage : Page
{
    public BlankPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var box1 = new ListBox(); 
        box1.ItemsSource = new List<Object> { new Object() }; // works without problems
        Content = box1; 

        var box2 = new MyListBox();
        box2.ItemsSource = new List<Object> { new Object() }; // throws exception
        Content = box2; 
    }
}
like image 724
Rico Suter Avatar asked Apr 13 '12 21:04

Rico Suter


1 Answers

I encountered a similar problem when subclassing ListView. In my case the following approach partially helped: I stopped trying to set ItemsSource of my ListView directly in the code behind but instead I created CollectionViewSource in XAML like:

<UserControl.Resources>
    <CollectionViewSource x:Name="myCollectionViewSource"/>
</UserControl.Resources>
...
...
<ListView
    ...
    ItemsSource="{Binding Source={StaticResource myCollectionViewSource}}" />

And in the code behind I set

this.myCollectionViewSource.Source = new List<Object> { new Object() }; // The real data source respectively

However this seems only to postpone the problem. At least in my case. In my real example I use ObservableVector as the data source. And as soon as any change of ObservableVector collection is performed (for example Clear) I also experience Catastrophic failure (0x8000FFFF). As soon as I use the original ListView (not my subclassed version) all works fine again - exactly as in your case. So my reply cannot be understood as the solution of the problem but maybe it is a hint worth trying. In my case the original assignment works fine, the problems come first after observable collection tries to update. I experimented with ObservableCollection (should work in CP, it did not in DP) but there I faced other problems. It would be interesting to hear whether you was able to make any progress down this road...

like image 168
Jan Zeman Avatar answered Oct 21 '22 04:10

Jan Zeman