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

soy*_*chi 5 xaml win-universal-app windows-10 template10

将项目调整为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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

例外信息:

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()
Run Code Online (Sandbox Code Playgroud)

Jer*_*xon 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;
}
Run Code Online (Sandbox Code Playgroud)

你可以将其简化为:

var a = Application.Current.Resources.ToArray();
Run Code Online (Sandbox Code Playgroud)

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

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

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

<Page.Resources>
    <ResourceDictionary Source="..\Styles\Custom.xaml" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

我的意思是,你在页面而不是应用程序中完成工作。那么,我们如何在不修复 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

祝你好运,杰瑞