[XAML] [UWP]在加载期间使用绑定避免NullReferenceException [英] [XAML][UWP]Avoiding NullReferenceException With Binding During Load

查看:76
本文介绍了[XAML] [UWP]在加载期间使用绑定避免NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UWP应用程序,它使用Binding来根据其他元素的值确定几个元素的可见性。但是,在应用程序的初始加载期间,我收到NullReferenceException,因为我绑定的元素尚未完成
加载。在进行绑定之前有没有办法等到加载完成(没有大量的代码隐藏,我想在XAML中进行绑定)?谢谢。



Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

解决方案

< blockquote>


通常,由于双向绑定,我们不需要在绑定成功后考虑数据更改。但是成功建立绑定通常需要绑定到现有对象。对于NullReferenceException,你提到了
,我认为有两种方法。


首先,在收到或创建所有数据后完成绑定。


其次,设置一个默认值,如数据源的占位符,然后在收到或创建实际数据后通过双向绑定更改UI。


致以最诚挚的问候,


Roy


I have a UWP app that uses Binding to determine the Visibility of several elements based on values of other elements. However, during the initial loading of the app, I receive a NullReferenceException because the element I am binding to has not finished loading. Is there any way to wait until loading is completed before doing the binding (without a large amount of codebehind, I want to do the binding in XAML)? Thanks.


Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

解决方案

Hi,

In general, because of the two-way binding, we don't need to consider the data changes after the binding is successful. But the successful establishment of a binding generally requires binding to an existing object. For the NullReferenceException you mentioned about, I think there are two ways.

First, complete the binding after all the data is received or created.

Second, set a default value like placeholder for the data source , then change the UI through two-way binding after the real data is received or created.

Best regards,

Roy


这篇关于[XAML] [UWP]在加载期间使用绑定避免NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆