您的位置:首页 > 其它

Windows 8.1 新控件和功能:

2014-06-26 10:01 113 查看
http://msdn.microsoft.com/zh-cn/library/windows/apps/bg182878.aspx#five

将 XAML 树呈现为位图:

适用于 Windows 8.1 的 Windows 运行时为 Windows.UI.Xaml.Media.Imaging 命名空间添加了一种新类型:RenderTargetBitmap

此类型提供了两个关键方法:

RenderTargetBitmap.RenderAsync,用于提取 XAML 可视化树 并为其创建位图表示。

注意 此操作采用异步方式,将给定的 XAML 元素树呈现为位图。 此方法与屏幕刷新不同步,不能保证精确的帧计时,因此该位图可能在假定捕获时刻前后的一瞬间进行呈现。

RenderTargetBitmap.GetPixelsAsync,用于以特定格式返回像素的字节数组。

下例显示如何呈现 XAML 元素树。

var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(myElementTree);

myImage.Source = renderTargetBitmap;


RenderTargetBitmap 继承自 ImageSource,因此可以直接将其设置为 Image 对象的源,而无需调用 GetPixelsAsync 以获取及显示位图数据。

下例显示如何将呈现的位图写入文件。

var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(this.C1);
IBuffer buffer = await bitmap.GetPixelsAsync();
var pixelStream = buffer.AsStream();

FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.FileTypeChoices.Add("Bitmap", new List<string>() { ".png" });
savePicker.SuggestedFileName = "New Bitmap";

StorageFile savedItem = await savePicker.PickSaveFileAsync();

Guid encoderId = BitmapEncoder.PngEncoderId;
IRandomAccessStream fileStream = await savedItem.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, fileStream);
byte[] pixels = new byte[pixelStream.Length];
pixelStream.Read(pixels, 0, pixels.Length);

//pixal format shouldconvert to rgba8
for (int i = 0; i < pixels.Length; i += 4)
{
byte temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}

encoder.SetPixelData(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Straight,
(uint)bitmap.PixelWidth,
(uint)bitmap.PixelHeight,
96, // Horizontal DPI
96, // Vertical DPI
pixels);

await encoder.FlushAsync();


MetroApp保存UIEment为图片 /article/5233638.html 的代码 也差不多。其//pixal format shouldconvert to rgba8 下面的一段交换代码不用会变色。

A new control for XAML that lets you easily create the hub design pattern that reflects the proper design guidelines and behaviors: the Hub control.

Hub pages are the user's entry point to your app.

(Hub页是用户进入应用的入口点。)

Hub可以分区显示,通过将不同的内容放入不同的HubSection来实现。HubSection也可以设置标题,做法与Hub一致。如果HubSection的IsHeaderInteractive属性为true,那么标题默认包含 '>' 字型,以及悬停和按下状态。

void Hub_SectionHeaderClick(object sender, HubSectionHeaderClickEventArgs e)
{
HubSection section = e.Section;
var group = section.DataContext;
this.Frame.Navigate(typeof(SectionPage), ((SampleDataGroup)group).UniqueId);
}


临时显示与用户当前操作相关的 UI:Flyout 控件。

临时显示与用户当前操作相关的命令或选项列表:MenuFlyout 控件。

轻松地创建应用设置浮出控件,恰如其分地反映设计思想和行为:SettingsFlyout 控件。

Windows 8.1 将 PlaceholderText 属性添加到多个包含文本的控件中。某些控件(如 ComboBoxPasswordBox)可能需要用户输入。如果不想使用默认值或显示空控件,你可以添加占位符文本以便为用户提供上下文。

XAML 数据绑定改进:

Windows 8.1 中添加了以下 API 元素:

FrameworkElement.DataContextChanged 事件允许你响应 DataContext 属性值中的变化。你可以使用此事件而不是通过数据绑定来手动更新控件属性。这对于解决数据绑定性能问题十分有效。

Binding.FallbackValueBinding.TargetNullValue 属性可让你设置当绑定无法解析某个值或解析为 null 值时将会显示的默认值。

Binding.UpdateSourceTrigger 属性、FrameworkElement.GetBindingExpression 方法和 BindingExpression 类可让你将双向绑定的默认行为更改为 TextBox.Text 属性。默认情况下,TextBox.Text 绑定只有在控件失去焦点时才会更新其绑定源。将 UpdateSourceTrigger 设置为 PropertyChanged,以便在 TextBox.Text 属性更改值时更新源。将 UpdateSourceTrigger 设置为 Explicit,以便使用 BindingExpression.UpdateSource 方法通过编程方式更新源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: