x:DeferLoadStrategy 屬性x:DeferLoadStrategy attribute


從 Windows 10,版本 1703 (Creators Update) 開始,x:DeferLoadStrategy 已由 x:Load 屬性 取代。Starting in Windows 10, version 1703 (Creators Update), x:DeferLoadStrategy is superseded by the x:Load attribute. 使用 x:Load="False" 與使用 x:DeferLoadStrategy="Lazy" 具有相同的效果,但在需要的時候可以提供您取消載入 UI 的能力。Using x:Load="False" is equivalent to x:DeferLoadStrategy="Lazy", but provides the ability to unload the UI if required. 請參閱 x:Load 屬性See the x:Load attribute for more info.

您可以使用 x:DeferLoadStrategy="Lazy" 來為您的 XAML 應用程式最佳化啟動或建立樹狀結構的效能。You can use x:DeferLoadStrategy="Lazy" to optimize the startup or tree creation performance of your XAML app. 當您使用 x:DeferLoadStrategy="Lazy" 時,元素及其子元素的建立將會延後,進而達到減少啟動時間和記憶體消耗量的效果。When you use x:DeferLoadStrategy="Lazy", creation of an element and its children is delayed, which decreases startup time and memory costs. 這在降低不常顯示或只在特定條件下需要顯示之元素的成本時很有用。This is useful to reduce the costs of elements that are shown infrequently or conditionally. 當從程式碼或 VisualStateManager 參考元素時,元素將會具現化。The element will be realized when it's referred to from code or VisualStateManager.

然而,XAML 架構將會針對每個受影響的元素增加大約 600 位元組的記憶體使用量,作為追蹤延遲元素使用。However, the tracking of deferred elements by the XAML framework adds about 600 bytes to the memory usage for each element affected. 延遲的元素樹狀結構愈大,節省的啟動時間就愈多,但代價是記憶體使用量較大。The larger the element tree you defer, the more startup time you'll save, but at the cost of a greater memory footprint. 因此,過度使用此屬性可能導致效能降低。Therefore, it's possible to overuse this attribute to the extent that your performance decreases.

XAML 屬性用法XAML attribute usage

<object x:DeferLoadStrategy="Lazy" .../>


x:DeferLoadStrategy 的使用限制如下:The restrictions for using x:DeferLoadStrategy are:

有幾種不同的方式可用來辨識延遲的元素:There are several different ways to realize the deferred elements:

  • 使用您在元素上定義的名稱呼叫 FindNameCall FindName with the name that you defined on the element.
  • 使用您在元素上定義的名稱呼叫 GetTemplateChildCall GetTemplateChild with the name that you defined on the element.
  • VisualState 中,使用以延遲的元素為目標的 SetterStoryboard 動畫。In a VisualState, use a Setter or Storyboard animation that targets the deferred element.
  • 在任何一個 Storyboard 中以延遲的元素做為目標。Target the deferred element in any Storyboard.
  • 使用以延遲的元素為目標的繫結。Use a binding that targets the deferred element.

注意:元素在開始具現化後,就會建立在 UI 執行緒上,因此如果同時建立太多,就可能造成 UI 間斷。NOTE: Once the instantiation of an element has started, it is created on the UI thread, so it could cause the UI to stutter if too much is created at once.

以任何前述方法建立延遲的元素後,將會發生幾件事:Once a deferred element is created in any of the ways listed previously, several things happen:

  • 引發元素的 Loaded 事件。The Loaded event on the element is raised.
  • 元素的任何繫結會受到評估。Any bindings on the element are evaluated.
  • 如果您已登錄而會接收包含延遲元素之屬性的屬性變更通知,通知將會引發。If you have registered to receive property change notifications on the property containing the deferred element(s), the notification is raised.

您可以內嵌延遲的元素,但必須從最外層元素開始辨識這些元素。You can nest deferred elements, however they have to be realized from the outer-most element in. 如果您嘗試在辨識父元素之前辨識子元素,將會引發例外狀況。 If you try to realize a child element before the parent has been realized, an exception is raised.

一般而言,我們建議您延後不會出現在第一個畫面中的元素。Typically, we recommend that you defer elements that are not viewable in the first frame.在尋找要延遲的候選項目時,建議您尋找以摺疊的 Visibility 建立的元素。 A good guideline for finding candidates to be deferred is to look for elements that are being created with collapsed Visibility. 此外,使用者互動所觸發的 UI 也是您尋找延遲元素的理想之處。Also, UI that is triggered by user interaction is a good place to look for elements that you can defer.

請留意 ListView 中的延遲元素,因為它雖然可減少您的啟動時間,但也可能隨著您所建立的項目而導致移動瀏覽效能下降。Be wary of deferring elements in a ListView, as it will decrease your startup time, but could also decrease your panning performance depending on what you're creating. 如果您想要提升移動瀏覽效能,請參閱 {x:Bind} 標記延伸x:Phase 屬性文件。If you are looking to increase panning performance, see the {x:Bind} markup extension and x:Phase attribute documentation.

如果 x:Phase 屬性結合 x:DeferLoadStrategy 使用,則當元素或元素樹狀結構實現之後,繫結會向上套用且包含目前階段。If the x:Phase attribute is used in conjunction with x:DeferLoadStrategy then, when an element or an element tree is realized, the bindings are applied up to and including the current phase. 針對 x:Phase 指定的階段不會影響或控制元素的延遲。The phase specified for x:Phase does not affect or control the deferral of the element. 當清單項目被視為移動瀏覽的一部分回收時,實現的元素會與其他作用中元素的行為相同,而已編譯的繫結 ({x:Bind} 繫結) 會使用相同的規則處理 (包括階段處理)。When a list item is recycled as part of panning, realized elements behave in the same way as other active elements, and compiled bindings ({x:Bind} bindings) are processed using the same rules, including phasing.

一般會建議您在之前或之後測量您應用程式的執行效能,以確保您得到想要的效能。A general guideline is to measure the performance of your app before and after to make sure you are getting the performance that you want.


<Grid x:Name="DeferredGrid" x:DeferLoadStrategy="Lazy">
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />

    <Rectangle Height="100" Width="100" Fill="#F65314" Margin="0,0,4,4" />
    <Rectangle Height="100" Width="100" Fill="#7CBB00" Grid.Column="1" Margin="4,0,0,4" />
    <Rectangle Height="100" Width="100" Fill="#00A1F1" Grid.Row="1" Margin="0,4,4,0" />
    <Rectangle Height="100" Width="100" Fill="#FFBB00" Grid.Row="1" Grid.Column="1" Margin="4,4,0,0" />
<Button x:Name="RealizeElements" Content="Realize Elements" Click="RealizeElements_Click"/>
private void RealizeElements_Click(object sender, RoutedEventArgs e)
    // This will realize the deferred grid.