简体   繁体   English

如何从C#中的其他类访问MainWindow类工具

[英]How to access MainWindow class tools from other classes in c#

I need to access textbox and label controls for other c# classes(not only MainWindow class) in wpf. 我需要访问wpf中其他c#类(不仅是MainWindow类)的文本框和标签控件。 I used x:FieldModifier="public" but still negative. 我使用x:FieldModifier =“ public”,但仍为负数。 Is it possible to populate MainWindow class Tools for other classes? 是否可以为其他类填充MainWindow类工具? 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>

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));
    }
}
}

You should always be passing this information to the class, this is separation of concerns and ensures when the view changes you don't have to update your other classes. 您应该始终将此信息传递给类,这是关注点分离,并确保在视图更改时您不必更新其他类。 It also means other classes can be reused. 这也意味着可以重用其他类。

You're also coming at this problem from the wrong way for WPF - you don't want to get the value from the TextBox, you want the TextBoxto set the value. WPF的错误方式也会导致此问题-您不想从TextBox中获取值,而是希望TextBox设置值。 You should use a binding for this. 您应该为此使用绑定。

Here is a working sample for you, I appreciate it doesn't directly answer your question but I hope it helps you in the right direction. 这是一个适合您的工作示例,我很欣赏它不能直接回答您的问题,但希望它能在正确的方向上为您提供帮助。

View: 视图:

<Window x:Class="WPFDynamicControls.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:WPFDynamicControls"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" >
    <StackPanel>
        <TextBox Text="{Binding BoxA}"/>
        <TextBox Text="{Binding BoxB}"/>
        <Button Content="Add" Click="btn1_Click"/>
    </StackPanel>
</Window>

Code behind: 后面的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.DataContext = this;
        InitializeComponent();
    }

    public int BoxA { get; set; }

    public int BoxB { get; set; }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Added value= " + Manipulate.Add(this.BoxA, this.BoxB));
    }
}

public static class Manipulate
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}
var mainWindow = new MainWindow();
int a = int32.Parse(mainWindow.txt1.Text);
int b = int32.Parse(mainWindow.txt2.Text);

This method should give you access to anything in any window by passing the reference to a static property: 通过将引用传递给静态属性,此方法应使您可以访问任何窗口中的任何内容:

public partial class MainWindow : Window
{
    public static TextBox txt1Mirror;
    public static TextBox txt2Mirror;

    public MainWindow()
    {
        InitializeComponent();
        txt1Mirror = txt1;
        txt1Mirror = txt2;
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        Manipulate.Add();
    }
}

public static class Manipulate
{
    public static void Add()
    {
       int a = int32.Parse(MainWindow.txt1Mirror.Text);
       int b = int32.Parse(MainWindow.txt2Mirror.Text);

       MessageBox.Show("Added value= " + (a + b));
    }
}

A Better More Clean Solution 更好更清洁的解决方案

You could simply use Binding with an Interface then pass the entire window: 您可以简单地将BindingInterface 结合使用,然后传递整个窗口:

<StackPanel>
    <TextBox Text="{Binding X}"/>
    <TextBox Text="{Binding Y}"/>
    <Button Content="Add" Click="Btn_Add"/>
</StackPanel>

Implement the Interface: 实施接口:

interface IManipulate
{
   int X { get; set; }
   int Y { get; set; }
}

Setup your window: 设置窗口:

public partial class MainWindow : Window, IManipulate
{
    public int X { get; set; }
    public int Y { get; set; }

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_Add(object sender, RoutedEventArgs e)
    {
        Manipulate.Add(this);
    }
}

public static class Manipulate
{
    public static void Add(IManipulate e)
    {
       MessageBox.Show("Added value= " + (e.X + e.Y).toString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM