Windows.UI.Xaml.Core.Direct 命名空間

提供 XamlDirect API,可讓中介軟體在較基本層級存取大部分的 Xaml,以達到更佳的 CPU 和工作集效能。

類別

XamlDirect

表示所有 XamlDirect API 的基類。 所有 XamlDirect API 都是這個類別的實例方法。

XamlDirect 是一種 API,可在較基本層級存取 Xaml,以取得更佳的 CPU 和工作集效能。

適用于 UWP 的對等 WinUI 2 APIMicrosoft.UI.Xaml.Core.Direct.XamlDirect (在Windows 應用程式 SDK中,請參閱Windows 應用程式 SDK命名空間) 。

介面

IXamlDirectObject

表示參與 XamlDirect API 集合的主要物件類型。

列舉

XamlEventIndex

列出 XamlDirect中所有支援事件的列舉。

適用于 UWP 的對等 WinUI 2 APIMicrosoft.UI.Xaml.Core.Direct.XamlEventIndex (在Windows 應用程式 SDK中,請參閱Windows 應用程式 SDK命名空間) 。

XamlPropertyIndex

列出 XamlDirect中所有支援屬性的列舉。

適用于 UWP 的對等 WinUI 2 APIMicrosoft.UI.Xaml.Core.Direct.XamlPropertyIndex (Windows 應用程式 SDK,請參閱Windows 應用程式 SDK命名空間) 。

XamlTypeIndex

列出 XamlDirect中所有支援類型的列舉。

適用于 UWP 的對等 WinUI 2 APIMicrosoft.UI.Xaml.Core.Direct.XamlTypeIndex (Windows 應用程式 SDK,請參閱Windows 應用程式 SDK命名空間) 。

範例

此範例示範如何以 3 種方式使用標準 UIElements 建立物件、設定屬性和介面:在 C# 中使用一般 XAML 標記,以及使用 XamlDirect API 的新方式。

在此範例中,我們會建立 Border 元素和 Rectangle 元素,並在每個元素上設定一些屬性。 然後,我們會將它們新增至 UI 元素的樹狀結構。

  1. 使用 XAML 標記:
<Grid x:Name="RootGrid">
    <Border BorderBrush="Black" BorderThickness="5">
        <Rectangle Height="100" Width="100" Fill="Red" />
    </Border>
</Grid>
  1. 使用具有完整 XAML 類型的一般命令式程式碼:
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5);

Rectangle rectangle = new Rectangle();
rectangle.Height = 100;
rectangle.Width = 100;
SolidColorBrush rectBrush = new SolidColorBrush(Colors.Red);
rectangle.Fill = rectBrush;

border.Child = rectangle;

RootGrid.Children.Add(border);
  1. 使用 XamlDirect 程式碼:

下列程式碼的效能會高於使用完整 XAML 類型,因為透過 IXamlDirectObjects 而非完整的 XAML 類型來完成各種元素上屬性的具現化和設定等所有作業。

XamlDirect xamlDirect = XamlDirect.GetDefault();

IXamlDirectObject border = XamlDirect.CreateInstance(XamlTypeIndex.Border);
xamlDirect.SetThicknessProperty(border, XamlPropertyIndex.Border_BorderThickness, new Thickness(5));

IXamlDirectObject borderBrush = XamlDirect.CreateInstance(XamlTypeIndex.SolidColorBrush);
xamlDirect.SetColorProperty(borderBrush, XamlPropertyIndex.SolidColorBrush_Color, Colors.Black);
xamlDirect.SetXamlDirectObjectProperty(border, XamlPropertyIndex.Border_BorderBrush, borderBrush);

IXamlDirectObject rectangle = XamlDirect.CreateInstance(XamlTypeIndex.Rectangle);
xamlDirect.SetDoubleProperty(rectangle, XamlPropertyIndex.FrameworkElement_Width, 100);
xamlDirect.SetDoubleProperty(rectangle, XamlPropertyIndex.FrameworkElement_Height, 100);

IXamlDirectObject rectBrush = XamlDirect.CreateInstance(XamlTypeIndex.SolidColorBrush);
xamlDirect.SetColorProperty(rectBrush, XamlPropertyIndex.SolidColorBrush_Color, Colors.Red);
xamlDirect.SetXamlDirectObjectProperty(rectangle, XamlPropertyIndex.Shape_Fill, rectangleBrush);

xamlDirect.SetXamlDirectObjectProperty(border, XamlPropertyIndex.Border_Child, rectangle);

RootGrid.Children.Add((UIElement) XamlDirect.GetObject(border));

備註

XamlDirect專為中介軟體所建置 ,主要使用命令式 API 來建立 UI,而不是標記。 使用 XamlDirect API,即使在程式碼中以命令方式建立 UI,您還是可以達到與 XAML 剖析器的效能同位。

XamlDirect API 可以與傳統 API 並存使用,並利用播放效能改善的付費。

並非所有 Xaml API 都可搭配 XamlDirect使用。 XamlTypeIndex 列舉會列出所有支援的類型、XamlPropertyIndex 列舉會列出所有支援的屬性,而XamlEventIndex 列舉會列出所有支援的事件。

支援的函數

您可以使用 XamlDirect API 來執行下列函式:

XamlDirect.CreateInstance傳回的所有物件都是IXamlDirectObject類型。 所有其他 API,例如 Set*Property API,接受 IXamlDirectObject 作為其第一個參數。

若要將 IXamlDirectObject 轉換成其完整的 APINDEX,例如 Button,請使用 XamlDirect.GetObject 方法。 同樣地,您可以使用 XamlDirect.GetXamlDirectObject ,從完整的 Object/DependencyObject 轉換為其 XamlDirect 對等實例。

另請參閱