I want to use a custom Main method in my WPF application.
To do that, I need to build the application XAML file as a Page, instead of an application definition :
When I try to define a style as a resource :
<Application
x:Class="MyProgram.Program"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProgram"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
and I try to use that style in my window :
<Window
x:Class="MyProgram.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:MyProgram"
mc:Ignorable="d" Style="{StaticResource WindowStyle}"
Title="MainWindow" Height="350" Width="525"/>
even though the designer works fine, when I try and run the program, it does not end well. I get an exception, the inner exception which reads {"Cannot find resource named 'WindowStyle'. Resource names are case sensitive."}
When the Build Action on the program is ApplicationDefinition
, everything goes fine. But like I said, when I set it as Page
( which I need to for the sake of being able to define a custom main method ), I can't use the styles defined in the application resources.
Why is that? Is there a work-around?
Put your Main()
method into a custom class, and in Project Properties -> Application
set Startup object
to your custom class. Your new Main()
method should get called upon startup and you don't have to build App.xaml
as Page, which should solve the problems with resource resolution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments