【WPF】app.g.cs文件无法修改、修改后自动恢复问题解决办法
2011-06-16 11:11
1181 查看
刚接触WPF,公司要求用WPF开发一个项目,单机版【小额担保贷款信息统计系统】,后期要求登录时加上Ukey验证,于是开始找项目的入口方法,没找到,重写Main入口方法,编译报错,说已经存在Main方法,网上查资料,了解到是因为WPF应用程序在创建的时候就会为我们创建Main函数并调用Application实例的run函数,从而启动Application进程。这个Main函数就是app.g.cs中那个自动生成的Main函数。app.g.cs文件的位置在\obj\x86\Debug或Release文件夹下(根据你的输出方式)。自动的Main函数如下:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
我试图在此Main方法中添加自己的Ukey认证逻辑
1 /// <summary>
2 /// Application Entry Point.
3 /// </summary>
4 [System.STAThreadAttribute()]
5 [System.Diagnostics.DebuggerNonUserCodeAttribute()]
6 public static void Main() {
7 if (!Webac.Webac_CustomerSystemValidate())//是否插入Ukey并且Ukey是本系统使用
8 {
9 CheckUKWhenLogin newWindow = new CheckUKWhenLogin();
newWindow.ShowDialog();
return;
}
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
运行 成功
到这里以为成功了,岂料点重新生成应用程序时,会弹出消息提示,让我讲app.g.cs文件恢复,我选择不恢复,这样虽然麻烦,也算实现了,但是发现当项目关闭重新打开后,app.g.cs文件又重新生成。
解决方案1:
之所以出现这样的原因是由于App.xaml文件的属性中的生成操作(Build Action)所导致的。
将默认的ApplicationDefinition改为Page
然后在App.xaml.cs文件中实现Main方法
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[STAThread]
static void Main(string[] args)
{
if (!Webac.Webac_CustomerSystemValidate())
{
CheckUKWhenLogin newWindow = new CheckUKWhenLogin();
newWindow.ShowDialog();
return;
}
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
}
重新生成,成功。
下面简单说下各个Buid Action之间的区别
* None: 此文件不参与编译也不被输出。比如:工程中的文档文件, readme.txt。
* Compile: 参与编译并输出。主要是代码文件。
* Content: 不参与编译,但会被输出。
* Embedded Resource: 此文件被嵌入到主工程生成的DLL或exe中。主要是资源文件。
* ApplicationDefinition: 和Page类似,但只用于Silverlight的启动页面(默认是App.xaml)。
* Page: Silverligh中所有的usercontrol/page/childwindow xaml都属于"Page” build,其它的build action不能将code behind文件和xaml文件连接起来。
* CodeAnalysisDictionary: 自定义的CodeAnalysis字典。(参考http://blogs.msdn.com/b/codeanalysis/archive/2007/08/20/new-for-visual-studio-2008-custom-dictionaries.aspx)
* Resource:embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources
* SplashScreen: Silverlight的欢迎界面。
* DesignData: Sample data types will be created as faux types. Use this Build Action when the sample data types are not creatable or have read-only properties that you want to defined sample data values for.
* DesignDataWithDesignTimeCreatableTypes: Sample data types will be created using the types defined in the sample data file. Use this Build Action when the sample data types are creatable using their default empty constructor.
* EntityDeploy: 适用于Entity框架。
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
我试图在此Main方法中添加自己的Ukey认证逻辑
1 /// <summary>
2 /// Application Entry Point.
3 /// </summary>
4 [System.STAThreadAttribute()]
5 [System.Diagnostics.DebuggerNonUserCodeAttribute()]
6 public static void Main() {
7 if (!Webac.Webac_CustomerSystemValidate())//是否插入Ukey并且Ukey是本系统使用
8 {
9 CheckUKWhenLogin newWindow = new CheckUKWhenLogin();
newWindow.ShowDialog();
return;
}
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
运行 成功
到这里以为成功了,岂料点重新生成应用程序时,会弹出消息提示,让我讲app.g.cs文件恢复,我选择不恢复,这样虽然麻烦,也算实现了,但是发现当项目关闭重新打开后,app.g.cs文件又重新生成。
解决方案1:
之所以出现这样的原因是由于App.xaml文件的属性中的生成操作(Build Action)所导致的。
将默认的ApplicationDefinition改为Page
然后在App.xaml.cs文件中实现Main方法
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[STAThread]
static void Main(string[] args)
{
if (!Webac.Webac_CustomerSystemValidate())
{
CheckUKWhenLogin newWindow = new CheckUKWhenLogin();
newWindow.ShowDialog();
return;
}
PettySecuredLoanMS.App app = new PettySecuredLoanMS.App();
app.InitializeComponent();
app.Run();
}
}
重新生成,成功。
下面简单说下各个Buid Action之间的区别
* None: 此文件不参与编译也不被输出。比如:工程中的文档文件, readme.txt。
* Compile: 参与编译并输出。主要是代码文件。
* Content: 不参与编译,但会被输出。
* Embedded Resource: 此文件被嵌入到主工程生成的DLL或exe中。主要是资源文件。
* ApplicationDefinition: 和Page类似,但只用于Silverlight的启动页面(默认是App.xaml)。
* Page: Silverligh中所有的usercontrol/page/childwindow xaml都属于"Page” build,其它的build action不能将code behind文件和xaml文件连接起来。
* CodeAnalysisDictionary: 自定义的CodeAnalysis字典。(参考http://blogs.msdn.com/b/codeanalysis/archive/2007/08/20/new-for-visual-studio-2008-custom-dictionaries.aspx)
* Resource:embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources
* SplashScreen: Silverlight的欢迎界面。
* DesignData: Sample data types will be created as faux types. Use this Build Action when the sample data types are not creatable or have read-only properties that you want to defined sample data values for.
* DesignDataWithDesignTimeCreatableTypes: Sample data types will be created using the types defined in the sample data file. Use this Build Action when the sample data types are creatable using their default empty constructor.
* EntityDeploy: 适用于Entity框架。
相关文章推荐
- Ubuntu修改etc/environment文件后无法登陆系统的问题解决办法
- Linux下修改配置文件导致系统无法启动问题解决办法
- [C#] .NET Core项目修改project.json来引用其他目录下的源码等文件的办法 & 解决多框架时 project.json 与 app.config冲突的问题
- Tomcat配置文件修改无效自动恢复还原的解决办法总结
- 修改系统文件后解决系统自动恢复的问题
- Ubuntu在恢复模式下无法修改系统文件解决办法
- WPF项目中.xaml文件和.xaml.cs文件无法收缩显示的解决办法
- Ubuntu由于修改etc/profile文件导致开机无法登陆问题解决办法(仅供参考)
- Tomcat配置文件修改无效自动恢复还原的解决办法总结
- Ubuntu在恢复模式下无法修改系统文件解决办法
- jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
- 解决无法修改my.ini配置文件的问题
- react最简单的新建方式--Create React App,(附文件路径问题解决的办法)
- jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法
- MySQL的配置文件无法修改的解决办法(Win8)
- 编译DXUT框架中CustomUI例子遇到问题:无法打开包括文件:“msctf.h”的 解决办法
- 有关c#.net“无法加载 CSOpenGLC.dll:找不到指定的模块”的问题解决办法
- C#技巧【调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法】【C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件”的解决】
- 修改web.xml文件之后,tomcat无法把项目自动启动的问题
- 【原】oracle11gR2 dbconsole修改主机无法登陆问题解决办法