繁体   English   中英

找不到 WPF C# 资源

[英]WPF C# Resource Cannot Be Found

刚开始使用 C# 和 VS Community 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 Active Error CS0246 找不到类型或命名空间名称“MainWindow”(您是否缺少使用指令或程序集引用?)StartUp_Window C:\Users\faisal\Documents\Visual Studio 2017\Projects\WPF_Tutorial\StartUp_Window\StartUp_Window\App.xaml.cs 20 活动错误 CS0246 类型或命名空间名称“MainWindow”无法找到(是否缺少 using 指令或程序集引用?)StartUp_Window C:\Users\faisal\Documents\Visual Studio 2017\Projects\WPF_Tutorial\StartUp_Window\StartUp_Window\App.xaml.cs 20 Active

在 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.cs中的OnStartup方法:

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中什么也不做。

刚开始使用 C# 和 VS Community 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 Active Error CS0246 The type or namespace name 'MainWindow' could not be found(你是否缺少一个using 指令或程序集引用?)StartUp_Window C:\\Users\\faisal\\Documents\\Visual Studio 2017\\Projects\\WPF_Tutorial\\StartUp_Window\\StartUp_Window\\App.xaml.cs 20 Active Error CS0246 The type or namespace name 'MainWindow' could not be找到(您是否缺少 using 指令或程序集引用?) StartUp_Window C:\\Users\\faisal\\Documents\\Visual Studio 2017\\Projects\\WPF_Tutorial\\StartUp_Window\\StartUp_Window\\App.xaml.cs 20 Active

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM