开发者

Can a c#.net class be automatically initialized by databinding to one of the properties of the class?

I am following a tutorial on http://msdn.microsoft.com/en-us/wp7trainingcourse_usingbingmapslab_topic3#_Toc271039355.

Source code can be found here: http://az12722.vo.msecnd.net/wp7trainingcourse1-4/labs/usingbingmapslab1-1-0/Source.zip. Select the second project called "Ex2-HandlingPushpins"

This tutorial shows how to place pins on a Bing map by long-pressing a location on the map. In this tutorial there is a class PushpinCatalog, containing a collection of pushpins (Items). Items is bound to a ListBox control like this:

<Popup x:Name="PushpinPopup" IsOpen="False" Canvas.Top="330" Canvas.Left="45" Opacity="0">
            <ListBox x:Name="ListBoxPushpinCatalog"
                     Width="392" Height="56"
                     Background="{StaticResource ControlBackgroundBrush}"
                     ItemsSource="{Binding Items}"
                     SelectionChanged="ListBoxPushpinCatalog_SelectionChanged">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Width="56" Height="56" Source="{Binding Icon}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.DataContext>
                    <models:PushpinCatalog />
                </ListBox.DataContext>
            </ListBox>
        </Popup>

And the Items collection is initialized in the constructor of the PushpinCatalog. My question is, when does the constructor of the PushpinCatalog get called? I tried right-clicking it and selecting "Find all references" in visual studio, but there are none. If I debug, the call stack shows this:

>   UsingBingMaps.dll!UsingBingMaps.Models.PushpinCatalog.PushpinCatalog() Line 44 + 0x6 bytes  C#
    mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo rtci, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark) 
    mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x114 bytes    
    mscorlib.dll!System.Reflection.ConstructorInfo.Invoke(object[] parameters) + 0xa bytes  
    System.Windows.dll!MS.Internal.TypeProxy.GetCreateObjectDelegate.AnonymousMethod__2a() + 0xb bytes  
    System.Windows.dll!MS.Internal.TypeProxy.CreateInstance(uint customTypeId) + 0x12 bytes 
    System.Windows.dll!MS.Internal.FrameworkCallbacks.CreateKnownObject(System.IntPtr nativeRootPeer, uint customTypeId, string initializationString, out System.IntPtr nativePeer, uint isCreatedByParser) + 0x7a bytes    
    [External Code] 
    System.Windows.dll!MS.Internal.XcpImports.Application_LoadComponentNative(System.IntPtr pContext, System.IntPtr pComponent, uint cUriStringLength, string uriString, uint cXamlStrLength, byte* pXamlStr, uint cAssemblyStrLength, string assemblyStr)  
    System.Windows.dll!MS.Internal.XcpImports.Application_LoadComponent(MS.Internal.IManagedPeerBase componentAsDO, string resourceLocator, System.IO.UnmanagedMemoryStream stream, uint numBytesToRead, string assemblyString) + 0x39 bytes    
    System.Windows.dll!System.Windows.Application.LoadComponent(object component, System.Uri resourceLocator) + 0x136 bytes 
    UsingBingMaps.dll!UsingBingMaps.MainPage.InitializeComponent() Line 90 + 0x11 bytes C#
    UsingBingMaps.dll!UsingBingMaps.MainPage.MainPage() Line 51 + 0x6 bytes C#
    mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo rtci, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark) 
    mscorlib.dll!System.Reflection.RuntimeCon开发者_高级运维structorInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x114 bytes    
    mscorlib.dll!System.Activator.InternalCreateInstance(System.Type type, bool nonPublic, ref System.Threading.StackCrawlMark stackMark) + 0xf0 bytes  
    mscorlib.dll!System.Activator.CreateInstance(System.Type type) + 0x2 bytes  
    Microsoft.Phone.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(System.AsyncCallback userCallback, System.Windows.Navigation.PageResourceContentLoader.PageResourceContentLoaderAsyncResult result) + 0xe6 bytes   
    Microsoft.Phone.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad.AnonymousMethod__0(object args) + 0x11 bytes  
    mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
    mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
    mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
    mscorlib.dll!System.Delegate.DynamicInvokeOne(object[] args) + 0x98 bytes   
    mscorlib.dll!System.MulticastDelegate.DynamicInvokeImpl(object[] args) + 0x8 bytes  
    mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0x2 bytes   
    System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0xc bytes    
    System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0x83 bytes  
    System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0x8 bytes 
    System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x19 bytes 
    System.Windows.dll!System.Windows.Hosting.DelegateWrapper.InternalInvoke(object[] args) + 0x2 bytes 
    System.Windows.RuntimeHost.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0x5e bytes 
    [External Code] 

I suspect that through databinding of one of the properties of the class, the containing class is somehow automatically initialized, can anyone explain?


The PushpinCatalog class was created during XAML processing.

<ListBox.DataContext>
    <models:PushpinCatalog /> - PushpinCatalog instance creation
 </ListBox.DataContext>

After it the DataContext property of ListBox has a reference to you PushpinCatalog model instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜