按钮与主文件中的文件不同

时间:2019-05-17 09:37:39

标签: c# wpf xaml button

我有2个按钮,但它们必须位于与mainWindow.cs不同的文件中。 我不知道该怎么做。

因此,Button_Click_2必须位于ReadData.cs中,并且 Button_Click_3必须在WriteData.cs

当应用程序不在mainWindow中时,应用程序无法识别该按钮。 我怎样才能做到这一点 ?

ReadData.cs:

public new void Button_Click_2(object sender, RoutedEventArgs e)
    {
        string text = verifyCard("5"); // 5 - is the block we are reading
        textBlock1.Text = text.ToString();
    }

MainWindow.xaml.cs:

public void Button_Click_2(object sender, RoutedEventArgs e)
    {
        //When I click it don't detected the code in the ReadData.cs
        // The code of the button must be in the ReadData.cs
    }

MainWindow.xaml:

 <Grid>
    <Button Content="Connexion" HorizontalAlignment="Left" 
 Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44" 
 Click="Button_Click_1"/>
    <Button  Content="Lire donnée carte" HorizontalAlignment="Left" 
 Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42" 
 Click="Button_Click_2"/>
    <Button Content="Ecrire donnée carte" HorizontalAlignment="Left" 
 Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41" 
 Click="Button_Click_3"/>
    <TextBox Name="textBox1" HorizontalAlignment="Left" Height="23" 
 Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox" 
 VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
    <TextBlock Name="textBlock1" HorizontalAlignment="Left" 
 Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" 
 Height="23" Width="177"/>

</Grid>

必须有可能强制应用检测并在除Main以外的其他文件中执行代码。

我被卡住了...你们有解决办法吗?

1 个答案:

答案 0 :(得分:1)

您不能只将按钮代码放在ReadData.cs类中。由于该事件与MainWindow.xaml的ui有关,因此请为ReadData创建一个对象

 public new void Button_Click_2(object sender, RoutedEventArgs e)
 {
    ReadData rd= new ReadData();
    string text = rd.verifyCard("5"); 
    textBlock1.Text = text.ToString();
 }