当前位置:网站首页>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);
}
边栏推荐
- file contains vulnerabilities
- ESP8266-Arduino编程实例-MCP9808数字温度传感器驱动
- Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
- Hybrid brain-computer interface system based on steady-state visual evoked potentials and attentional EEG
- Comparison of ipv4 and ipv6 (IPV4)
- Use jOOQ to write vendor-agnostic SQL with JPA's native query or @Formula.
- 数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
- JS列表数据通过递归实现树形结构
- 一周精彩内容分享(第14期)
- How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
猜你喜欢

无法将“node.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。

JVS轻应用的组成与配置

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?

Redis学习笔记-3.慢查询和其他高级数据结构

想吃菌子,当然是自己上山找了

Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性

R语言做面板panelvar例子

A Week of Wonderful Content Sharing (Issue 14)
502 bad gateway causes and solutions
随机推荐
JS列表数据通过递归实现树形结构
音视频基础
St. Regis Takeaway Project: File Upload and Download
最近两个月谷歌 ad 掉的厉害
am335x 看门狗驱动&看门狗应用例程序
In PLC communication error or timeout or download the prompt solution of the model
Caused by: 类找不到: org.apache.flink.table.planner.delegation.ParserFactory或者ExecutorFactory
5 open source Rust web development frameworks, which one do you choose?
Acwing第 62 场周赛【未完结】
第十二章 使用中的 OpenAPI 属性
How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
Json和对象之间转换的封装(Gson)
CWE4.8 -- 2022年危害最大的25种软件安全问题
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
[core]-ARMV7-A, ARMV8-A, ARMV9-A Architecture Introduction "Recommended Collection"
Selenium自动化测试之Selenium IDE
Redis学习笔记-3.慢查询和其他高级数据结构
Standard SQL/JSON - the sobering part
文件包含漏洞
关于IDEA开发工具的介绍