当前位置:网站首页>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);
}
边栏推荐
- Hybrid brain-computer interface system based on steady-state visual evoked potentials and attentional EEG
- Shengxin Weekly Issue 38
- 学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
- 瑞吉外卖项目:文件的上传与下载
- 深度学习基本概念
- In PLC communication error or timeout or download the prompt solution of the model
- 普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
- 安装MYSQL遇到问题:write configuration file卡主
- 「R」使用ggpolar绘制生存关联网络图
- 数据持久化技术——MP
猜你喜欢
After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
Power BI----几个常用的分析方法和相适应的视觉对象
MySQL index usage and optimization
Experience innovation and iteration through the development of lucky draw mini-programs
MySql模糊查询大全
一周精彩内容分享(第14期)
Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
apisix-Getting Started
使用docker搭建mysql主从
随机推荐
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
PAT考试总结(考试心得)
R语言做面板panelvar例子
IDEA configure method annotation automatic parameters
0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
[Shader] Shader official example [easy to understand]
【Shader】Shader官方示例[通俗易懂]
Initial JDBC programming
关于IDEA开发工具的介绍
DCM middleware family welcomes a new member
Full GC (Ergonomics)排查分析
B/S架构模式的一个整体执行流程
给你一个大厂面试的机会,你能面试上吗?进来看看!
dosbox基础使用[通俗易懂]
Docker搭建Mysql主从复制
三六零与公安部三所发布报告:关基设施保护成为网络安全博弈关键
A Week of Wonderful Content Sharing (Issue 14)
Chrome开发自定义右键菜单实现快速跳转到指定页面
使用 Excel 读取 SAP ABAP CDS View 通过 ODBC 暴露出来的数据
Use Excel to read data exposed by SAP ABAP CDS View through ODBC