Why is my shared list populated in XAML empty at runtime?

All,

I have a generic list defined in a user control.

private List<string> m_AnimationNames = new List<string>();

public List<string> AnimationNames
        {
            get { return this.m_AnimationNames; }
            set { this.m_AnimationNames = value; }
        }

I refer to this list in xaml and populate it like this.

<local:AnimatedCharacter.AnimationNames>
        <System:String>Walk</System:String>
        <System:String>Run</System:String>
        <System:String>Talk</System:String>
</local:AnimatedCharacter.AnimationNames>

Then I try to reference this list elsewhere in the code, after calling InitializeComponent (), and the list always returns size 0 and contains no elements.

Why is this list empty at runtime? What am I missing, what does this list count 0 do when I access it in code?

Full class:

public partial class AnimatedCharacter : UserControl
    {

        private List<string> m_AnimationNames = new List<string>();



        public AnimatedCharacter()
        {
            InitializeComponent();                        
            DoSomething();
        }


        public List<string> AnimationNames
        {
            get { return this.m_AnimationNames; }
            set { this.m_AnimationNames = value; }
        }


        public void DoSomething(){
            Console.WriteLine("Anim: " + AnimationNames.Count);   
        }
    }
}

XAML example:

<local:AnimatedCharacter x:Name="ac_guy1" Height="315" Width="273" Canvas.Left="-666" Canvas.Top="-99" >            
            <local:AnimatedCharacter.AnimationNames>
                <System:String>Walk</System:String>
                <System:String>Run</System:String>
                <System:String>Talk</System:String>
            </local:AnimatedCharacter.AnimationNames>

</local:AnimatedCharacter>
+3
source share
2 answers

DoSomething ( Loaded). , AnimationNames XAML:

public AnimatedCharacter() 
{ 
    InitializeComponent();                         

    this.Loaded += new RoutedEventHandler(OnLoaded);
} 

private void OnLoaded(object sender, RoutedEventArgs e)
{
     this.DoSomething();
}
+4

, , .

Xaml Xaml: -

 <local:AnimatedCharacter

AnimatedCharacter, , , , InitialiseComponent . , , Xaml .

Loaded , Xaml , . , List, Loaded .

+4

Source: https://habr.com/ru/post/1733067/


All Articles