首页 > 解决方案 > 是否可以添加在单独的 cs 中创建的子元素

问题描述

我对编程还是很陌生,并不真正了解如何将负载外包给其他 class1.cs。我想知道是否可以在另一个 .cs 和 grid.Children.add(//Button); 中创建一个按钮或标签?

这就是我目前所处的位置。

using System.Windows;

namespace IconManager
{
    public partial class MainWindow : Window
    {
        Test NewTest;
        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NewTest = new Test();
            NewTest.some();
        }
    }
}

using System.Windows.Controls;
using System.Windows.Media;

namespace IconManager
{
    class Test : MainWindow
    {
        public Test()
        {
            InitializeComponent();
            
        }

        public void some()
        {
            Button Zan = new Button() { Height = 100, Width = 100, Background = Brushes.Black };
            grid.Children.Add(Zan);
        }
    }
}

标签: c#

解决方案


是的,这也是我开始在 WPF 中编程时遇到的问题。一般来说,学习更多关于 MVVM (Model-View-ViewModel) 和 MVC (Model-View-Controller) 等前端模式的知识对我构建应用程序有很大帮助。

但是从你的问题开始,让我这样说:可以将窗口或视图容器传递给其他类。例如,您可以执行以下操作:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var mainWindowFiller = new MainWindwoFiller(this.mainGrid);
    }
}

请注意,我在 MainWindow 中使用 x:Name="mainGrid" 命名了我的网格。然后 MainWindowFiller 可以执行以下操作:

 public class MainWindwoFiller
{
    private readonly Grid _mainGrid;

    public MainWindwoFiller(Grid mainGrid)
    {
        _mainGrid = mainGrid;
        FillMainWindowGrid();
    }

    private void FillMainWindowGrid()
    {
        var button = new Button();
        button.Click += Button_Click;
        _mainGrid.Children.Add(button);
        _mainGrid.Children.Add(new Label()
            {
                Content = "Label"
            }
        );
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Click!");
    }
}

在此示例中,将两个简单的 UI 元素添加到网格中。您实际上可以在此处执行所有操作,例如删除网格并添加画布或添加新事件。实际上您可以在 MainWindow.xaml.cs 中执行的所有操作。

但是,即使这可以解决您的直接问题,也不是推荐的方式。我主要使用 MVVM 模式,建议在视图中使用绑定(视图基本上是所有容器,其中包含 Window 或 UserControl 等可视元素)。通过绑定,您可以使用另一个类中的属性和命令。但是为了让它更明显,这里有一个小例子:

<Window x:Class="WpfTestApp.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:WpfTestApp"
    mc:Ignorable="d" DataContext="MainWindowViewModel"
    d:DataContext="{d:DesignInstance local:MainWindowViewModel, IsDesignTimeCreatable=True}"
    Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid x:Name="mainGrid">
       <Label Height="80" Width="200" VerticalAlignment="Top" Margin="0,100,0,0" Content="{Binding LabelText}"/>
       <Button x:Name="MainWindowButton" Height="40" Width="120" Content="{Binding ButtonText}" Click="MainWindowButton_OnClick"></Button>
    </Grid>
</Window>

对应的 xaml.cs 文件:

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

    private void MainWindowButton_OnClick(object sender, RoutedEventArgs e)
    {
        ((MainWindowViewModel)this.DataContext).ButtonClickEvent();
    }
}

和视图模型:

    public class MainWindowViewModel
{
    public string ButtonText { get; set; } = "Click Me";
    public string LabelText { get; set; } = "Sir ReadALot";

    public void ButtonClickEvent()
    {
        MessageBox.Show("Click!");
    }
}

现在,您可以从 ViewModel 访问数据或连接到后端服务。但是使用这种结构,您应该能够克服大多数挑战。如果您想拥有可更改的属性,您将需要 INotifyPropertyChanged 稍后。但这是一个不同的话题。我希望这可以帮助您在开发人员的道路上更进一步。干杯!


推荐阅读