Search code examples
c#.netwpfwpf-controls

Trying to make a WPF CRUD app and it is showing me a Static Resource error for some unknown reason


So I'm trying to make my own CRUD app based from what I've learned and I do know that I did not do anything wrong but for some reason this program keeps throwing an error about Static Resource PageHeader and I really don't know why is it throwing this error. This is specifically the error thrown in the output "Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '23' and line position '24'." and this is the error shown https://imgur.com/WRHn1ea

MainWindow.xaml

<Window x:Class="Crud.WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Crud.WPF" 
        xmlns:views="clr-namespace:Crud.WPF.Views"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Border Margin="20">
        <views:CrudView/>
    </Border>
</Window>

App.xaml

<Application x:Class="Crud.WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Crud.WPF">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock"
                   x:Key="PageHeader">
                <Setter Property="FontSize"
                        Value="32"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

using System.Windows;

namespace Crud.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            MainWindow = new MainWindow();
            MainWindow.Show();

            base.OnStartup(e);
        }
    }
}

CrudView.xaml

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" 
                       Text="Crud"
                       FontSize="32"
                       Style="{StaticResource PageHeader}"/>
            
            <Button Grid.Column="1"
                    Content="Add"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Center"/>
        </Grid>

        <Grid Grid.Row="1"
              Margin="0 20 0 0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <components:CrudListing Grid.Column="0"
                                    Margin="0 0 20 0"/>

            <components:CrudDetails Grid.Column="1"/>
        </Grid>
    </Grid>

i tried chatgpt and google bard but they were just saying that the "PageHeader" in App.xaml is spelled with a lower case "p" which is clearly not. I've tried rewatching the video tutorial again to see if I've missed learning something and I have not. So overall, this is just a total nightmare


Solution

  • Try to specify a StartupUri instead of overriding the OnStartup method:

    <Application x:Class="Crud.WPF.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:Crud.WPF"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <Style TargetType="TextBlock"
                       x:Key="PageHeader">
                    <Setter Property="FontSize"
                            Value="32"/>
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    If you have a requirement for creating the window programmatically for whatever reason, you could handle the OnStartup event:

    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            MainWindow = new MainWindow();
            MainWindow.Show();
        }
    }
    

    App.xaml:

    <Application x:Class="Crud.WPF.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:Crud.WPF"
                 Startup="Application_Startup">
        <Application.Resources>
            <ResourceDictionary>
                <Style TargetType="TextBlock"
                       x:Key="PageHeader">
                    <Setter Property="FontSize"
                            Value="32"/>
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    So use StartupUri or Startup in App.xaml but do not override the OnStartup method in App.xaml.cs if you define your resources like this.