【发布时间】:2019-05-17 09:37:38
【问题描述】:
我有 2 个按钮,但它们需要位于与 mainWindow.cs 不同的文件中。 我不知道该怎么做。
所以Button_Click_2 必须在ReadData.cs 和
Button_Click_3 必须在 WriteData.cs 中
当按钮不在主窗口中时,应用程序无法识别按钮。 我怎样才能做到这一点 ?
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 之外的另一个文件中的代码。
我被困住了……你们有解决办法吗?
【问题讨论】:
-
如果我理解正确,您可以将此代码
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(); }放在 ReadData.cs 中,然后按下按钮不起作用 -
如果您将
button_Click_2代码放在MainWindow.xaml.cs中,它可以正常工作 -
是的,但我需要在 mainWindow.cs 之外执行此按钮。
-
"按钮的代码必须在 ReadData.cs" - 这是完全错误的。您正在谈论在 MainWindow.xaml 中使用的按钮单击事件处理程序方法。因此,该方法必须在 MainWindow 类中声明,即在 MainWindow.xaml.cs 中。您实际需要的是对 ReadData 类实例的对象引用,以调用其方法或设置其属性。或者您在没有对象引用的情况下调用 ReadData 类的静态方法。
-
是的,当我单击按钮时,它需要访问 ReadData.cs 并执行代码。