【问题标题】:Adding BasedOn Style on App.xaml is crashing on App() { InitializeComponent(); }在 App.xaml 上添加 BasedOn 样式在 App() { InitializeComponent(); 上崩溃; }
【发布时间】:2016-03-07 19:15:31
【问题描述】:

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

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

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

【问题讨论】:

  • 请编辑您的问题以包含您的实际源代码。不要链接到您的代码图片。至于您的问题,您应该更清楚运行代码时发生了什么。您是否收到任何错误消息?他们说什么?
  • 我用更多细节更新了我的问题。欢迎任何关于更多细节的建议:)。谢谢!

标签: xaml windows-10 win-universal-app template10


【解决方案1】:

这让我深感困惑。我很容易重现这一点。然后,我无需参考模板 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 辩护后,桌子周围的每个人都开始摸不着头脑。而且,那时我才意识到@dachibox,您在 XAML 平台中发现了一个真正的错误。

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

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

我的意思是,您在页面中而不是在 App 中完成工作。那么,我们将如何在不修复 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;
}

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

我将在本周的某个时候更新模板 10 并进行修复。

祝你好运,杰里

【讨论】:

  • 哇!我很高兴能为 XAML 平台做出贡献。如果您需要更多详细信息,请随时问我。
  • 哇,我遇到了同样的错误。只要我的风格不是 BasedOn 其他任何东西,我就可以走了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多