在App.xaml上添加BasedOn Style在App()上崩溃[InitializeComponent(); } [英] Adding BasedOn Style on App.xaml is crashing on App() { InitializeComponent(); }

查看:112
本文介绍了在App.xaml上添加BasedOn Style在App()上崩溃[InitializeComponent(); }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使项目适应 Template10 ,我会继承样式App.xaml中的文件崩溃了.

Adapting an project to Template10, I go into inherited styles in App.xaml are crashing.

它看起来像 Template10 ,不支持继承或扩展的样式.我试图从 TitleStyle 扩展 SubTitleStyle ,但是在 XamlTypeInfo.g.cs 中的 GetXamlType 上出现COM异常.

It looks like Template10, doesn´t support inherited or extended styles. I was trying to extend SubTitleStyle from TitleStyle but I get an COM exceptions on GetXamlType in XamlTypeInfo.g.cs

我的App.xaml.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;
    }
}

我的App.xaml

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>

异常信息:

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()

推荐答案

这对我来说是深深的困惑.我很容易就能重现这一点.然后,我无需参考Template 10就可以轻松重现此代码.令人讨厌的代码是T10中的该代码块:

This was deeply confounding to me. I was easily able to reproduce this. And, then, I was easily able to reproduce this without a reference to Template 10. The offending 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;
}

您可以将其简化为:

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

放置在任何应用程序的应用程序的OnLaunched中.繁荣.当我们尝试访问资源集合时(仅当将BasedOn样式添加到资源中时),错误本身就会出现.

Placed in the OnLaunched of any app's Application. Boom. The error itself comes when we are attempting to access the resource collection but only when a BasedOn style has been added to the resources.

与平台团队坐下来为模板10辩护后,桌子周围的每个人都开始挠头.并且,当我意识到@dachibox时,您已经在XAML平台中发现了一个真正的错误.

After sitting down with the platform team to try and vindicate Template 10, everyone around the table started scratching their heads. And, that's when I realized @dachibox, you have discovered a genuine bug in the XAML platform.

这是当前唯一的解决方法,直到我们更新模板10.

Here's the only current workaround until we update Template 10.

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

我的意思是,您在页面中而不是在App中完成工作.那么,如何在不修复XAML平台的情况下修复模板10?看看我们将在Bootstrapper中使用的这个奇怪的代码:

What I mean is, you do the work in the page instead of in App. So, how will we fix Template 10 without the XAML platform getting fixed? Take a look at this wonky code we will be using 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;
}

至少,该错误存在于迭代器的count属性中,该属性似乎在您遍历迭代器时增加而不是减少.疯了吧?事实证明,这种迭代路径不是常见的用例.但是,现在没关系,由于您在这里提出的问题,我们已经升起了国旗.

The error, at least, is in the iterator's count property which seems to increment instead of decrement as you iterate through it. Crazy huh? Turns out, this iteration path is not a common use case. But, that does not matter now, we've raised the flag thanks to your question here.

我将在本周的某个时间使用修复程序更新模板10.

I'll update Template 10 with the fix sometime this week.

祝你好运,杰里

这篇关于在App.xaml上添加BasedOn Style在App()上崩溃[InitializeComponent(); }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆