WPF应用程序的Main函数(转)

初学WPF进行应用开发,所有的教材中都直接默认的添加Window1.xaml等等的窗体。可是我们在进行项目开发中肯定需要修改这个默认的名称。使用VS2008的开发环境重命名默认的名称后编译是没有问题的,但运行起来后总是会出如下的错误信息:

找不到资源“window1.xaml”。

System.IO.IOException was unhandled
Message="找不到资源“window1.xaml”。"
Source="PresentationFramework"
StackTrace:
       在 MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
       在 System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
       在 System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
       在 System.IO.Packaging.PackWebResponse.GetResponseStream()
       在 System.IO.Packaging.PackWebResponse.get_ContentType()
       在 MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse response)
       在 MS.Internal.WpfWebRequestHelper.CreateRequestAndGetResponseStream(Uri uri, ContentType& contentType)
       在 System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       在 SDKSample.Window1.InitializeComponent()
       在 SDKSample.Window1..ctor()
       在 SDKSample.app.AppStartingUp(Object sender, StartupEventArgs e)
       在 System.Windows.Application.OnStartup(StartupEventArgs e)
       在 System.Windows.Application.<.ctor>b__0(Object unused)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       在 System.Windows.Threading.DispatcherOperation.InvokeImpl()
       在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       在 System.Threading.ExecutionContext.runTryCode(Object userData)
       在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Windows.Threading.DispatcherOperation.Invoke()
       在 System.Windows.Threading.Dispatcher.ProcessQueue()
       在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       在 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       在 System.Windows.Threading.Dispatcher.Run()
       在 System.Windows.Application.RunInternal(Window window)
       在 System.Windows.Application.Run(Window window)
       在 System.Windows.Application.Run()
       在 SDKSample.app.Main()
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()

这个问题以前在WinForm开发中也有过,就是Main启动项的设置问题。但我们在VS2008的开发环境中怎么也找不到Main的入口。然后到文件目录下去查看,可以看到在根目录下的obj目录中对应的Debug/Release目录里面存在App.g.cs,这个Main入口就在该文件中。App.g.cs的内如如下:

#pragma checksum "..\..\App.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "9B09BBD8ACB3D5449BCE99F79E73AB32"
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3603
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace VerSysClient {
   
   
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {
       
        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent() {
           
            #line 4 "..\..\App.xaml"
            this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative);
           
            #line default
            #line hidden
        }
       
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public static void Main() {
            VerSysClient.App app = new VerSysClient.App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

所以在如果需要更改启动项的文件名,还需要在App.g.cs文件中修改 this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative); 修改完成后重新编译运行就不会有问题了。注意如果是Debug版本就修改Debug目录下的App.g.cs,如果是Release版本的就修改Release目录下的App.g.cs。

 

 

另一篇 :http://www.cnblogs.com/chenxizhang/archive/2010/03/25/1694611.html

posted on 2010-08-13 15:54  满汗全席  阅读(1605)  评论(0编辑  收藏  举报

导航