Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建IntegerUpDown ValueChanged事件时出现编译器错误_C#_Wpf - Fatal编程技术网

C# 创建IntegerUpDown ValueChanged事件时出现编译器错误

C# 创建IntegerUpDown ValueChanged事件时出现编译器错误,c#,wpf,C#,Wpf,我正在使用来自的IntegerUpDown控件 但是,我无法将事件分配给我的函数,因此当值更改时,它将调用我的函数。我对c#和wpf都是新手,非常感谢您的帮助。我一直在努力让它像一个类似的例子中显示的那样工作 private IntegerUpDown m_argumentUpDown; 公共IntArgumentOption(ArgumentOption描述argumentDesc):基础(argumentDesc) { m_argumentUpDown=新整数下降 { 水印=argument

我正在使用来自的
IntegerUpDown
控件

但是,我无法将事件分配给我的函数,因此当值更改时,它将调用我的函数。我对c#和wpf都是新手,非常感谢您的帮助。我一直在努力让它像一个类似的例子中显示的那样工作

private IntegerUpDown m_argumentUpDown;
公共IntArgumentOption(ArgumentOption描述argumentDesc):基础(argumentDesc)
{
m_argumentUpDown=新整数下降
{
水印=argumentDesc.m_水印,
增量=(int)argumentDesc.m_间隔,
最小值=(int)argumentDesc.m_最小值,
最大值=(int)argumentDesc.m_最大值,
FormatString=“G”,
选择AllongotFocus=true,
最小宽度=50,
FontSize=10,
余量=新系统窗口厚度(5,0,0,0),
};
//编译器错误:
m_argumentUpDown.ValueChanged+=新路由属性ChangedEventHandler(ArgumentChanged);
}
无效参数已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
}
执行此操作将导致编译器错误:

错误CS0029:无法将类型“System.Windows.RoutedPropertyChangedEventHandler”隐式转换为“System.Windows.RoutedPropertyChangedEventHandler


下面的方法会起作用,我已经测试过了。但我不知道这是否被认为是一种变通方法,或者IntegerUpDown控件的创建者是想这样使用它的

m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
//or you can change above line to following for brevity. ReSharper always suggesting me to do this
//m_argumentUpDown.ValueChanged += ArgumentChanged;

void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    //you need to cast New and Old value to int since both are of type object now
    int newVal = (int)e.NewValue;
    int oldVal = (int)e.OldValue;
}
m_argumentUpDown.ValueChanged+=新路由属性changedeventhandler(ArgumentChanged);
//或者,为了简洁起见,您可以将“上一行”更改为“下一行”。ReSharper总是建议我这么做
//m_argumentUpDown.ValueChanged+=ArgumentChanged;
无效参数已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
//您需要将新值和旧值强制转换为int,因为它们现在都是object类型
int newVal=(int)e.NewValue;
int oldVal=(int)e.OldValue;
}

在UpDownBase类(Xceed.wpfToolkit.dll)中,ValueChanged的方法签名为:

public event RoutedPropertyChangedEventHandler<object> ValueChanged;
公共事件RoutedPropertyChangedEventHandler值已更改;
因此,在代码中,您必须声明一个事件处理程序,其中泛型的类型为“Object”而不是int。由于类型不匹配,编译器无法从对象隐式转换为Int。 因此,更改代码如下

m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);

    }

    void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
           //type caste e.newValue and e.OldValue 
    }
m_argumentUpDown.ValueChanged+=新路由属性changedeventhandler(ArgumentChanged);
}
无效参数已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
//类型caste e.newValue和e.OldValue
}

您是否尝试显式使用它?如果您指的是新的System.Windows.RoutedPropertyChangedEventHandler(ArgumentChanged),则类似于
(System.Windows.RoutedPropertyChangedEventHandler)错误\u变量
?这不起作用。你试过使用可空类型吗?ie:RoutedPropertyChangedEventArgs也不起作用。它只是将错误输出更改为int?相反
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);

    }

    void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
           //type caste e.newValue and e.OldValue 
    }