WPF C# Resource Cannot Be Found

Just started to WPF with C# and VS Community 2017. Tried to run code that will change the start up window. but get the following exception.

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

this code is from the 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>

This is from the 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();
    }

  }
}

this is from the 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>

this is also from the 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();
    }
  }
}

This is the new error that comes up after changing the startupuri in App.xaml file.

Severity Code Description Project File Line Suppression State Error CS1061 'App' does not contain a definition for 'Application_Startup' and no extension method 'Application_Startup' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?) StartUp_Window C:UsersfaisalDocumentsVisual Studio 2017ProjectsWPF_TutorialStartUp_WindowStartUp_WindowApp.xaml 5 Active Error CS0246 The type or namespace name 'MainWindow' could not be found (are you missing a using directive or an assembly reference?) StartUp_Window C:UsersfaisalDocumentsVisual Studio 2017ProjectsWPF_TutorialStartUp_WindowStartUp_WindowApp.xaml.cs 20 Active Error CS0246 The type or namespace name 'MainWindow' could not be found (are you missing a using directive or an assembly reference?) StartUp_Window C:UsersfaisalDocumentsVisual Studio 2017ProjectsWPF_TutorialStartUp_WindowStartUp_WindowApp.xaml.cs 20 Active


In the App.xaml, this line here:

StartupUri="Application_Startup"

should be:

Startup="Application_Startup"

Edit: As @AQuirky mentioned, you should make StartupUri="MainWindow.xaml".


Remove the StartupUri attribute from 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>

...and override the OnStartup method in 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();

    }
}

Or set the StartupUri property to the name of your window:

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

...and do nothing in App.xaml.cs .

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

上一篇: 在旧的WPF项目中使用交互式Toast通知

下一篇: 无法找到WPF C#资源