2017-08-15 275 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