CompositionObject 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
[WebHostHidden]
public ref class CompositionObject : IClosable
public ref class CompositionObject : IClosable
public ref class CompositionObject : IClosable, IAnimationObject
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[WebHostHidden]
class CompositionObject : IClosable
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 131072)]
class CompositionObject : IClosable
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 131072)]
class CompositionObject : IClosable, IAnimationObject
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class CompositionObject : 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(Windows.Foundation.UniversalApiContract), 131072)]
public class CompositionObject : 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(Windows.Foundation.UniversalApiContract), 131072)]
public class CompositionObject : System.IDisposable, IAnimationObject
Public Class CompositionObject
Implements IDisposable
Public Class CompositionObject
Implements IAnimationObject, IDisposable
- 继承
- 派生
- 属性
- 实现
Windows 要求
设备系列 |
Windows 10 (在 10.0.10240.0 中引入)
|
API contract |
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
|
示例
此示例演示如何使用合成 API 创建独立应用,而无需使用 XAML、WWA 或 DirectX。 该示例初始化一个新的 Compositor,然后创建包含两个 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 的情况下构造和演练简单的视觉对象树以更改不透明度。 此示例基于上一个示例生成,演示如何创建、添加子视觉对象以及更改属性。
在此方案中,将生成视觉对象的可视化树层次结构。 创建 Compositor 后,它将用于创建对象,然后可以对其进行管理。
// 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 对象:
Visual – 基对象,大部分属性均位于此处且继承自其他视觉对象。
ContainerVisual - 派生自 Visual,并添加了创建子级的功能。
SpriteVisual – 派生自 ContainerVisual,包含图像、效果和交换链形式的内容。
Compositor – 管理应用程序与系统合成器进程之间的关系。 动画更新任何可创建动画的合成对象 (的属性,例如 Visual) 。 有两种类型的动画:
KeyFrameAnimation:具有两个或多个关键帧的基于时间的动画。 这些帧是标记,使开发人员能够定义在指定时间应包含的动画值。 此外,还可以通过指定动画如何内插 () 关键帧之间的值进行混合来微调动画。 KeyFrameAnimation 有许多子类,每个子类都支持不同类型的关键帧值。
ExpressionAnimation:使用数学表达式指定每个帧应如何计算动画值的动画。 表达式可以引用组合对象中的属性。 ExpressionAnimation不是基于时间的,如有必要, (处理每个帧) 。
基元视觉对象
基元视觉对象 ((如 SpriteVisual) 描述屏幕上的视觉内容)以及将应用于该内容的呈现选项。
效果
效果可以附加到可视化树,以便对源内容(如图像或视觉对象树)进行动态像素更改。
效果可以是简单的操作(如去饱和度)、更复杂的操作(如模糊)或非常复杂的 A B 混合操作(如交叉淡出)。
有关创建和使用效果的其他信息,请参阅 CompositionEffectBrush 的备注部分。
版本历史记录
Windows 版本 | SDK 版本 | 增值 |
---|---|---|
1607 | 14393 | 注释 |
1607 | 14393 | ImplicitAnimations |
1607 | 14393 | StartAnimationGroup |
1607 | 14393 | StopAnimationGroup |
1709 | 16299 | DispatcherQueue |
1803 | 17134 | TryGetAnimationController |
1809 | 17763 | PopulatePropertyInfo |
1809 | 17763 | StartAnimationGroupWithIAnimationObject |
1809 | 17763 | StartAnimationWithIAnimationObject |
属性
Comment |
要与 CompositionObject 关联的字符串。 |
Compositor | |
Dispatcher |
CompositionObject 的调度程序。 |
DispatcherQueue |
获取 CompostionObject 的 DispatcherQueue 。 |
ImplicitAnimations |
附加到此对象的隐式动画集合。 |
Properties |
与 CompositionObject 关联的属性的集合。 |
方法
Close() |
关闭 CompositionObject 并释放系统资源。 |
ConnectAnimation(String, CompositionAnimation) |
连接和动画。 |
DisconnectAnimation(String) |
断开动画的连接。 |
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。 |