访问部分类之间的 UI 控件

发布于 2024-10-19 17:08:03 字数 745 浏览 3 评论 0原文

在我的 XAML 中,我有一个按钮,带有按钮单击事件。我在 MainWindow 类中初始化该组件:RibbonWindow。我在一个单独的类中有一个 Button Click 函数,但错误窗口给了我“FooApplication.MainWindow”不包含 fooBtn_Click 的定义,下面是我的代码

namespace FooApplication1 {
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();

        }
    }


/* in another .cs class file located in the same project */
    namespace FooApplication1
    {
        public partial class Jenny : MainWindow
        {        
            public void btnApplyL_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }
        }    
    }

,我试图给 Jenny 一个 RibbonWindow 部分,认为这可能是问题所在,但给出错误是什么?

In my XAML I have a Button, with a button click event. I initialize the component in my MainWindow class : RibbonWindow. I have a the Button Click function in a separate class, but error window gives me "FooApplication.MainWindow" does not contain a definition for fooBtn_Click", below is my code

namespace FooApplication1 {
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();

        }
    }


/* in another .cs class file located in the same project */
    namespace FooApplication1
    {
        public partial class Jenny : MainWindow
        {        
            public void btnApplyL_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }
        }    
    }

I tried to give Jenny a RibbonWindow partial, thinking it might be the issue, but gives error. What is wrong?

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

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

发布评论

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

评论(1

楠木可依 2024-10-26 17:08:03

您的 XAML 文件需要通过 class 属性指向负责事件处理的类。

x:Class="FooApplication1.Jenny"

在 XAML 中定义事件处理程序会强制使用 class 属性,因为它将指示处理程序存在的位置。您仍然可以选择部分课程;只是 XAML 需要知道哪个类负责修改 class 属性将解决的事件处理。

此外,您还需要将 InitializeComponent(); 调用从 MainWindow 类移至 Jenny 类。

    public partial class Jenny : MainWindow
    {        
        public Jenny()
        {
           InitializeComponent();
        }

        ...
    }

Your XAML file needs to point to the class responsible for the event handling via the class attribute.

x:Class="FooApplication1.Jenny"

Defining event handlers within the XAML forces the use of the class attribute as it will dictate where the handlers exist. You can still have the partial classes; it's just that the XAML needs to know which class is responsible for the event handling which modifying the class attribute will address.

In addition you will need to move the InitializeComponent(); call from the MainWindow class to the Jenny class.

    public partial class Jenny : MainWindow
    {        
        public Jenny()
        {
           InitializeComponent();
        }

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