如果背景颜色发生更改,WinRT ListPickerFlyout会使应用程序崩溃



如果我把以下代码放在一个框架WinRT应用程序中,它就不会构建主页:

<Page
x:Class="TestApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="Blue">
<Page.Resources>
    <ListPickerFlyout x:Key="btnfly"/>
</Page.Resources>
<Grid>
</Grid>
</Page>

错误为:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in TestApp1.WindowsPhone.exe but was not handled in user code WinRT information: Cannot create instance of type '%0' [Line: 12 Position: 42]

Background标记更改回{ThemeResource ApplicationPageBackgroundThemeBrush}可修复此问题。

有什么想法可以改变页面的背景色,同时仍然使用ListPickerFlyout吗?

简单的解决方案是在构造函数内部设置背景颜色,调用InitializeComponent:之后

public MainPage()
{
  this.InitializeComponent();
  // Or the colour of your choice...
  Background = new SolidColorBrush(Windows.UI.Colors.Blue);
  this.NavigationCacheMode = NavigationCacheMode.Required;
}

有趣的问题是"为什么?"——我的猜测是,因为ListPickerFlyout实际上不是UIElement,所以在初始化过程中发生了一些奇怪的交互。

是的,"为什么"是一个有趣的问题,Peter的回答很好。

我还从另一个StackOverflow问题中找到了另一个答案。

首先,通过在App.xaml中添加以下内容来覆盖FlyoutBackgroundThemeBrush:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="FlyoutBackgroundThemeBrush" Color="Blue" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="FlyoutBackgroundThemeBrush" Color="Blue" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后将页面背景更改为Background="{ThemeResource FlyoutBackgroundThemeBrush}"

相关内容

  • 没有找到相关文章

最新更新