C# PropertyChangedCallback在WinRT中OnApplyTemplate之前调用
我正在创建一个WinRT CustomControl,该控件具有PropertyChangedCallback的依赖属性。在该回调方法中,我尝试在使用GetTemplateChild方法从OnApplyMethod检索的控件的某些部分上设置值 问题是PropertyChangedCallback在OnApplyTemplate之前被调用,因此控件部分仍然为null 我发现的一个解决方法是在自定义控件的加载事件中调用此DP。那样的话,一切对我来说都很好。但每种情况都不适用。假设有人想通过xaml绑定这些值,那么问题再次出现C# PropertyChangedCallback在WinRT中OnApplyTemplate之前调用,c#,windows-8,windows-runtime,propertychanged,C#,Windows 8,Windows Runtime,Propertychanged,我正在创建一个WinRT CustomControl,该控件具有PropertyChangedCallback的依赖属性。在该回调方法中,我尝试在使用GetTemplateChild方法从OnApplyMethod检索的控件的某些部分上设置值 问题是PropertyChangedCallback在OnApplyTemplate之前被调用,因此控件部分仍然为null 我发现的一个解决方法是在自定义控件的加载事件中调用此DP。那样的话,一切对我来说都很好。但每种情况都不适用。假设有人想通过xaml绑
是否有人对此问题有任何永久性的解决办法。当我想做您所描述的事情时,以下是我遵循的一般模式:
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(...)
}
希望伪代码有帮助 当我想做你所描述的事情时,以下是我遵循的一般模式:
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(...)
}
希望伪代码有帮助 这会发生什么情况?在应用模板之前何时触发回调方法?在XAML中设置属性值时,它们会立即触发回调。这通常发生在XAML调用OnApplyTemplate之前。会发生什么情况?在应用模板之前何时触发回调方法?在XAML中设置属性值时,它们会立即触发回调。这通常发生在XAML调用OnApplyTemplate之前。