Closed Palatis closed 4 years ago
Thanks @Palatis for the report, moving it to the Win32 for initial triage as it's occurring with XAML Islands, though see now that the stack does have a toolkit component. Not sure where that reference is from.
@marb2000 @ocalvo @azchohfi thoughts?
I'm also running into this. I don't think we can call into CoreView in the context of a XAML Island.
For me, this was occurring because RadialGauge
has a static ThemeResource
field which sets up the event subscription to ColorValuesChanged
.
I think the effect of this is that any XAML Island application using the Windows Community Toolkit will crash on a color change.
I'll leave the fix up to you all, but I do have a workaround that I came up with to get my own app to stop crashing :-):
I run this in my app's main page Loaded
event:
`
var radialGaugeType = typeof(RadialGauge);
var themeListener = radialGaugeType
.GetField("ThemeListener", BindingFlags.NonPublic | BindingFlags.Static)
.GetValue(null) as ThemeListener;
themeListener.Dispose();
`
Calling Dispose
unregisters the event subscription to ColorValuesChanged
.
It won't help anyone who wishes to use RadialGauge
or MarkdownTextBlock
directly though.
Doing the repro steps and I can't repro the bug...
Doing the repro steps and I can't repro the bug...
maybe try switch system theme in the control panel? this is really theme related. it crashes on me everytime i switch theme.
sometimes my Windows 10 switch theme from dark to light and immediately back to dark randomly... i dunno why... but it does......
@azchohfi can you look at the ThemeListener helper here in the toolkit when you have time? Is there a different model we should be using that's more XAML Islands friendly due to it's use of CoreWindow
?
In UWP, the Application's RequestedTheme only changes in the constructor. So no sure what this helper is listening...
@marb2000 so what actually happens if the user switch theme from the control panel? my other UWP apps actually switch theme (like from light to dark or vise versa).
Gotcha! I get the scenario. now. You are listening the Window activation event to figure out whether the Theme has changed and then raise the event, am I right?
@Palatis can you attach/link a repro project where we can see the "Cannot create new view because main window is not created." exception, please?
Well you really just have to use the XamlIsland tutorial project and switch system theme... Because that is what I did when I get the issue...
OK. Let me change the title of the bug so it can be addressed properly . Bug description: XAML Islands doesn't respond Windows app themes changes (Dark, Light) dynamically, only at app start.
Repro:
Expected: The content of the XAML Island changes. Observed: The context of the XAML Island doesn't change.
This is crashing my XAML Islands application referencing the Windows Community Toolkit when changing the accent color on line 81 of ThemeListener.cs due to RunAsync:
CoreApplication.MainView?.CoreWindow?.Dispatcher?.RunAsync
Shouldn't this be using a DispatcherQueue
instead in a XAML Island context?
That's what I've been using elsewhere to run code on the UI Thread.
@nc4rrillo Yes, using DispatcherQueue is the recommendation for UWP and XAML Islands given it works everywhere. If this doesn;t fix your bug, please, open a new bug within the repo steps, adding a project that fails is really helpful. Thanks!
About the bug description: Unfortunately, we can't fix this bug in the OS, but we can do it in WinUI 3 😊
@marb2000 thank you! The fix for this will have to come specifically from WCT in this instance, though I’d be happy to offer up a PR.
cc: @michael-hawker
@nc4rrillo Thanks! I don't know if we have another issue tracking this specifically in the WCT repo, this issue may have started there, but I moved here originally due to the XAML Islands context, but there is indeed an issue in the toolkit. If you don't mind filing an issue there for tracking with the specific details you've called out from ThemeListener, that'd be great for tracking!
I know my colleague @azchohfi has also been looking into these scenarios across the toolkit as well, so just coordinate with him in case he's already started something; otherwise, always happy for another hand and a PR!
Describe the bug
I have no idea if this issue should go here (or to
Microsoft.Toolkit.Win32
), but since the source code is here, I post it here.I'm using
XamlIsland
in WPF to host my UWP custom control. However it occasionally crash withInvalidOperationException
says something like "Cannot create new view because main window is not created." (the actual message is "無法建立新的檢視,因為尚未建立主視窗", this is just a translation.) with a stacktrace:I haven't switch the theme, tho... I just leave it there running for several minute and it crashes.
Steps to Reproduce
Steps to reproduce the behavior:
Expected behavior
no problem
Screenshots
probably don't need one...
Environment
NuGet Package(s):
Package Version(s): 6.0.0
Windows 10 Build Number:
App min and target version:
Device form factor:
Visual Studio
Additional context
currently no