屏幕按钮CanExecute()方法 [英] Screen button CanExecute() method

查看:305
本文介绍了屏幕按钮CanExecute()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问我的屏幕实体的一个属性。但它在加载事件之前被调用。结果是实体在CanExecute()和Loaded()中为null。如果我评论访问实体的代码,那么一切都执行得很好。

I want to access one property of my screen entity. But it's called before the loaded event. The result is that the entity is null on the CanExecute() and in the Loaded(). If I comment the code that access the entity, then everything is executed just fine.

我尝试了一种解决方法,在加载的方法中设置一个标志,然后只设置CanExecute()如果标志为真,则访问该实体,但CanExecute只触发一次。

I've tried one workaround, by setting a flag in the loaded method and then the CanExecute() only access the entity if the flag is true, but the CanExecute triggers only one time.

我在这里遗漏了什么吗?我做错了什么?

Am I missing something here? What am I doing wrong?

推荐答案

而不是使用标志来确定CanExecute何时应该读取实体,而是进行空检查。

Instead of using a flag to determine when the CanExecute should read the entity, do a null check.

运行时对LightSwitch提供给您的对象执行依赖性检查,然后允许一旦依赖项发生更改,运行时将再次调用您的代码。 因此,在第一次调用CanExecute时,运行时确定方法尝试访问屏幕属性的
,然后返回该方法。 一个屏幕属性改变,该方法将由运行时再次执行,因为它知道方法的依赖性已经改变。

The runtime performs dependency checking on objects that are provided to you by LightSwitch, which then allows the runtime to call your code again once one of the dependencies changes.  So, on the first time it calls CanExecute, the runtime determines the method tried to access the screen property and then the method returned.  One the screen property changes, the method will be executed again by the runtime because it knows that a dependency of the method has changed.

你会想要写下面的东西(假设有)屏幕方法的CanExecute方法中的一个名为Customer的屏幕属性:

You will want to write something like the following (assuming there's a screen property called Customer) in the CanExecute method of the screen method:


if (null == this.Customer)
  return false;

// Perform rest of CanExecute check


这篇关于屏幕按钮CanExecute()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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