在 App.xaml 上添加 BasedOn 样式在 App() { InitializeComponent(); 上崩溃}

Adding BasedOn Style on App.xaml is crashing on App() { InitializeComponent(); }

使项目适应 Template10,我进入 App.xaml 中的继承样式正在崩溃。

看起来 Template10,不支持继承或扩展的样式。我试图从 TitleStyle 扩展 SubTitleStyle 但我在 GetXamlType 上收到 COM 异常 XamlTypeInfo.g.cs

我的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

<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 中的这个块:

// 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 样式已添加到资源时。

在与平台团队坐下来尝试证明模板 10 正确之后,table 周围的每个人都开始摸不着头脑。而且,就在那时我意识到@dachibox,你在 XAML 平台中发现了一个真正的错误。

这是我们更新模板 10 之前唯一的当前解决方法。

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

我的意思是,你在页面而不是在应用程序中完成工作。那么,在 XAML 平台未修复的情况下,我们将如何修复模板 10?看看我们将在 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;
}

至少,错误是在迭代器的计数 属性 中,当您迭代它时,它似乎是递增而不是递减。疯了吧?事实证明,这个迭代路径不是一个常见的用例。但是,现在没关系,我们已经升起旗帜,感谢您在这里的问题。

我将在本周某个时候用修复程序更新模板 10。

祝你好运,杰里