C# CF:如何使用我的程序打开特定文件扩展名
我正在 .Net Compact Framework 中开发移动应用程序。 我设法编辑注册表 HKEY_CLASSES_ROOT,以便单击扩展名为 .xyz 的文件将打开我的应用程序。基本上,我需要在单击该文件时对其进行一些操作。
但是,我意识到,如果我第一次这样做,它会到达位于 static void Main
的 program.cs。但是,当程序运行时,我再次单击扩展名为 .xyz 的文件,它不会加载程序 static void Main
。我尝试在当前正在运行的窗体上设置断点,但仍然没有任何结果。
那么它去哪里呢?如何检测文件 .xyz 被单击并执行某些操作?
I'm developing a mobile application in .Net Compact Framework.
I managed to edit the registry HKEY_CLASSES_ROOT so that a click on a file with the .xyz extension will open my application. Basically, I need to do some operation on this file when it's clicked.
However, I realise that if I do that the first time, it reaches program.cs at static void Main
. But when the program is running and I click on the file with .xyz extension again, it doesn't load the program static void Main
. I tried setting breakpoints at the form that is currently running but still nothing.
So where does it go to? How can I detect file .xyz is clicked and do something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您遇到的大问题是,一旦应用程序运行,Main 将永远不会再次被调用,事实上也不应该。
不幸的是,在 Windows Mobile 下,CF 本身会尝试保持应用程序单例,因此当您尝试第二次运行应用程序时,CLR 本身会拦截该操作,并将现有实例带到前台。这样做的不幸的副作用之一是您没有机会处理命令行参数。
要使其正常工作,您必须执行以下操作:
The big problem you're having is that once the application is running Main will never get called again, and in fact it shouldn't.
Under Windows Mobile, unfortunately, the CF itself attempts to keep applications singletons, so when you try to runt he app a second time, the CLR itself intercepts that and instead brings the existing instance to the fore. One of the unfortunate side effects of this is that you get no opportunity to handle command-line parameters.
To get this to work, you have to do a few things:
您说每次单击文件时都需要对文件执行一些操作。我假设这是与 GUI 相关的东西,就像您想在双击文件时显示文件的属性一样。
假设您的程序有一个主窗体,您可以将其
MinimizeBox
属性设置为false
并在其Deactivate
事件中放置this.Close( );
。这样,当您单击正确类型的文件时,您的应用程序将启动并读取命令行参数并显示文件详细信息。如果用户随后使用右上角的“确定”按钮关闭您的应用程序,它将真正关闭,以便下次打开时它将打开一个新实例并正确读取命令行参数。或者,如果用户只是导航到 WinMo 中的某个其他程序,则应用程序的Deactivate
事件将触发,从而关闭该应用程序。无论哪种方式,应用程序始终要么打开并位于顶部,要么完全关闭,因此单击文件资源管理器中的文件将始终打开一个新实例。You say that you need to perform some operation on a file every time it's clicked. I'm assuming this is something GUI-related, like you want to show properties of the file when it's double-clicked.
Assuming your program has one main form, you could set its
MinimizeBox
property tofalse
and in itsDeactivate
event putthis.Close();
. This way, when you click a file of the right type, your application will start and read the command line args and display the file details. If the user then closes your app with the OK button in the upper right, it will close out for real so that the next time it opens it will open a new instance and read the command line args properly. Or, if the user just navigates to some other program in WinMo, your application'sDeactivate
event will fire, closing the app. Either way, the app is always either open and on top, or completely closed, so clicking a file in file explorer will always open a new instance.Main 中有字符串参数吗?
Do you have string args in Main?
您的问题是您正在寻找再次调用
static void Main
,这是一个 静态构造函数。静态构造函数仅被调用一次。它们在类(在本例中是您的程序)初始化之前被调用,然后再也不会调用。程序的每个新实例都不会调用它们。Your problem is that you're looking for
static void Main
to be called again, which is a Static Constructor. Static constructors are only called once. They are called before the class (in this case your program) is initialized, then never again. They're not called for each new instance of your program..