首页 > 解决方案 > 无法将类型“System.Windows.Forms.Form”隐式转换为“bool”

问题描述

我将 DiscordRPC 放入我的应用程序中。我确保窗口处于活动状态,因此它可以轻松更改并实际更改状态。问题是在使用 ActiveForm 时,它不起作用,因为它无法将其转换为“布尔”。这是我的代码。

private void active()
        {
            Form currentForm = Form.ActiveForm;

            if(Form.ActiveForm)
            {
                ptr2tools.Presence = new RichPresence()
                {
                    State = "Using pwf2tex",
                    Assets = new Assets()
                    {
                        LargeImageKey = "pwf2tex",
                        LargeImageText = "pwf2tex"
                    }
                };
                ptr2tools.DiscordClient.SetPresence(ptr2tools.Presence);
            }
        }

标签: c#boolean

解决方案


Form.ActiveForm 保留当前活动的表单,如果没有活动的表单,则为 null。

 var currentForm = Form.ActiveForm;

 if(currentForm != null)
 {
     //logic
 }

推荐阅读