从其他类访问 MainWindow 属性

发布于 2024-08-08 20:00:48 字数 171 浏览 1 评论 0原文

我可以在 WPF 应用程序的 MainWindow.xaml.cs 文件中轻松修改/访问控件和 MessageBox。但是,当我创建一些文件夹并在其中添加类时,我无法再访问 MessageBox,也无法在 Intellisense 下拉列表中看到控件名称。

(这看起来很简单,但我是 C# 和 WPF 的新手)

I can modify/access controls and MessageBox with ease in the MainWindow.xaml.cs file of my WPF application. However, when I create some folders and add classes there, I no longer have access to MessageBox nor do I have see the control names in the Intellisense dropdown.

(This seems quite elementary but I'm new to C# and WPF)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-08-15 20:00:48

您一定缺少命名空间。您可以执行以下操作:


MainWindow.cs:

 using System;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;

namespace YourNameSpaceHere
{
    public partial class MainWindow : Window
    {
        internal static string message_ = string.Empty;
        MainWindow()
        {
            InitializeComponent();
            SetupMessage();
        }

        private void SetupMessage()
        {
            message_ = "Hello World!";
        }
    }
}

OtherFile.cs :

using System;
using System.Text;
using System.Windows;
using System.Windows.Controls; //are you missing this namespace?
namespace YourNameSpaceHere
{
        public class Messaging
        {
            Messaging()
            {
                MessageBox.Show(MainWindow.message_);
            }
        }
}

请注意,我在每个文件中使用相同的命名空间,并且通过使用内部关键字,可以将消息只要位于同一命名空间中,任何东西都可以访问。我希望这有帮助!

You must be missing a namespace. You could do something like this:


MainWindow.cs:

 using System;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;

namespace YourNameSpaceHere
{
    public partial class MainWindow : Window
    {
        internal static string message_ = string.Empty;
        MainWindow()
        {
            InitializeComponent();
            SetupMessage();
        }

        private void SetupMessage()
        {
            message_ = "Hello World!";
        }
    }
}

OtherFile.cs :

using System;
using System.Text;
using System.Windows;
using System.Windows.Controls; //are you missing this namespace?
namespace YourNameSpaceHere
{
        public class Messaging
        {
            Messaging()
            {
                MessageBox.Show(MainWindow.message_);
            }
        }
}

Note that I am using the same namespace in each file and by using the internal keyword the message can be accessed by anything as long as it is in the same namespace. I hope this helps!

酒儿 2024-08-15 20:00:48

您的类是否具有正确的 using 语句,允许您访问您在创建的新类中尝试访问的类?

Does your classes have the right using statement in that would allow you access to the class you are attempting to access within the new classes you've created?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文