저는 코딩이 매우 쉽습니다. 입력 변수가 관련된 경우 B에서 클래스 A로 메소드를 호출 할 때 문제점이 있습니다. 왜 설명 a.PrintAfromB(); 저에게 0 값을 주셨습니다. 상황을 극복하는 방법.? MVVM을 사용하는 예제가 있습니다. 그러나 그것들은이 단계에서 나를 위해 매우 복잡합니다. 입력 변수가 잘못되거나 메서드를 잘못 호출하는 것 같습니다. 나는 빨려서 이것을 해결하지 않고 앞으로 나아갈 수 없습니다.다른 클래스에서 호출하는 C# (WPF) 메서드
메인
public partial class MainWindow : Window {
public MainWindow(){
InitializeComponent();
this.DataContext = this;
}
B b = new B();
A a = new A();
private void Button_Click(object sender, RoutedEventArgs e) {
b.Bat = double.Parse(one.Text);
b.PrintB();
a.PrintAfromB();
a.PrintAfromBB();
}
}
class A
{
double Apple { get; set; }
B b1 = new B();
public void PrintAfromB() {
Console.WriteLine("Calling method from B where input involved: "+b1.CallB());
}
public void PrintAfromBB() {
Console.WriteLine("Calling method from B where input not involved: " + b1.CallBB());
}
}
,363,210B
public class B{
public double Bat { get; set; }
public double k = 0;
public void PrintB(){
k = Bat;
Console.WriteLine("test input value" +k);
}
public double CallB(){
return 10 * Bat;
}
public double CallBB(){
return 10 * 10;
}
}
XAML
<Window x:Class="inputtest.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:inputtest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="62,141,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
<TextBox x:Name="one" HorizontalAlignment="Left" Height="23" Margin="198,134,0,0" TextWrapping="Wrap" Text="10" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="380,263,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
화면 이름은 처음에'YIKES' 코드를 처음 보았을 때 제공 한 것입니다. 변수를 만들 때 좀 더 의미있는 이름을 사용 하시길 권합니다. 무료/온라인 Google'C# Basics Tutorial'을 읽고/이해합니다. 'Class Section'에 중점을두고 디버거를 사용하고, 중단 점을 설정하고,이 코드를 통해 스테핑을 시작하는 방법을 배웁니다. WPF는 고전적인 winforms로 시작하십시오. 초보자를 위해 좀 더 진보 된 것입니다. – MethodMan
제안을 주셔서 대단히 감사합니다. :) – Yikes