5

使项目适应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;
    }
}

我的应用程序.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()
4

1 回答 1

2

这让我深感困惑。我很容易重现这一点。然后,我无需参考模板 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。

祝你好运,杰瑞

于 2016-03-08T02:16:59.003 回答