12

我正在使用 Visual Studio 2010 RC1。

我在 app.xaml_ 中定义了一个资源“Brush2”:

<Application x:Class="VideoThumbnails.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <RadialGradientBrush x:Key="Brush2" RadiusX="1" RadiusY="1" GradientOrigin="0.3,0.3">
            <GradientStop Color="White" Offset="0"/>
            <GradientStop Color="#ffc0c0" Offset="1"/>
        </RadialGradientBrush>

    </Application.Resources>
</Application>

在我的主窗口中,我正在尝试使用该资源:

...
<Border Margin="4,2" BorderBrush="Black" BorderThickness="2" CornerRadius="4"
        ToolTip="{Binding Path=FullPath}" HorizontalAlignment="Stretch"
        Background="{StaticResource Brush2}">
...

无论我做什么,它总是在运行时引发异常(找不到资源)。我更改了构建操作但没有成功。

如何使用 app.xaml 中定义的资源?

4

6 回答 6

21

如果您已将 Startup Object 设置为自定义类,则需要创建自定义 Application 类并调用其 InitializeComponent 方法,如下所示:

App app = new App();
app.InitializeComponent();

更新:正如@qqww2 建议的那样,InitializeComponent调用可以在App类构造函数中移动。

于 2011-02-08T23:57:27.917 回答
5

你所做的一切都是不正确的。您要么 1) 以某种方式搞砸了项目构建,同时随机做一些事情以使其工作,或者 2) 这里发生了其他事情,如果没有异常细节,我们永远不会知道。

我强烈建议您尝试在一个全新的 WPF 项目中重现这一点。执行以下步骤(并且仅执行以下步骤):

创建一个新的 WPF 项目,将完全相同的画笔添加到 app.xaml,然后打开 Window1 并将窗口的背景绑定到资源。运行应用程序。

它应该按预期工作。如果没有,请返回异常详细信息。如果是这样,请将这个新项目与您当前的项目进行比较,看看您在做什么不同。

于 2010-03-21T14:48:00.067 回答
4

我不是使用StartupUriin启动我的应用程序App.xaml,而是使用 in 中的事件处理程序App.xaml.cs

使用override Startup()App.xaml 中的资源时加载:

public partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ...
    }
}

但是当使用事件时Startup,资源加载得很好:

<Application x:Class="OxyplotTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="OnStartup">

和后面的代码:

public partial class App
{
    private void OnStartup(object sender, StartupEventArgs e)
    {
        ...
    }
}
于 2019-12-12T08:32:15.993 回答
3

我知道有一个已经被接受的答案,但我想我也会添加我的解决方案。我有可以工作的代码,但是一些配置更改破坏了设计器中的资源引用。在执行代码时,它运行良好。

经过一些初步研究,我确定应将 App.xaml 的 BuildAction 属性设置为 ApplicationDefinition。我的设置为 Page。但是,这会导致多个入口点出现一些问题。Main() 已在 App.xaml.cs 中定义。编译错误表明 App.g.cs 中的另一个入口点(这是一个自动生成的文件)。

我最终使用了http://www.infosysblogs.com/microsoft/2008/09/how_to_write_custom_main_metho.html中描述的方法#3 。基本思想是创建一个只负责启动的新类。就我而言,我将其命名为 Startup.cs。它应该具有与此类似的代码:

using System.Threading;

namespace MyNamespace
{
    public class Startup
    {
        [System.STAThreadAttribute()]
        private static void Main()
        {   
            var app = new App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

然后在项目设置中,更改 Application -> Startup 对象,以便选择您的新类。

于 2012-10-09T20:19:08.783 回答
3

我有一个类似的问题并解决了它,所以我想我不妨发布我的解决方案。Resource not found如上所述,我只在运行时不断收到错误。在我的 Windows 8.1 c# 应用程序中,我使用了我定义的样式,它在 Blend 和设计器视图中显示良好,但在运行时不起作用。我试图在按照这些说明创建的 SettingsFlyout 中使用这种样式。让它工作之后,我在 App.xaml 中设置了一个字段来保存我的弹出窗口(首选项和颜色设置),这样我就不会每次都制作一个新的。

public static Preferences preferences;
public static ColorSettings colorsettings;

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    preferences = new Preferences();
    colorsettings = new ColorSettings();
}

在四处寻找和谷歌搜索大约一个小时后,我发现我太早地创建了弹出窗口,并且在创建它们时它们无法访问应用程序的资源。所以我把他们的创作移到了那里App.OnLaunched(),这解决了这个问题。

我不确定这是否是最好的处理方式,但它确实有效。因此,请尝试确定您尝试访问所需资源的位置,以及您是否尝试过早。很抱歉含糊不清,也许不正确,我对 WPF 真的很陌生。

于 2014-02-14T22:20:53.460 回答
3

如果您在 App 构造函数中有某些内容,我可以同意资源很容易被弄乱。将您自己的全局对象的初始化移动到 OnStartup 方法中:

protected override void OnStartup(StartupEventArgs e)
{
}
于 2014-04-15T11:31:27.800 回答