Static properties not initialized on LoadComponent()?
Posted: 08 Apr 2017, 05:38
I have the following XAML (simplified and emphasied for readability):
XAML:
<UserControl x:Class="FantasiaPhone.BrushAndColorControl"
...
xmlns:t="clr-namespace:Translation">
...
<TextBlock Text="all colors" FontSize="14" VerticalAlignment="Bottom" HorizontalAlignment="Center" TextWrapping="Wrap" TextAlignment="Center"
t:Translate.Text="allColors" />
...
</UserControl>
That I load like so:
Load code:
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load(Path.GetFileNameWithoutExtension(xamlPath));
xaml.LoadComponent(o);
Debug.Log("Loaded");
I also have the Translate class:
C# t:Translate code:
public class Translate : DependencyObject {
...
public static DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text", typeof(string), typeof(Translate), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged)));
static void OnTextChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
Debug.Log("TEXT CHANGED");
}
...
I can load the XAML fine, display it fine, and OnTextChanged is called OK. However, I expect to see that the text changed event is called before "loaded":
Expected debug log:
TEXT CHANGED
Loaded
Actual debug log:
Loaded
TEXT CHANGED
After I call LoadComponent() I immediately go through the list of controls and update their text with translation. Since this is already in place I don't want to use bindings (they are also slower.)
Why is the above order of operations happening? How can I reliably determine that all controls have their t:Translate="..." handlers executed?
XAML:
<UserControl x:Class="FantasiaPhone.BrushAndColorControl"
...
xmlns:t="clr-namespace:Translation">
...
<TextBlock Text="all colors" FontSize="14" VerticalAlignment="Bottom" HorizontalAlignment="Center" TextWrapping="Wrap" TextAlignment="Center"
t:Translate.Text="allColors" />
...
</UserControl>
That I load like so:
Load code:
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load(Path.GetFileNameWithoutExtension(xamlPath));
xaml.LoadComponent(o);
Debug.Log("Loaded");
I also have the Translate class:
C# t:Translate code:
public class Translate : DependencyObject {
...
public static DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text", typeof(string), typeof(Translate), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged)));
static void OnTextChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
Debug.Log("TEXT CHANGED");
}
...
I can load the XAML fine, display it fine, and OnTextChanged is called OK. However, I expect to see that the text changed event is called before "loaded":
Expected debug log:
TEXT CHANGED
Loaded
Actual debug log:
Loaded
TEXT CHANGED
After I call LoadComponent() I immediately go through the list of controls and update their text with translation. Since this is already in place I don't want to use bindings (they are also slower.)
Why is the above order of operations happening? How can I reliably determine that all controls have their t:Translate="..." handlers executed?