locked
A stupid question about XAML ResourceDictionary loading mechanism

    Question

  • Hi, I've been programming Windows Store Apps for quite a long time, but still got some difficulty when trying to understand the XAML ResourceDictionary loading mechanism. For example, I've a Page with many Resources:

    <Page x:Class="Test.BlankPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test"> <Page.Resources> <DataTemplate x:Key="someDataTemplate"> <Grid> <!--......--> </Grid> </DataTemplate>

    <!--......-->

    </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> </Grid> </Page>

    The problem is that this page will be put on a Popup control, and created each time the Popup shows, so it is important to know if there will be extra performance hit to put so many stuff in its Resources. In other words, will the Page loads/creates its Resources each time it is created?

    Or will the Xaml loading system only loads Resources once on first access and then caches them for reuse in the whole lifetime of application?

    It would be a huge performance hit if it would load/create the Resources everytime the FrameWorkElment is created.

    Thanks!



    Wednesday, February 25, 2015 9:14 AM

Answers

  • >>will the Page loads/creates its Resources each time it is created?

    Yes, the resources in <Page.Resources> will be created each time an instance of the Page is created.

    If you create an instance of the Page frequently you should consider moving the resources to App.xaml. Then they will only be created once when the application starts up.

    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

     

    • Marked as answer by silverbird2015 Wednesday, February 25, 2015 10:03 AM
    Wednesday, February 25, 2015 9:36 AM
  • As I said, whenever the page is recreated all of its elements and resources are also recreated.

    There is no caching of a control that has not yet been created and there is no caching of a control that has been disposed since this would effectively lead to memory leaks.

    If you create instances of the Page frequently and the resources are expensive to create, then you should move the resources out of the page to for example App.xaml as already mentioned.

    Please remember to close your threads by marking all helpful posts as answer.

     



    Wednesday, February 25, 2015 9:52 AM

All replies

  • >>will the Page loads/creates its Resources each time it is created?

    Yes, the resources in <Page.Resources> will be created each time an instance of the Page is created.

    If you create an instance of the Page frequently you should consider moving the resources to App.xaml. Then they will only be created once when the application starts up.

    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

     

    • Marked as answer by silverbird2015 Wednesday, February 25, 2015 10:03 AM
    Wednesday, February 25, 2015 9:36 AM
  • >>will the Page loads/creates its Resources each time it is created?

    Yes, the resources in <Page.Resources> will be created each time an instance of the Page is created.

    If you create an instance of the Page frequently you should consider moving the resources to App.xaml. Then they will only be created once when the application starts up.

    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

     

    Thanks! A futher question: does the Xaml parsing system need to load the Xaml text in the Resources node and parse them each time when the Resources is created?  Or it just load/parse the Xaml text once and then cache it for reuse later? If so, it would reduce the performance hit hugely, which I believe and hope so.

    Wednesday, February 25, 2015 9:49 AM
  • As I said, whenever the page is recreated all of its elements and resources are also recreated.

    There is no caching of a control that has not yet been created and there is no caching of a control that has been disposed since this would effectively lead to memory leaks.

    If you create instances of the Page frequently and the resources are expensive to create, then you should move the resources out of the page to for example App.xaml as already mentioned.

    Please remember to close your threads by marking all helpful posts as answer.

     



    Wednesday, February 25, 2015 9:52 AM