Home > Mobile >  WPF ''Resources' property has already been set on 'App'.'
WPF ''Resources' property has already been set on 'App'.'

Time:06-21

At the start of learning WPF and so far, it's not much fun! Inscrutable error messages ahoy! I'm trying to set up images in a global app resource library. I also have some global app control styling in there:

<Application x:Class="niftymonkey.GameBoxer.UI.WpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:niftymonkey.GameBoxer.UI.WpfApp"
             StartupUri="MainWindow.xaml">
    
    <Application.Resources>

         <ResourceDictionary>
            <BitmapImage x:Key="IconPng128" UriSource="Assets/space-invader-icon-128.png"></BitmapImage>
        </ResourceDictionary>
        
        <Style TargetType="Window">
            <Setter Property="Background" Value="#303030"/>
        </Style> 

        <Style TargetType="Menu">
            <Setter Property="Background" Value="#202020"/>
            <Setter Property="Foreground" Value="Gainsboro"></Setter>
            <Setter Property="Padding" Value="0,4,0,4"></Setter>
        </Style>

     </Application.Resources>
</Application>

This is throwing the titular error. Main error: System.Windows.Markup.XamlParseException

It compiles and runs fine without it.

The line and column space it points me to makes no sense what so ever. Does anyone know what I'm doing wrong?

CodePudding user response:

if you explicitly declare <ResourceDictionary>, you must put all resources inside it:

<Application.Resources>
     <ResourceDictionary>
        <BitmapImage x:Key="IconPng128" UriSource="Assets/space-invader-icon-128.png"></BitmapImage>
    
        <Style TargetType="Window">
            <Setter Property="Background" Value="#303030"/>
        </Style> 

        <Style TargetType="Menu">
            <Setter Property="Background" Value="#202020"/>
            <Setter Property="Foreground" Value="Gainsboro"></Setter>
            <Setter Property="Padding" Value="0,4,0,4"></Setter>
        </Style>
    </ResourceDictionary>
 </Application.Resources>

if you don't have MergedDictionaries, you can simply use Application.Resources:

<Application.Resources>
    <BitmapImage x:Key="IconPng128" UriSource="Assets/space-invader-icon-128.png"></BitmapImage>

    <Style TargetType="Window">
        <Setter Property="Background" Value="#303030"/>
    </Style> 

    <Style TargetType="Menu">
        <Setter Property="Background" Value="#202020"/>
        <Setter Property="Foreground" Value="Gainsboro"></Setter>
        <Setter Property="Padding" Value="0,4,0,4"></Setter>
    </Style>
 </Application.Resources>
  • Related