加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么我的转换器会出现无效的转换错误?

发布时间:2020-12-15 08:07:53 所属栏目:百科 来源:网络整理
导读:我创建了一个转换器,从double转换为整数. 但是行“return(int)value;”总是得到“指定演员阵容无效”. 我该怎么做才能让我的Converter成功转换一个double并发回一个整数? 转换器: namespace TestChangeAngle{ [ValueConversion(typeof(double),typeof(int)
我创建了一个转换器,从double转换为整数.

但是行“return(int)value;”总是得到“指定演员阵容无效”.

我该怎么做才能让我的Converter成功转换一个double并发回一个整数?

转换器:

namespace TestChangeAngle
{
    [ValueConversion(typeof(double),typeof(int))]
    class DoubleToIntegerConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value,System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

XAML:

<Page x:Class="TestChangeAngle.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestChangeAngle"
    Title="Page1">
    <Page.Resources>
        <local:DoubleToIntegerConverter x:Key="DoubleToIntegerConverter"/>
    </Page.Resources>

    <StackPanel HorizontalAlignment="Left" Margin="20">
        <Image Source="imageslogo2.png" 
               RenderTransformOrigin="0.5,0.5"
               Width="100" 
               Margin="10">
            <Image.RenderTransform>
                <RotateTransform Angle="{Binding ElementName=TheSlider,Path=Value}"/>
            </Image.RenderTransform>
        </Image>
        <Slider x:Name="TheSlider"
                Width="200" 
                Minimum="0"
                Maximum="360"
                HorizontalAlignment="Center"
                Margin="10" 
                Cursor="Hand"/>
        <TextBox x:Name="TheAngle"
                 Margin="10"
                 Width="100">
            <TextBox.Text>
                <Binding ElementName="TheSlider"
                         Path="Value"
                         UpdateSourceTrigger="PropertyChanged"
                         Converter="{StaticResource DoubleToIntegerConverter}"
                         Mode="TwoWay">
                    <Binding.ValidationRules>
                        <local:MinMaxValidationRule Minimum="0" Maximum="360"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

    </StackPanel>
</Page>

解决方法

您正在尝试将(不转换)从double转换为int,这将无效.你需要进行隐式转换或使用Convert.ToInt32() – 因为参数实际上是对象类型我认为你需要后者来保持编译器满意.是否要包含文化格式提供程序取决于您.
public object Convert(object value,System.Globalization.CultureInfo culture)
{
    return Convert.ToInt32(value);
}

当对象具有相同的形状时,即当一个对象是您要转换的类型的实例时,可以使用强制转换操作符.例如,如果类Foo扩展了类Bar,那么您可以将类型为Foo的对象转换为类型Bar – 因为Foo对象具有Bar对象所具有的所有方法和属性.但是,您无法将类型为Bar的对象强制转换为Foo,因为通过添加Bar对象不具有的方法或属性,Foo会更改(或者可以更改,就编译器而言)Bar的形状.

在你的情况下,你正在处理只共享对象接口的原始类型 – 除了它们都是从对象派生之外,它们之间没有继承关系.然而,两者之间存在隐含的转换.您可以将一种类型的对象分配给另一种类型的变量,但可能会丢失该值的某些精度.

double x = 1.1;
int y = 0;

y = x;  // implicit conversion,this works,y will be 1
x = y;  // implicit conversion,x will be 1.0

但是,您不能将一种类型的对象强制转换为另一种对象.强制转换意味着您将使用该对象,就好像它是另一种类型一样.在这种情况下,形状不同,无法完成.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读