CompositionObject 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
[WebHostHidden]
public ref class CompositionObject : IAnimationObject, IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.Foundation.LiftedContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[WebHostHidden]
class CompositionObject : IAnimationObject, IClosable
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.Foundation.WindowsAppSDKContract, 65536)]
[WebHostHidden]
class CompositionObject : IAnimationObject, IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.Foundation.LiftedContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class CompositionObject : IAnimationObject, System.IDisposable
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.Foundation.WindowsAppSDKContract), 65536)]
public class CompositionObject : IAnimationObject, System.IDisposable
Public Class CompositionObject
Implements IAnimationObject, IDisposable
- 繼承
- 衍生
- 屬性
- 實作
範例
本範例展示了如何利用 composition API 建立一個自包含的應用程式,而無需使用 XAML、WWA 或 DirectX。 範例會初始化一個新的 合成器,然後建立包含兩個 SpriteVisual 物件的場景圖。
using System;
using System.Numerics;
using Windows.ApplicationModel.Core;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Core;
namespace HelloCompositionCs
{
public sealed class HelloComposition : IFrameworkView
{
//------------------------------------------------------------------------------
//
// HelloComposition.Initialize
//
// This method is called during startup to associate the IFrameworkView with the
// CoreApplicationView.
//
//------------------------------------------------------------------------------
void IFrameworkView.Initialize(CoreApplicationView view)
{
_view = view;
}
//------------------------------------------------------------------------------
//
// HelloComposition.SetWindow
//
// This method is called when the CoreApplication has created a new CoreWindow,
// allowing the application to configure the window and start producing content
// to display.
//
//------------------------------------------------------------------------------
void IFrameworkView.SetWindow(CoreWindow window)
{
_window = window;
InitNewComposition();
}
//------------------------------------------------------------------------------
//
// HelloComposition.Load
//
// This method is called when a specific page is being loaded in the
// application. It is not used for this application.
//
//------------------------------------------------------------------------------
void IFrameworkView.Load(string unused)
{
}
//------------------------------------------------------------------------------
//
// HelloComposition.Run
//
// This method is called by CoreApplication.Run to actually run the
// dispatcher's message pump.
//
//------------------------------------------------------------------------------
void IFrameworkView.Run()
{
_window.Activate();
_window.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessUntilQuit);
}
//------------------------------------------------------------------------------
//
// HelloComposition.Uninitialize
//
// This method is called during shutdown to disconnect the CoreApplicationView,
// and CoreWindow from the IFrameworkView.
//
//------------------------------------------------------------------------------
void IFrameworkView.Uninitialize()
{
_window = null;
_view = null;
}
//------------------------------------------------------------------------------
//
// HelloComposition.InitNewComposition
//
// This method is called by SetWindow, where we initialize Composition after
// the CoreWindow has been created.
//
//------------------------------------------------------------------------------
void InitNewComposition()
{
//
// Set up Windows.UI.Composition Compositor, root ContainerVisual, and associate with
// the CoreWindow.
//
_compositor = new Compositor();
_root = _compositor.CreateContainerVisual();
_view.CompositionRootVisual = _root;
//
// Create a simple scene.
//
var child1 = _compositor.CreateSpriteVisual();
child1.Offset = new Vector2(50.0f, 50.0f);
child1.Size = new Vector2(200, 200);
child1.Brush = _compositor.CreateColorBrush(Color.FromArgb(0xFF, 0x00, 0xCC, 0x00));
_root.Children.InsertAtTop(child1);
var child2 = _compositor.CreateSpriteVisual();
child2.Offset = new Vector2(50.0f, 50.0f);
child2.Size = new Vector2(200, 200);
child2.Brush = _compositor.CreateColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0xCC));
child1.Children.InsertAtTop(child2);
}
// CoreWindow / CoreApplicationView
private CoreWindow _window;
private CoreApplicationView _view;
// Windows.UI.Composition
private Compositor _compositor;
private ContainerVisual _root;
}
public sealed class HelloCompositionFactory : IFrameworkViewSource
{
//------------------------------------------------------------------------------
//
// HelloCompositionFactory.CreateView
//
// This method is called by CoreApplication to provide a new IFrameworkView for
// a CoreWindow that is being created.
//
//------------------------------------------------------------------------------
IFrameworkView IFrameworkViewSource.CreateView()
{
return new HelloComposition();
}
//------------------------------------------------------------------------------
//
// main
//
//------------------------------------------------------------------------------
static int Main(string[] args)
{
CoreApplication.Run(new HelloCompositionFactory());
return 0;
}
}
} // namespace HelloCompositionCs
這個範例展示了如何在不使用 XAML、WWA 或 DirectX 的情況下,建構並走過簡單的視覺樹來改變不透明度。 本範例建立在前一個範例之上,展示子視覺化如何被建立、新增及屬性變更。
在此情境中,會建立一個視覺樹階層。 合成器建立後,用來建立物件,進而可管理物件。
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Numerics;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Core;
namespace VisualTreeCs
{
public sealed class VisualTree : IFrameworkView
{
//------------------------------------------------------------------------------
//
// VisualTree.Initialize
//
// This method is called during startup to associate the IFrameworkView with the
// CoreApplicationView.
//
//------------------------------------------------------------------------------
void IFrameworkView.Initialize(CoreApplicationView view)
{
_view = view;
_random = new Random();
}
//------------------------------------------------------------------------------
//
// VisualTree.SetWindow
//
// This method is called when the CoreApplication has created a new CoreWindow,
// allowing the application to configure the window and start producing content
// to display.
//
//------------------------------------------------------------------------------
void IFrameworkView.SetWindow(CoreWindow window)
{
_window = window;
InitNewComposition();
_window.PointerPressed += OnPointerPressed;
_window.PointerMoved += OnPointerMoved;
_window.PointerReleased += OnPointerReleased;
}
//------------------------------------------------------------------------------
//
// VisualTree.OnPointerPressed
//
// This method is called when the user touches the screen, taps it with a stylus
// or clicks the mouse.
//
//------------------------------------------------------------------------------
void OnPointerPressed(CoreWindow window, PointerEventArgs args)
{
Point position = args.CurrentPoint.Position;
//
// Walk our list of visuals to determine who, if anybody, was selected
//
foreach (var child in _root.Children)
{
//
// Did we hit this child?
//
Vector2 offset = child.Offset;
Vector2 size = child.Size;
if ((position.X >= offset.X) &&
(position.X < offset.X + size.X) &&
(position.Y >= offset.Y) &&
(position.Y < offset.Y + size.Y))
{
//
// This child was hit. Since the children are stored back to front,
// the last one hit is the front-most one so it wins
//
_currentVisual = child;
_offsetBias = new Vector2((float)(offset.X - position.X),
(float)(offset.Y - position.Y));
}
}
//
// If a visual was hit, bring it to the front of the Z order
//
if (_currentVisual != null)
{
ContainerVisual parent = _currentVisual.Parent as ContainerVisual;
parent.Children.Remove(_currentVisual);
parent.Children.InsertAtTop(_currentVisual);
}
}
//------------------------------------------------------------------------------
//
// VisualTree.OnPointerMoved
//
// This method is called when the user moves their finger, stylus or mouse with
// a button pressed over the screen.
//
//------------------------------------------------------------------------------
void OnPointerMoved(CoreWindow window, PointerEventArgs args)
{
//
// If a visual is selected, drag it with the pointer position and
// make it opaque while we drag it
//
if (_currentVisual != null)
{
Point position = args.CurrentPoint.Position;
_currentVisual.Opacity = 1.0f;
_currentVisual.Offset = new Vector2((float)(position.X + _offsetBias.X),
(float)(position.Y + _offsetBias.Y));
}
}
//------------------------------------------------------------------------------
//
// VisualTree.OnPointerReleased
//
// This method is called when the user lifts their finger or stylus from the
// screen, or lifts the mouse button.
//
//------------------------------------------------------------------------------
void OnPointerReleased(CoreWindow window, PointerEventArgs args)
{
//
// If a visual was selected, make it transparent again when it is
// released
//
if (_currentVisual != null)
{
_currentVisual.Opacity = 0.8f;
_currentVisual = null;
}
}
//------------------------------------------------------------------------------
//
// VisualTree.Load
//
// This method is called when a specific page is being loaded in the
// application. It is not used for this application.
//
//------------------------------------------------------------------------------
void IFrameworkView.Load(string unused)
{
}
//------------------------------------------------------------------------------
//
// VisualTree.Run
//
// This method is called by CoreApplication.Run to actually run the
// dispatcher's message pump.
//
//------------------------------------------------------------------------------
void IFrameworkView.Run()
{
_window.Activate();
_window.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessUntilQuit);
}
//------------------------------------------------------------------------------
//
// VisualTree.Uninitialize
//
// This method is called during shutdown to disconnect the CoreApplicationView,
// and CoreWindow from the IFrameworkView.
//
//------------------------------------------------------------------------------
void IFrameworkView.Uninitialize()
{
_window = null;
_view = null;
}
//------------------------------------------------------------------------------
//
// VisualTree.InitNewComposition
//
// This method is called by SetWindow, where we initialize Composition after
// the CoreWindow has been created.
//
//------------------------------------------------------------------------------
void InitNewComposition()
{
//
// Set up Windows.UI.Composition Compositor, root ContainerVisual, and associate with
// the CoreWindow.
//
_compositor = new Compositor();
_root = _compositor.CreateContainerVisual();
_view.CompositionRootVisual = _root;
//
// Create a few visuals for our window
//
for (int index = 0; index < 20; index++)
{
_root.Children.InsertAtTop(CreateChildElement());
}
}
//------------------------------------------------------------------------------
//
// VisualTree.CreateChildElement
//
// Creates a small sub-tree to represent a visible element in our application.
//
//------------------------------------------------------------------------------
Visual CreateChildElement()
{
//
// Each element consists of two visuals, which produce the appearance
// of a framed rectangle
//
var visual = _compositor.CreateSpriteVisual();
//
// Position this visual randomly within our window
//
visual.Offset = new Vector2((float)(_random.NextDouble() * 400), (float)(_random.NextDouble() * 400));
//
// The outer rectangle is always white
//
visual.Brush = _compositor.CreateColorBrush( Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF) );
visual.Size = new Vector2(100.0f, 100.0f);
//
// The inner rectangle is inset from the outer by three pixels all around
//
var child = _compositor.CreateSpriteVisual();
visual.Children.InsertAtTop(child);
child.Offset = new Vector2(3.0f, 3.0f);
child.Size = new Vector2(94.0f, 94.0f);
//
// Pick a random color for every rectangle
//
byte red = (byte)(0xFF * (0.2f + (_random.NextDouble() / 0.8f)));
byte green = (byte)(0xFF * (0.2f + (_random.NextDouble() / 0.8f)));
byte blue = (byte)(0xFF * (0.2f + (_random.NextDouble() / 0.8f)));
child.Brush = _compositor.CreateColorBrush( Color.FromArgb(0xFF, red, green, blue) );
//
// Make the subtree root visual partially transparent. This will cause each visual in the subtree
// to render partially transparent, since a visual's opacity is multiplied with its parent's
// opacity
//
visual.Opacity = 0.8f;
return visual;
}
// CoreWindow / CoreApplicationView
private CoreWindow _window;
private CoreApplicationView _view;
// Windows.UI.Composition
private Compositor _compositor;
private ContainerVisual _root;
private Visual _currentVisual;
private Vector2 _offsetBias;
// Helpers
private Random _random;
}
public sealed class VisualTreeFactory : IFrameworkViewSource
{
//------------------------------------------------------------------------------
//
// VisualTreeFactory.CreateView
//
// This method is called by CoreApplication to provide a new IFrameworkView for
// a CoreWindow that is being created.
//
//------------------------------------------------------------------------------
IFrameworkView IFrameworkViewSource.CreateView()
{
return new VisualTree();
}
//------------------------------------------------------------------------------
//
// main
//
//------------------------------------------------------------------------------
static int Main(string[] args)
{
CoreApplication.Run(new VisualTreeFactory());
return 0;
}
}
} // namespace VisualTreeCs
備註
物件是使用 Compositor 物件建立的。 組合物件可以是純容器,也可以是內容。 API 透過提供明確的視覺物件集合,方便使用,針對存在於階層結構中的特定任務:
Visual – 基礎物件,大部分屬性都在這裡,並由其他視覺物件繼承。
ContainerVisual – 源自 Visual,並新增建立子節點的功能。
SpriteVisual – 源自 ContainerVisual,包含圖片、特效與交換鏈(swapchains)等內容。
Compositor – 管理應用程式與系統 Compositor 流程之間的關係。 動畫會更新任何可動畫合成物件(例如視覺)的屬性。 動畫有兩種類型:
關鍵影格動畫:基於時間的動畫,包含兩個或以上關鍵影格。 這些影格是標記,讓開發者能在指定時間點定義動畫值。 動畫還可透過指定如何插值(混合)關鍵幀間的數值來微調。 KeyFrameAnimation 有許多子類別,每個子類別支援不同類型的關鍵影格值。
ExpressionAnimation:使用數學表達式指定每幀如何計算動畫值的動畫。 這些表達式可以參考組合物件的屬性。 ExpressionAnimations不是基於時間的,會根據每幀(如有需要)進行處理。
原始視覺
原始視覺(如 SpriteVisual)描述螢幕上的視覺內容,以及將套用在該內容上的渲染選項。
影響
效果可以附加到視覺樹上,以對來源內容(如圖片或視覺樹)進行動態像素變化。
一個效果可以是簡單的操作如去飽和,較複雜的操作如模糊,或是非常複雜的 A B 混合操作,如交叉淡入淡出。
更多關於製作與使用效果的資訊,請參閱 CompositionEffectBrush 的備註區。
屬性
| 名稱 | Description |
|---|---|
| Comment |
一個用來關聯 CompositionObject 的字串。 |
| Compositor |
用於建立此 CompositionObject 的合成器。 |
| DispatcherQueue |
取得 CompositionObject 的 DispatcherQueue。 |
| ImplicitAnimations |
附加於此物件的隱含動畫集合。 |
| Properties |
與 CompositionObject 相關聯的屬性集合。 |
方法
| 名稱 | Description |
|---|---|
| Close() |
關閉 CompositionObject 並釋放系統資源。 |
| Dispose() |
執行與釋放、釋放或重設非受控資源相關聯的應用程式定義工作。 |
| PopulatePropertyInfo(String, AnimationPropertyInfo) |
定義了一個可以被動畫化的屬性。 |
| StartAnimation(String, CompositionAnimation, AnimationController) |
將動畫與物件指定的屬性連結起來,然後開始動畫。 |
| StartAnimation(String, CompositionAnimation) |
將動畫與物件指定的屬性連結起來,然後開始動畫。 |
| StartAnimationGroup(ICompositionAnimationBase) |
成立了一個動畫小組。 CompositionObject 上的 StartAnimationGroup 方法可以讓你啟動 CompositionAnimationGroup。 群組中的所有動畫都會同時在物件上啟動。 |
| StartAnimationGroupWithIAnimationObject(IAnimationObject, ICompositionAnimationBase) |
在指定目標上啟動動畫群組。 |
| StartAnimationWithIAnimationObject(IAnimationObject, String, CompositionAnimation) |
將動畫與目標物件指定的屬性連結起來,並開始動畫。 |
| StopAnimation(String) |
斷開動畫與指定屬性的連結並停止該動畫。 |
| StopAnimationGroup(ICompositionAnimationBase) |
停止一個動畫組。 |
| TryGetAnimationController(String) |
回傳一個 AnimationController,代表在指定屬性上執行的動畫。 |