gpt4 book ai didi

c# - PropertyChangedCallback 在 WinRT 中先于 OnApplyTemplate 调用

转载 作者:行者123 更新时间:2023-11-30 20:51:50 25 4
gpt4 key购买 nike

我正在创建一个 WinRT CustomControl,它具有 PropertyChangedCallback 的依赖属性。在该回调方法中,我尝试为我使用 GetTemplateChild() 方法从 OnApplyMethod 检索的某些控件部分设置值。

问题是在 OnApplyTemplate 之前调用了 PropertyChangedCallback,因此控制部分仍然为空。

我发现的一种解决方法是在我的自定义控件的加载事件中调用此 DP。在那种情况下,一切对我来说都很好。但不是每一种情况都适用。假设如果有人想通过 xaml 绑定(bind)值,问题会再次出现。

有人对此问题有任何永久性解决方法吗。

最佳答案

当我想要执行您所描述的操作时,这是我遵循的一般模式:

private void OnFooChanged(...)
{
if (someNamedPart != null && someOtherNamedPart != null && ...)
{
// Do something to the named parts that are impacted by Foo
// when Foo changes.
}
}

private void FooChangedCallback(...)
{
// Called by WinRT when Foo changes
OnFooChanged(...)
}

protected override void OnApplyTemplate(...)
{
// Theoretically, this can get called multiple times - every time the
// consumer of this custom control changes the template for this control.
// If the control has named parts which must react to the properties
// this control exposes, all that work must be done here EVERY TIME
// a new template is applied.

// Get and save named parts as local variables first

OnFooChanged(...)
}

希望伪代码对您有所帮助!

关于c# - PropertyChangedCallback 在 WinRT 中先于 OnApplyTemplate 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21346116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com