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

查看:32
本文介绍了在 App.xaml 上添加 BasedOn 样式在 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()

推荐答案

这让我深感困惑.我很容易重现这一点.然后,我无需参考模板 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="..StylesCustom.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 样式在 App() { InitializeComponent(); 上崩溃;}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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