gpt4 book ai didi

c# - 当我的应用程序构建为 'Page' 而不是 'Application Definition' 时,如何使用应用程序范围的资源?

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:43 24 4
gpt4 key购买 nike

我想在我的 WPF 应用程序中使用自定义 Main 方法。

为此,我需要将应用程序 XAML 文件构建为页面,而不是应用程序定义:

enter image description here

当我尝试将样式定义为资源时:

<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>

我尝试在我的窗口中使用该样式:

<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"/>

尽管设计器工作正常,但当我尝试运行该程序时,它并没有很好地结束。我得到一个异常,内部异常显示为 {"Cannot find resource named 'WindowStyle'. Resource names are case sensitive."

当程序上的 Build Action 为 ApplicationDefinition 时,一切正常。但正如我所说,当我将其设置为 Page(为了能够定义自定义 main 方法,我需要这样做)时,我无法使用应用程序资源中定义的样式。

这是为什么呢?有解决方法吗?

最佳答案

您在主应用程序 XAML 中编写样式是否有任何特定原因。如果您愿意,您可以在单独的文件中的资源字典下创建您的样式,并在您想要的任何地方合并该样式。

例如:

<ResourceDictionary x:Class="MyProgram.MainWindow.Styles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<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>
</ResourceDictionary>

并在你要使用的文件中合并“WindowStyle”。如果您需要更多详细信息,请告诉我。

关于c# - 当我的应用程序构建为 'Page' 而不是 'Application Definition' 时,如何使用应用程序范围的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com