为什么我不能在方法中使用这些选定索引?- 克罗地亚库纳到欧元货币转换器



对C#来说是个全新的概念。

我想从组合框中读取用户输入,并在将一种货币转换为另一种货币的方法中使用所选货币。输出不在控制台中,而是在Windows窗体上。

这是我的组合框输入读取代码:

public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double first = comboBox1.SelectedIndex;
}
public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
double second = comboBox2.SelectedIndex;
}

这是我的代码,它使用选定的索引并转换它们:

internal class Currencies
{
public double[] Convert(double[] currencies, int amount)
{
double[] convertedAmount = new double[currencies.Length];
if (Converter.comboBox1.first == HRK && Converter.comboBox2.second == EURO)
{
int exchangeRate1 = 1;
double exchangeRate2 = 7.4;
for (int i = 0; i < currencies.Length; i++)
{
convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
}
}
else if (Converter.comboBox1.first == EURO && Converter.comboBox2.seocnd == HRK)
{
int exchangeRate1 = 1;
double exchangeRate2 = 7.4;
for (int i = 0; i < currencies.Length; i++)
{
convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
}
}
else
{
throw new NotSupportedException();
}
return convertedAmount;
}
}

为什么这个方法不让我使用变量";第一个";以及";第二个";我将选定的索引存储在何处以转换货币?

去掉selectedIndexChanged过程,因为您在那里声明局部变量,这些变量在过程激发后会被垃圾收集。

只需将Convert((方法中的第一个第二个合框1.SelectedValueandcomboBox2.SelectedValue

很抱歉出现问题,我有

public string SelectedItem { get; set; }

您必须添加新类,如utils,并且必须添加到{ get ; set }

你所有的概念这是更好的

你可以使用这种方法,这是我在应用程序中经常做的事情

  1. 创建三个文件夹(ViewModel、View和model(
  2. 安装fody.propertyChange from Nugget
  3. 为页面创建视图模型
  4. 在视图模型中定义变量string SelectedIted {get; set;}
  5. 在页面后面的xaml代码中,放入DataContext = new NameOfYourViewModel

类似的东西


<ComboBox
Grid.Row="2"
Grid.Column="1"
Margin="10,0,10,0"
HorizontalAlignment="Stretch"
ui:ControlHelper.PlaceholderText="Select Lenguague"
DisplayMemberPath="Value.Name"
ItemsSource="{Binding LanguagesDictionary}"
SelectedValue="{Binding SelectedItem}"
SelectedValuePath="Value.Code" />

在您的VM 中

public string SelectedItem { get; set; }

在后面的XAML代码中

DataContext = new AudioPageViewModel();

最新更新