您的位置:首页 > 移动开发

【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框架。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐