我是 getting/checking 类型的 UIElement 通过非拥有 UI 线程安全吗?

I am getting/checking type of UIElement through non owning UI thread is it safe?

我有一个自定义 UI 元素。我正在通过拥有线程以外的线程访问它。我能够 get/check 它的类型(自定义类型)并得到正确的结果。依赖这个结果是否安全? (我知道为了 access/update 它的价值我们必须使用拥有 UI 线程)

例如:

bool result = ((uiElement as CustomType) != null)

可以在拥有线程以外的线程中安全地检查控件的类型:

bool result = uiElement is CustomType;

如果出于任何原因(你没有在问题中提到),

  • uiElement是一个外部可访问的变量(例如字段或属性),
  • 并且 uiElement 的值可能会被另一个线程更改,
  • 并且在类型检查之后你仍然需要访问它,

将类型检查的结果赋值给局部变量会更安全:

var customElement = uiElement as CustomType;

if (customElement != null)
{
    // do something with customElement ...
}