无法访问C#中的静态公共类 [英] Can't get access to a static public class in C#

查看:97
本文介绍了无法访问C#中的静态公共类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的问题中( 方法必须在C#类中具有返回类型 [ ^ ])我使用该类制作全局热键。但是当我尝试使用它时,我无法访问常量,这是一个公共静态类;显示错误:名称当前上下文中不存在常量。



这是代码用于使用全局热键类(它在DreamInCode帖子中使用 http://www.dreamincode.net/forums/topic/180436-global-hotkeys/ [ ^ ])



In my previous question (Method must have a return type in C# Class[^]) I used that class to make Global Hotkeys. But when I'm trying to use it, I can't get access to the Constants, which is a public static class; An error is shown: The name "Constants does not exist in the current context.

This is the code for "using" the global hotkeys class(it is used like that in the DreamInCode post http://www.dreamincode.net/forums/topic/180436-global-hotkeys/[^])

namespace Quick_Preview
{
    public partial class Form1 : Form
    {
        private GlobalHotkeys.GBH ghk;
        public Form1()
        {
            InitializeComponent();
            ghk = new GlobalHotkeys.GBH(Constants.NOMOD, Keys.Space, this);
        }
    }
}
//Constants is what "does not exists"





我还会显示完整的类代码(没有显示任何错误):



I will also show the complete class code(Which does not show any errors):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GlobalHotkeys
{
    public class GBH
    {
        public void GlobalHotkey(int modifier, Keys key, Form form)
        {
            this.modifier = modifier;
            this.key = (int)key;
            this.hWnd = form.Handle;
            id = this.GetHashCode();

        }

        public static class Constants
        {
            //modifiers
            public const int NOMOD = 0x0000;
            public const int ALT = 0x0001;
            public const int CTRL = 0x0002;
            public const int SHIFT = 0x0004;
            public const int WIN = 0x0008;

            //window message id for hotkey
            public const int WM_HOTKEY_MSG_ID = 0x0312;
        }

        private int modifier;
        private int key;
        private IntPtr hWnd;
        private int id;

        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);



        public override int GetHashCode()
        {
            return modifier ^ key ^ hWnd.ToInt32();
        }

        public bool Register()
        {
            return RegisterHotKey(hWnd, id, modifier, key);
        }

        public bool Unregister()
        {
            return UnregisterHotKey(hWnd, id);
        }
    }
}







这个我正在尝试做的事情:

http://iicbsi.com/cnwk.1d/i/tim/2012/01/09/QuickLookExample.png [ ^ ]

当您使用在Mac OSX中选择的文件按空格键时会发生这种情况。如果再次按空格键,则格式最小化。并且它没有显示在mac os x app启动器中。这与我想做的完全一样,但标题栏显示在我的屏幕的下角,这不应该发生。




This is what I'm trying to do:
http://i.i.cbsi.com/cnwk.1d/i/tim/2012/01/09/QuickLookExample.png[^]
That happens when you press Space with a file selected in Mac OSX. If you press Space again, that "form" minimizes. And it does not show in the mac os x app launcher. That's exactly the same I want to do, but a title bar shows in the bottom corner of my screen, thing that should not happen.

推荐答案

看起来一切都是正确。您可能缺少的只是正常的常量命名。 (再次,请在部分命名空间中查看我之前对您引用的问题的回答。)



这是在大多数情况下使用此常量的方法,独立于内容。这意味着,它可以在程序集中的任何类型的代码或使用程序集的任何程序集中完成(由于 public 类和成员上的访问修饰符):



It looks like everything is correct. All you might have missing is correct naming of the constant. (Again, please review my previous answer to the question you referenced, in part of namespaces.)

This is how you can use this constant in the most general case, independent from content. It means, it could be done in the code of any type at all in your assembly or any assembly using your assembly (due to public access modifiers on both class and members):

int ctrlPressed = GlobalHotkeys.GBH.Constants.CTRL;
int ctrlAltPressed =
   GlobalHotkeys.GBH.Constants.CTRL |
   GlobalHotkeys.GBH.Constants.ALT;





请注意您使用嵌套类(常量嵌套在 GBH 中的将自己的部分添加到命名空间和完整的类型名称。



这就是你如何使用使用别名:



Note that your use of nested class (Constant nested in GBH) adds its own part to namespace and the full type name.

And this is how you could use using alias:

using Constants = GlobalHotkeys.GBH.Constants;
// instead of Constants, it could be any other suitable name

// ...

int ctrlPressed = Constants.CTRL;
int ctrlAltPressed =
   Constants.CTRL |
   Constants.ALT;









做一个大忙,只是忽略解决方案2中的建议。将任何东西放在单独的文件或上层都没有错,但这只是一种避免理解事物真正起作用的方法,在这种情况下,一个非常简单的事情。



-SA


ChrisCreateBoss问
ChrisCreateBoss asked



嘿嘿,我不想在任务栏中显示我的应用程序,已经完成了,但是当我最小化它时,标题栏保留在屏幕的左下角,我怎么能转到假?


Hey something more, I don't want to show my app in the taskbar, that's done already, but when I minimize it, a title bar stays in the left bottom corner of the screen, how can I turn to "false" that?

请在评论解答1中看到我对你的问题的评论。



这没有任何意义。如果您从桌面隐藏应用程序,根本就无法访问它,您只能使用任务管理器终止进程,或者使用一些全局热键(如果您有兴趣,请问我怎么样,现在它会偏离主题)。您需要在桌面上显示某些内容。



-SA

Please see my comment to your question in comments to Solution 1.

It just would not make any sense. If you hide your application from the desktop, you would not have access to it at all, you could only kill the process using Task Manager or, say, use some global hotkey (if you are interested, ask me how, right now it would be off-topic). You need something to show in a desktop.

—SA


这篇关于无法访问C#中的静态公共类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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