访问部分类之间的 UI 控件
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 XAML 文件需要通过
class
属性指向负责事件处理的类。在 XAML 中定义事件处理程序会强制使用
class
属性,因为它将指示处理程序存在的位置。您仍然可以选择部分课程;只是 XAML 需要知道哪个类负责修改class
属性将解决的事件处理。此外,您还需要将
InitializeComponent();
调用从MainWindow
类移至Jenny
类。Your XAML file needs to point to the class responsible for the event handling via the
class
attribute.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 theclass
attribute will address.In addition you will need to move the
InitializeComponent();
call from theMainWindow
class to theJenny
class.