【问题标题】:WinRT ListPickerFlyout crashes App if background color changed如果背景颜色更改,WinRT ListPickerFlyout 会使应用程序崩溃
【发布时间】:2015-02-09 01:30:23
【问题描述】:

如果我将以下代码放入框架 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?

【问题讨论】:

    标签: windows-runtime winrt-xaml win-universal-app


    【解决方案1】:

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

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

    有趣的问题是“为什么?” - 我的猜测是因为ListPickerFlyout 实际上不是UIElement,所以在初始化期间会发生一些奇怪的交互。

    【讨论】:

      【解决方案2】:

      是的,“为什么”是一个有趣的问题,彼得的回答很好。

      我还从另一个 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}"

      【讨论】:

        猜你喜欢
        • 2017-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2011-10-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多