当前位置:网站首页>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);
}
边栏推荐
- 想吃菌子,当然是自己上山找了
- A40i/T3 uboot启动时对PMU部分初始化
- [core]-ARMV7-A, ARMV8-A, ARMV9-A Architecture Introduction "Recommended Collection"
- 多线程学习笔记-2.final关键字和不变性
- 使用docker搭建mysql主从
- 带有对称约束切换线性系统的结构可控性
- 科学论文和学术论文写作
- The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
- 无法将“node.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
- JVS应用中心
猜你喜欢

busybox之reboot命令流程分析

Full GC (Ergonomics)排查分析

一文吃透接口调用神器RestTemplate

Selenium自动化测试之Selenium IDE

WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)

5 open source Rust web development frameworks, which one do you choose?

深度学习基本概念

订song餐系统

三六零与公安部三所发布报告:关基设施保护成为网络安全博弈关键

MySQL index usage and optimization
随机推荐
A40i/T3 uboot启动时对PMU部分初始化
busybox之reboot命令流程分析
MySQL百万数据优化总结 一
kernel syscore
5 open source Rust web development frameworks, which one do you choose?
cesium-Web网页优化进阶
VBA实现双击单元格自动输出对号再次双击取消对号
Three-Phase PWM Rectifier Predictive Direct Power Control
瑞吉外卖项目:文件的上传与下载
Docker实践经验:Docker 上部署 mysql8 主从复制
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Wearing detection and action recognition of protective gear based on pose estimation
apisix-Getting Started
jmeter性能测试步骤入门(性能测试工具jmeter)
JVM 运行时数据区与JMM 内存模型详解
Docker practical experience: Deploy mysql8 master-slave replication on Docker
JVS应用中心
机器学习基本概念
Use jOOQ to write vendor-agnostic SQL with JPA's native query or @Formula.
深度学习基本概念