microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 678 forks source link

Inherits TabViewItem crash with Microsoft C++-Exception: winrt::hresult_error #4900

Closed AndyMahub closed 1 year ago

AndyMahub commented 3 years ago

Hi,

i have created only a empty MyTabViewItem-Class with inherits to TabViewItem like this:

using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.UI.Xaml.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VereinsOrdner { public class MyTabViewItem : TabViewItem { } }

Nothing more. Then i have in MainWindow-Class this method:

public void OpenNewTab(Type Page, string Header, PageParameters p = null) { MyTabViewItem t = new MyTabViewItem(); t.Header = Header; t.IsClosable = true; Frame frame = new Frame(); frame.Navigate(Page, p); t.Content = frame; Tabs.TabItems.Add(t); //Tabs.SelectedItem = t; }

Okay, i start my app and after the OpenNewTab-Method i receive this message:

Ausnahme ausgelöst bei 0x75E0A6E2 (KernelBase.dll) in VereinsOrdner.exe: WinRT originate error - 0x800F1000 : 'ListViewItemPresenter can only be used as the first child in the template for a ListViewItem.'. onecore\com\combase\winrt\error\restrictederror.cpp(1016)\combase.dll!770411A5: (caller: 76F57BB6) ReturnHr(1) tid(7c5c) 8007007E Das angegebene Modul wurde nicht gefunden. Ausnahme ausgelöst bei 0x75E0A6E2 in VereinsOrdner.exe: Microsoft C++-Ausnahme: winrt::hresult_error bei Speicherort 0x0317D890.

I don't know, is this my fault, or is this a bug? I would like to expand the TabViewItem with a few own functions.

Thanks for your time and help Andy

Version Info

NuGet package version: Microsoft.ProjectReunion 0.5.0

Windows 10 version Saw the problem?
Insider Build (xxxxx) No
May 2020 Update (19041) Yes
AndyMahub commented 3 years ago

FYI:

But app crashes after add a new TabViewItem. I have no any idea why.

AndyMahub commented 3 years ago

STOP! I have found the problem, and i'm so proud, sorry :-D

Okay, what have helped me: `

</TabView.TabItems>`

That's is. After this, i can add my "own" TabItems without crash the app.

I say it again, i don't know, is this a bug, or my fault, but this here works for me :-)

Best regards Andy

andrewleader commented 3 years ago

@MikeHillberg should this have failed at compile time rather than runtime and is a bug? Otherwise Andy figured out the problem and isn't blocked, but transferring to WinUI to see if there's anything additional to be done at compile-time here!

StephenLPeters commented 3 years ago

@karenbtlai who saw something similar to this recently as well

StephenLPeters commented 3 years ago

@beervoley the tabview expert

beervoley commented 3 years ago

@AndyMahub did you have any TabViewItems in XAML markup inside TabView by default before your fix? I can't say for sure why this happens, but you can't have different "classes" of TabViewItem present at the same time inside the TabView. If you have any TabViewItem <TabViewItem>....</TabViewItem> inside your TabView in XAML markup and then you try to add your "own" TabViewItem - the TabView will crash :) As you correctly did, included local:TabViewItem to point to your own TabViewItem instead of controls:TabViewItem :)

AndyMahub commented 3 years ago

@beervoley No, in the XAML was nothing between <TabView>-Tabs. Only Attributes like Background and x:Name.

After a long search, i have add a empty "own Tab"(in the XAML) and it works.

You can reproduce this. Create a own (empty) class with inherits TabViewItem, create a empty XAML without TabViewItem-Tags and in Code-Behind you add a Tab from the inherited class. App crashes. But if you add a empty Tab in XAML and remove this after app starting in code behind, it works. For me.

FreddyDgh commented 3 years ago

Any ideas for a workaround if your TabViewItem-derived class can't be instantiated in XAML (i.e. no default constructor)?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.