从其他C#类访问mainwindow工具 [英] Access mainwindow tools from other C# classes

查看:384
本文介绍了从其他C#类访问mainwindow工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need to access textbox and label controls for other c# classes(not only MainWindow class) in wpf.
Is it possible to populate MainWindow class Tools for other classes? 
Here is my simplified example code:







<Window x:Class="MyAbsClass.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:MyAbsClass"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:FieldModifier="public" x:Name="txt1"/>
        <TextBox x:FieldModifier="public" x:Name="txt2"/>
        <Button x:FieldModifier="public" x:Name="btn1" Content="Button" Click="btn1_Click"/>
    </Grid>
</Window>







namespace MyAbsClass
{   
    class manipulate
    {
        public void add()
        {
           int a=int32.Parse(txt1.Text);
	   int b=int32.Parse(txt2.Text);
        }
    }   
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
		MessageBox.Show("Added value= "+(a+b));
        }
    }
}





我尝试了什么:



我使用x:FieldModifier =public但仍然是负数。



What I have tried:

I used x:FieldModifier="public" but still negative.

推荐答案

您显示的代码不起作用:它甚至不会编译!

The code you show won't work: it won't even compile!
MessageBox.Show("Added value= "+(a+b));



您展示的唯一a和b属于不同的类别,并且是 add 方法,这意味着它们仅在该方法中可用。即使它们是类的一部分(即公共或私有),它们也不能作为主窗口的一部分直接访问,因为它们是基于实例的,并且需要一个操作的示例要上班的课程。

并且操纵课程无法访问文本框,因为它根本不是一个控件而且可以'主机UI元素。

文本框是 MainWindow 类的一部分,只能在其中访问。

如果操作类需要访问值,则必须通过构造函数或通过属性将它们从窗口传递到类的相应实例:


The only "a" and "b" you show are in a different class, and are local to the add method, which means that they are available only within that method. Even if they were part of the class (i.e. public or private) they wouldn't be directly accessible as part of the main window as they would be instance based and would require an example of the manipulate class to work.
And the manipulate class can't access the text boxes because it's not a control at all and can't host UI elements.
The text boxes are part of the MainWindow class, and are only supposed to be accessible within it.
If the manipulate class needs to access the values, they must be passed from the window to the appropriate instance of the class, either via a constructor, or via properties:

class Manipulate
    {
    public string ParamA { get; set; }
    public string ParamB { get; set; }
    public int Add()
       {
       int a=int32.Parse(ParamA);
       int b=int32.Parse(ParamB);
       return a + b;
       }
    }




public partial class MainWindow : Window
    {
    public MainWindow()
       {
       InitializeComponent();
       }
     private void btn1_Click(object sender, RoutedEventArgs e)
       {
       Manipulate m = new Manipulate();
       m.ParamA = txt1.Text;
       m.ParamB = txt2.Text;
       MessageBox.Show("Added value= " + m.Add());
       }
    }


首先要明白的是:通常情况下,你做的事情不是来自班级,而是来自类实例,除非其他类方法是静态的。同样,更重要的是,表单表示从 System.Windows.Forms.Form 派生的某个类的实例。通常,您在问题中提到的控件是某些 Form 类的实例成员;因此,你需要一个实例,一个对表单实例的引用,来读取或修改控件的状态或任何控件的属性。



你怎么没关系传递实例。例如,您可以使用构造函数的其他类接受特定表单类的参数。在表单类中,您可以公开控件,因此您可以通过表单引用进行引用:

First thing to understand: most usually, you do something not "from class", but "from a class instance", unless the "other class" method is static. Likewise, and more importantly, the "form" means the instance of some class derived from System.Windows.Forms.Form. Normally, the controls you mentioned in the questions, are the instance members of some Form class; therefore, you need an instance, a reference to the form instance, to read or modify the state of the control or any control's properties.

It does not matter how you pass the instance. For example, you can have "other class" with constructor which accept a parameter of specific form class. In a form class, you can expose a control, so you would have its reference through the form reference:
myMainForm.myLabel.Text = "Done";



然而,这不是一个干净或整洁的做事方式。直接暴露控件真的很糟糕。如果某些类引用了某些表单控件,那么这个类最好应该是相同的表单(一个好主意:在单独的文件中使用相同的部分声明,但是它的一部分同班)。 公开一些功能要好得多。使用表单的类不必知道这是表单。最好是某些接口实例仅公开实现某些功能所需的接口成员。表单类实现了一些接口。我的文章解释了这个想法:一次回答的许多问题 - 之间的协作Windows窗体或WPF Windows



不要混淆:本文解释了表单与窗体或窗口的WPF窗口的协作,只是因为这是常见问题之一(非常频繁:-))。但是,如果这是表单与非表单类的某个对象之间的协作,则没有任何变化。



-SA


However, this is not a clean or neat way of doing things. It's really bad to expose controls directly. If some class has reference to some form controls, this class should better be the same form (one good idea: the same partial declaration in a separate file, but a part of the same class). It's much better to expose some functionality. The class working with the form does not have to "know" this is the form. It's better be some interface instance exposing only the interface members required to implement some functionality. The form class implements some interface. The idea is explained in my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

Don't be confused: the article explain collaboration of a form with a form or a WPF window with a window, just because this is one of the frequently asked questions (very frequently :-)). But nothing changes if this is the collaboration between a form and some object of a non-form class.

—SA


这篇关于从其他C#类访问mainwindow工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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