WPF C# parameterFindNamestrArray1 怎么确定是什么类型的怎么拆箱
在WPF中,FindName方法返回的是一个object类型的对象。如果需要确定它的具体类型,可以使用 is 运算符或者 as 运算符。
使用 is 运算符:
object obj = parameter.FindName(strArray[1]);
if (obj is Button)
{
Button btn = (Button)obj; //拆箱
//对btn进行操作
}
使用 as 运算符:
object obj = parameter.FindName(strArray[1]);
Button btn = obj as Button;
if (btn != null)
{
//对btn进行操作
}
在拆箱时,需要注意类型转换是否安全,否则会抛出异常。
原文地址: https://www.cveoy.top/t/topic/cRj9 著作权归作者所有。请勿转载和采集!