无法找到WPF C#资源

时间:2017-08-15 22:26:19

标签: c# .net wpf xaml

刚开始使用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:\用户\ faisal \ Documents \ Visual Studio 2017 \ Projects \ WPF_Tutorial \ StartUp_Window \ StartUp_Window \ App.xaml 5有效 错误CS0246找不到类型或命名空间名称'MainWindow'(您是否缺少using指令或程序集引用?)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活跃

3 个答案:

答案 0 :(得分:1)

在App.xaml中,这一行在这里:

StartupUri="Application_Startup"

应该是:

Startup="Application_Startup"

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

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> 中删除StartupUri属性:

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"> <Application.Resources> </Application.Resources> </Application> 中的OnStartup方法:

App.xaml.cs

或者将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> 中无所作为。

答案 2 :(得分:-1)

为您的代码做一个小小的更改(StartupUri-> Startup):

<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"
     Startup="Application_Startup">
<Application.Resources>