无法找到WPF C#资源

刚刚开始使用C#和VS社区2017年WPF。试图运行代码,将改变启动窗口。 但获得以下例外。

System.IO.IOException: 'Cannot locate resource 'application_startup'.'

这段代码来自App.xaml

<Application x:Class="StartUp_Window.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:StartUp_Window"
         StartupUri="Application_Startup">
<Application.Resources>

</Application.Resources>

这是来自App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace c
{
  /// <summary>
  /// Interaction logic for App.xaml
  /// </summary>
  public partial class App : Application
  {
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // Create the startup window 
        MainWindow wnd = new MainWindow();
        // Do stuff here, e.g. to the window 
        wnd.Title = "Something else for fs";

           // Show the window 
           wnd.Show();
    }

  }
}

这是来自MainWindow.xaml

<Window x:Class="StartUp_Window.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:StartUp_Window"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>

这也来自MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StartUp_Window
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
        InitializeComponent();
    }
  }
}

这是在App.xaml文件中更改startupuri后出现的新错误。

严重级代码说明项目文件行抑制状态错误CS1061'App'不包含'Application_Startup'的定义,并且没有找到接受'App'类型的第一个参数的扩展方法'Application_Startup'(您是否缺少using指令或)StartUp_Window C: Users faisal Documents Visual Studio 2017 Projects WPF_Tutorial StartUp_Window StartUp_Window App.xaml 5活动错误CS0246无法找到类型或名称空间名称'MainWindow'(您是否缺少)使用指令或程序集引用?)StartUp_Window C: Users faisal Documents Visual Studio 2017 Projects WPF_Tutorial StartUp_Window StartUp_Window App.xaml.cs 20活动错误CS0246类型或名称空间名称'MainWindow'不能是找到(您是否缺少使用指令或程序集引用?)StartUp_Window C: Users faisal Documents Visual Studio 2017 Projects WPF_Tutorial StartUp_Window StartUp_Window App.xaml.cs 20活动


在App.xaml中,这行代码如下:

StartupUri="Application_Startup"

应该:

Startup="Application_Startup"

编辑:正如@AQuirky提到的,你应该使StartupUri =“MainWindow.xaml”。


App.xaml删除StartupUri属性:

<Application x:Class="StartUp_Window.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:StartUp_Window">
    <Application.Resources>

    </Application.Resources>
</Application>

...并重写App.xaml.csOnStartup方法:

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

        // Create the startup window 
        MainWindow wnd = new MainWindow();
        // Do stuff here, e.g. to the window 
        wnd.Title = "Something else for fs";

        // Show the window 
        wnd.Show();

    }
}

或者将StartupUri属性设置为您窗口的名称:

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

    </Application.Resources>
</Application>

...并且在App.xaml.cs什么都不做。

链接地址: http://www.djcxy.com/p/51239.html

上一篇: WPF C# Resource Cannot Be Found

下一篇: System.Environment.Exit(0) preventing hockeyapp from registering a crash