Adding StyleOn Style to App.xaml crashes on App () {InitializeComponent (); }

Adapting the project to Template10 , I am switching to inherited styles in App.xaml, crashing.

It is similar to Template10, it does not support inherited or extended styles. I tried to extend SubTitleStyle from TitleStyle, but I get COM exceptions in GetXamlType in XamlTypeInfo.g.cs

My app.xaml.cs

sealed partial class App : BootStrapper { public App() { InitializeComponent(); } public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args) { NavigationService.Navigate(typeof(ShellView)) await Task.CompletedTask; } } 

My app.xaml

 <Style x:Key="TitleStyle" TargetType="TextBlock"> <Setter Property="Foreground" Value="{StaticResource TextTitleForeground}"/> <Setter Property="FontSize" Value="26"/> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="FontWeight" Value="Medium"/> </Style> <Style x:Key="SubTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TitleStyle}"> <Setter Property="Foreground" Value="{StaticResource TextForeground}"/> <Setter Property="FontSize" Value="20"/> </Style> 

Exception Information:

 Error HRESULT E_FAIL has been returned from a call to a COM component. at System.Runtime.InteropServices.WindowsRuntime.IIterator`1.MoveNext() at System.Runtime.InteropServices.WindowsRuntime.IteratorToEnumeratorAdapter`1.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at Template10.Common.BootStrapper.<InitializeFrameAsync>d__77.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Template10.Common.BootStrapper.<InternalLaunchAsync>d__53.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore() 
+5
source share
1 answer

It was deeply funny for me. I could easily reproduce this. And then I could easily reproduce this without reference to Template 10. The violation code is this block in T10:

 // title foreach (var resource in Application.Current.Resources .Where(x => x.Key.Equals(typeof(Controls.CustomTitleBar)))) { var control = new Controls.CustomTitleBar(); control.Style = resource.Value as Style; } 

You can simplify this:

 var a = Application.Current.Resources.ToArray(); 

Posted in OnLaunched any application. Boom. The error itself occurs when we try to access a collection of resources, but only when the BasedOn style is added to the resources.

After sitting with the platform team to try and justify pattern 10, everyone around the table began to scratch their heads. And when I understood @dachibox, you found a real bug on the XAML platform.

Here are just the current workaround until we update template 10.

 <Page.Resources> <ResourceDictionary Source="..\Styles\Custom.xaml" /> </Page.Resources> 

I mean, you are doing the work on the page, not the application. So, how do we fix template 10 without fixing the XAML platform? Take a look at this awkward code that we will use in Bootstrapper:

 int count = Application.Current.Resources.Count; foreach (var resource in Application.Current.Resources) { var k = resource.Key; if (k == typeof(Controls.CustomTitleBar)) { var s = resource.Value as Style; var t = new Controls.CustomTitleBar(); t.Style = s; } count--; if (count == 0) break; } 

An error, at least in the iterator counter property, which seems to increase rather than decrease when you iterate over it. Crazy huh? It turns out that this iterative path is not a common use case. But now it doesnโ€™t matter, we raised the flag thanks to your question here.

I am updating Template 10 with a fix this week.

Good luck Jerry

+2
source

Source: https://habr.com/ru/post/1244600/


All Articles