Windows runtime 如果背景颜色更改,WinRT ListPickerFlyout会使应用程序崩溃
如果我将以下代码放入框架WinRT应用程序中,它将不会构造主页面:Windows runtime 如果背景颜色更改,WinRT ListPickerFlyout会使应用程序崩溃,windows-runtime,winrt-xaml,win-universal-app,Windows Runtime,Winrt Xaml,Win Universal App,如果我将以下代码放入框架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.micro
<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>
错误是:
TestApp1.WindowsPhone.exe中发生类型为“Windows.UI.Xaml.Markup.XamlParseException”的异常,但未在用户代码中处理
WinRT信息:无法创建类型“%0”的实例[行:12位置:42]
将Background标记更改回{ThemeResource application pagebackgroundThemebrush}
可以解决此问题
有什么办法可以改变页面的背景色而仍然使用ListPickerfyOut吗?简单的解决方案是在调用
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的答案很好
我还从另一个问题中找到了另一个答案
首先,通过将以下内容添加到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 flyioutbackgroundThemebrush}”