当前位置:网站首页>WPF中报错:“未将对象引用设置到对象的实例。”
WPF中报错:“未将对象引用设置到对象的实例。”
2022-07-31 12:07:00 【Monkey_King_GL】
项目场景:
在WPF中,我们经常需要使用到各种控件的属性,不管是在后端还是前端,都避免不了对控件的操作,但是大家在使用的过程中我们一定要注意一个很重要的细节!!!
问题描述
今天我想从后台动态设置前端控件的属性时,突然遇到一个问题,我的控件都显示“未将对象引用设置到对象实例”。代码如下:
前端代码
<Grid Grid.Column="2">
<TextBlock HorizontalAlignment="Left"
Margin="0 6 0 6"
TextWrapping="Wrap"
Text="断面类型:"
VerticalAlignment="Center"
Height="15"
Width="60" />
</Grid>
后端代码:
public void CrossSectionTextIsEnabled(BIM_CrossSection crossSection, bool temp)
{
//this.SectionName.IsEnabled = temp;
//this.SectionType.Text = crossSection.EarlySupportFill.ToString();
this.SectionType.IsEnabled = temp;
//this.SectionLevel.IsEnabled = temp;
//this.IsArchUp.IsEnabled = temp;
//this.ArchUpFill.IsEnabled = temp;
this.SecondArch.IsEnabled = temp;
this.SecondUp.IsEnabled = temp;
this.FirstArch.IsEnabled = temp;
this.FirstUp.IsEnabled = temp;
this.R4.IsEnabled = temp;
}
这段代码表面上看起来确实没什么问题,也符合WPF的语法。
原因分析:
后来通过认真查看代码,发现在初始化组件的时候出问题了。
错误示范:
public Win_ManageWindow(string sectionId)
{
Id = sectionId;
CrossSectionTextIsEnabled(crossSection, temp);
InitializeComponent();
LoadSource(sectionId, 1);
}
InitializeComponent()函数是初始化前端组件的,是将前端的各个组件加载到后端的。因此该代码的调用应该位于调用组件之前!!!
解决方案:
将InitializeComponent()函数设置到调用组件之前,即可解决该问题。
public Win_ManageWindow(string sectionId)
{
Id = sectionId;
InitializeComponent();
CrossSectionTextIsEnabled(crossSection, temp);
LoadSource(sectionId, 1);
}
边栏推荐
- imx6ull看门狗使用
- 关于IDEA开发工具的介绍
- 基于C51实现按键控制
- 如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
- 全动力学约束的机器人高效时间最优轨迹规划
- Getting started with jmeter performance testing steps (performance testing tool jmeter)
- Three-Phase PWM Rectifier Predictive Direct Power Control
- Shengxin Weekly Issue 38
- JVM 运行时数据区与JMM 内存模型详解
- [Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
猜你喜欢
WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
订song餐系统
502 bad gateway causes and solutions
Chrome开发自定义右键菜单实现快速跳转到指定页面
busybox之reboot命令流程分析
数据持久化技术——MP
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
消息队列面试题(2022最新整理)
MySQL index usage and optimization
Mysql环境变量的配置(详细图解)
随机推荐
The latest MySql installation teaching, very detailed
Structural controllability of switched linear systems with symmetry constraints
mysql automatically adds creation time and update time
初识QEMU
Three-Phase PWM Rectifier Predictive Direct Power Control
apisix-Getting Started
Quickly learn database management
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
PAT exam summary (exam experience)
After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
Use jOOQ to write vendor-agnostic SQL with JPA's native query or @Formula.
JS列表数据通过递归实现树形结构
Power BI----几个常用的分析方法和相适应的视觉对象
深度学习基本概念
第十二章 使用中的 OpenAPI 属性
busybox之reboot命令流程分析
Distributed id solution
使用 Excel 读取 SAP ABAP CDS View 通过 ODBC 暴露出来的数据
ESP8266-Arduino编程实例-MCP9808数字温度传感器驱动
一周精彩内容分享(第14期)