c#WPF使用MainWindow.xaml.cs中的第二个window2.xaml [英] c# WPF using second window2.xaml from MainWindow.xaml.cs

查看:305
本文介绍了c#WPF使用MainWindow.xaml.cs中的第二个window2.xaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的是,我是否可以编写第二个window2.xaml的代码,我已经从"第一个"添加了该代码。 MainWindow.xaml.cs。


通常当你添加一个新窗口时,你必须在新窗口中编写代码2.xaml.cs


但是我想编写MainWindow.xaml.xs中的所有内容。我想在window2.xaml上创建一个新按钮并在MainWindow.xaml.cs上编码


答案是创建用户控件吗?如果是这样,你怎么做?


ex:


    public partial class MainWindow:Window

          {

         


        public MainWindow()

        {

            InitializeComponent();

        }¥b $ b        

       私人无效  SecondWin2_Click(对象发件人,RoutedEventArgs e)

        {

            SecondWindow win2 = new SecondWindow();

            win2.Show();

            this.Close();

        }¥b $ b    }



            

解决方案


您好MDiesel_Z,


< span class =""id ="x_x_result_box"lang ="en"tabindex =" - 1"> 每个窗口在.cs文件后面都有一个单独的代码, < span class ="x_x_short_text"id ="x_x_result_box"lang ="en"tabindex =" - 1"> 每个窗口由不同的
类名分隔。


定义处理事件的业务逻辑的事件订阅者代码应该在订阅者中。


如果您想要Main 窗口
订阅window2按钮 点击事件,你应该
订阅window2按钮 点击下面的事件

 public MainWindow()
{
InitializeComponent();

Window1 window1 = new Window1();
window1.Show();
window1.Btn.Click + = Btn_Click;
}

private void Btn_Click(object sender,RoutedEventArgs e)
{
MessageBox.Show(" MainWindow response");
}

在这方面,Window2中的按钮应该是Public,如果你在xaml中定义了这个按钮,你应该给按钮命名。


我不知道你为什么这样做,这不是一个好习惯,因为你必须确保订阅者的生命周期长于Publisher,
否则容易导致内存泄漏。



此致,


Bob


What I am wondering is if I can write code for the second window2.xaml that I have added from the "first" MainWindow.xaml.cs.

Usally when you add a new window you wound have to write code in the new window2.xaml.cs

But I want to code everything from the MainWindow.xaml.xs. I want to create a new button on the window2.xaml and code it on MainWindow.xaml.cs

Would the answer be to create user control? If so how do you do it?

ex:

    public partial class MainWindow : Window
         {
         

        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void  SecondWin2_Click(object sender, RoutedEventArgs e)
        {
            SecondWindow win2 = new SecondWindow();
            win2.Show();
            this.Close();
        }
    }

            

解决方案

Hi MDiesel_Z,

Each window has a separate code behind the .cs file,each window is separated by a different class name.

The event subscriber 's code that defines the business logic to handle the event should in subscriber.

If you want MainWindow subscribe window2 ButtonClick Event, you should subscribe the window2 ButtonClick Event as below.

        public MainWindow()
        {
            InitializeComponent();

            Window1 window1 = new Window1();
            window1.Show();
            window1.Btn.Click += Btn_Click;
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("MainWindow response");
        }

In this regard, your button in Window2 should be Public, if you define this button in xaml, you should give the button a name.

I don't konw why you do that, this is not good practice, since you have to ensure the life cycle of subscriber long than Publisher, otherwise easily lead to memory leaks.

Sincerely,

Bob


这篇关于c#WPF使用MainWindow.xaml.cs中的第二个window2.xaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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