2017-08-15 169 views
0

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

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“应用”不包含“Application_Startup”的定义,并没有扩展方法“Application_Startup”接受式“应用程序”的第一个参数可以找到(是否缺少)使用指令或程序集引用?)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'找不到(你是mi )StartUp_Window C:\ Users \ faisal \ Documents \ Visual Studio 2017 \ Projects \ WPF_Tutorial \ StartUp_Window \ StartUp_Window \ App.xaml.cs 20活动

回答

1

在App.xaml中,这是一个使用指令或程序集引用行这里:

StartupUri="Application_Startup" 

应该是:

Startup="Application_Startup" 

编辑:作为@AQuirky提到的,你应该做的StartupUri = “MainWindow.xaml”。

+2

还需要StartupUri =“MainWindow。xaml“ – AQuirky

+0

@AQuirky这是真的,我会编辑它 –

+0

@AQuirky我在哪里修复你的线 –

0

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> 

...和覆盖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> 

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

+0

当我从 StartupUri =“Application_Startup”到Startup =“Application_Startup”我得到一个错误,错误被粘贴在 –

+0

以上是的,你不应该改变它,你应该删除它,你没有阅读我的答案?然后再读一遍。覆盖OnStartup方法并删除来自App.xaml的StartupUri。 – mm8