使用 IValueConverter 的货币转换器
Currency Converter using IValueConverter
我正在使用 XAML 和 C# 开发 Windows 8.1 Store App。
我添加了 2 个文本框并实现了 IValueConverter 接口。
这里是Xaml代码
<Page.Resources>
<local:ConverterClass x:Key="C_Converter" />
<local:EuroConverterClass x:Key="Euro_Converter" />
<local:YenConverterClass x:Key="Yen_Converter" />
</Page.Resources>
<TextBox Name="PKR_TextBox"
Grid.Row="1"
Grid.Column="2"
Width="450"
Height="50"
FontSize="30"
FontWeight="Bold"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
<TextBox Name="DOLLAR_TextBox"
Grid.Row="2"
Grid.Column="2"
Text="{Binding ElementName=PKR_TextBox, Path=Text, Converter={StaticResource C_Converter}}"
Width="450"
Height="50"
FontSize="30"
FontWeight="Bold"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
这是我的转换器Class代码:
class ConverterClass : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
int pkr;
int dollar = 0;
if (Int32.TryParse(value.ToString(), out pkr))
{
dollar = pkr * 0.0099;
}
return dollar;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我试图在 运行 时转换货币,当用户在 PKR 文本框中输入一个值时,它应该会自动更新美元文本框。但它却给我一个错误 "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)".
请帮助并忽略我糟糕的英语。
错误信息很清楚。您必须使用 double
值进行计算。 double
是使用 C# 时的默认浮点类型。
public object Convert(
object value, Type targetType, object parameter, string language)
{
double pkr;
double dollar = 0.0;
if (double.TryParse(value.ToString(), out pkr))
{
dollar = pkr * 0.0099;
}
return dollar;
}
我正在使用 XAML 和 C# 开发 Windows 8.1 Store App。
我添加了 2 个文本框并实现了 IValueConverter 接口。
这里是Xaml代码
<Page.Resources>
<local:ConverterClass x:Key="C_Converter" />
<local:EuroConverterClass x:Key="Euro_Converter" />
<local:YenConverterClass x:Key="Yen_Converter" />
</Page.Resources>
<TextBox Name="PKR_TextBox"
Grid.Row="1"
Grid.Column="2"
Width="450"
Height="50"
FontSize="30"
FontWeight="Bold"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
<TextBox Name="DOLLAR_TextBox"
Grid.Row="2"
Grid.Column="2"
Text="{Binding ElementName=PKR_TextBox, Path=Text, Converter={StaticResource C_Converter}}"
Width="450"
Height="50"
FontSize="30"
FontWeight="Bold"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
这是我的转换器Class代码:
class ConverterClass : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
int pkr;
int dollar = 0;
if (Int32.TryParse(value.ToString(), out pkr))
{
dollar = pkr * 0.0099;
}
return dollar;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我试图在 运行 时转换货币,当用户在 PKR 文本框中输入一个值时,它应该会自动更新美元文本框。但它却给我一个错误 "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)".
请帮助并忽略我糟糕的英语。
错误信息很清楚。您必须使用 double
值进行计算。 double
是使用 C# 时的默认浮点类型。
public object Convert(
object value, Type targetType, object parameter, string language)
{
double pkr;
double dollar = 0.0;
if (double.TryParse(value.ToString(), out pkr))
{
dollar = pkr * 0.0099;
}
return dollar;
}