在台式计算机上运行紧凑框架代码 [英] running compact framework code on desktop computer

查看:66
本文介绍了在台式计算机上运行紧凑框架代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Windows Compact Framework 3.5制作了一个应用程序,看起来工作正常.

I made an application for Windows Compact Framework 3.5 , which appears to work fine.

编译后,在我的计算机上创建了一个 .exe 文件.直到最近,我实际上还可以在计算机上运行此exe文件.(无需使用模拟器)

After compilation a .exe file is created on my computer. Until recently, I could actually also run this exe file on my computer. (without the use of a simulator)

但是最近我注意到我的应用程序仅在移动设备上运行.当我尝试在台式计算机上运行它时,收到一条奇怪的错误消息,该消息表明我应该对 Main() [STAThread] 指令运行我的应用程序code>方法.

But recently I noticed that my application only runs on mobile devices. When I try to run it on my desktop computer I receive a strange error message which indicates that I should run my application with the [STAThread] directive for the Main() method.

但是,对于我的移动设备而言,这不是必需的,一切都可以正常进行.实际上,我什至不能将 [STAThread] 添加到源代码中,因为紧凑型框架不支持它.添加它会导致编译错误.

However, for my mobile devices this is not necessary, everything works fine as is. In fact, I cannot even add the [STAThread] to the source code because the compact framework does not support it. Adding it causes compilation errors.

不幸的是,这也是现在的问题.我想添加一些条件代码来评估它是否在Windows CE或Windows桌面上运行.在桌面上运行时,应以 STAThread 模式启动代码.但是,我找不到添加此类代码的方法,因为它无法编译.它总是归结为编译器不知道什么是STAThread.

Unfortunately that's also the problem now. I would love to add some conditional code which evaluates if it's running on Windows CE or Windows Desktop. When it runs on desktop it should then start the code in STAThread mode. However, I cannot find a way to add this kind of code, because it doesn't compile. It always comes down to the point that the compiler does not know what STAThread is.

有没有办法解决这个问题?

对我来说,一个好的解决方法是以不同的方式编译它,也许当我为台式计算机编译它时,可以选择一个不同的目标平台.但是,我目前无法这样做.有什么想法吗?

A good workaround for me, would be to compile it in a different way, perhaps by selecting a different target platform when I compile it for desktop computers. However, I am not able to do so currently. Any ideas ?

推荐答案

总而言之,当代码在台式计算机上运行时,仅需要在STA状态下运行.此外,STA状态甚至在移动设备上都不可用.

In summary the code only needs to run in STA state when it runs on a desktop computer. Furthermore the STA state isn't even available on a mobile device.

这是我想出的:

    static void Main() 
    {
        Type type = typeof(Thread);
        MethodInfo methodInfo = type.GetMethod("SetApartmentState");

        if (methodInfo != null)
        {
            // full .net framework
            // --> requires STA apartmentstate

            Thread thread = new Thread(() => Run());
            methodInfo.Invoke(thread, new object[] { ApartmentState.STA });
            thread.Start();
            thread.Join();
        }
        else
        {
            // .net compact framework
            // --> needs no special attention (can run in MTA)
            Run();
        }
    }

注意:上面的 Run()方法是启动应用程序的方法.

Note: The Run() method above, is the one that starts the application.

由于代码是用Compact Framework编写的,因此不能直接设置单元状态,这仅仅是因为没有 setApartmentState 方法.但是幸运的是,可以使用反射来完成,因为该方法实际上将在运行时(且仅当)代码在完整的.net框架上运行时才可用.

Due to the fact that the code is written in Compact Framework, the apartment state cannot be set directly, simply because there is no setApartmentState method. But fortunately, it can be done using reflection, because the method will actually be available at runtime when (and only when) the code runs on the full .net framework.

这篇关于在台式计算机上运行紧凑框架代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆