Windows Phone 实用开发技巧(27):创建透明Tile

简介:

 I have talked about how to create custom Live Tile in 《Windows Phone 实用开发技巧(17):自定义应用程序的Tile》. Today let’s dig a little bit deeper.

What do you see in this image shown below ?

image

Do you see the same tile with different background called accent color in windows phone . So how can we create such tile . One simplest way is we replace ApplicationIcon.png with a transparent png file. But as we know it is just main tile of our app. Can we create own tile since we can pin what we want to start ?

Yes, we can do that. All we need to do is to create a transparent png file and save it to Isolated.

Maybe you have already know, we can save jpg file to Iso with following code:

WriteableBitmap bit = new WriteableBitmap();
bit.SaveJpeg(stream, 480, 800, 0, 100);

or

Extensions.SaveJpeg(WriteableBitmap, stream, 480, 800, 0, 100);

But jpg can not be transparent. So we need some extra library to help us create transparent png images. Here I use the famous open souce ImageTools.

We can use two differernt ways to dynamic create images. One way I have post here.

//add encoder for png image Encoders.AddEncoder<PngEncoder>();

StandardTileData std = new StandardTileData {
    BackgroundImage = new Uri(CreateBackground()),
    Title = "Tile Test",
    BackTitle = "Secondary",
    BackBackgroundImage = new Uri(CreateBackground())
};
ShellTile.Create(new Uri("/MainPage.xaml?type=1", UriKind.Relative), std);
sw.Stop();
Debug.WriteLine("Tranditonal method took time :" + sw.ElapsedMilliseconds);

Here is CreateBackground method looks like:

public static string CreateBackground()
{
    Grid grid = new Grid     {
        Background = new ImageBrush         {
            ImageSource = new BitmapImage             {
                UriSource = new Uri("/mangTile;component/Images/1.png", UriKind.Relative),
                CreateOptions = BitmapCreateOptions.IgnoreImageCache
            }
        },
        Width = 173,
        Height = 173
    };
    TextBlock tb = new TextBlock     {
        Text = "Hello world",
        Foreground = new SolidColorBrush(Colors.Red),
        FontSize = 32,
    };
    grid.Children.Add(tb);
    grid.Arrange(new Rect(0d, 0d, 173, 173));
    WriteableBitmap wbmp = new WriteableBitmap(grid, null);

    ExtendedImage extendImage = wbmp.ToImage();

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!store.DirectoryExists(tiledirectory))
        {
            store.CreateDirectory(tiledirectory);
        }
        using (var stream = store.OpenFile(fullPath, System.IO.FileMode.OpenOrCreate))
        {
            extendImage.WriteToStream(stream, fullPath);
        }
    }
    return "isostore:/" + fullPath;
}
And as we run it we can see what shown below:

image

Another way is just render it within ImageOpened event:

public static void CreateTile(Uri imageUri, string temperature, string timeOfDay)
{
    var source = new BitmapImage(imageUri)
        {
            CreateOptions = BitmapCreateOptions.IgnoreImageCache,
        };
    
    string fullPath = tiledirectory + @"/" + timeOfDay + ".png";

    // This is important. The image can't be rendered before it's loaded.     source.ImageOpened += (sender, e) =>
    {
        // Create our image as a control, so it can be rendered to the WriteableBitmap.         var cloudImage = new Image { Source = source, Width = 173, Height = 173 };

        // TextBlock for the time of the day.         TextBlock tbTemperature = new TextBlock         {
            Text = temperature + '°',
            FontSize = 36,
            Foreground = new SolidColorBrush(Colors.White),
            FontFamily = new FontFamily("Segoe WP"),
        };

        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!store.DirectoryExists(tiledirectory))
            {
                store.CreateDirectory(tiledirectory);
            }

            var bitmap = new WriteableBitmap(173, 173);

            // Render our background. Remember the renders are in the same order as XAML,             // so whatever is rendered first, is rendered behind the next element.              // Render our cloud image             bitmap.Render(cloudImage, new TranslateTransform());

            // Render the temperature text.             bitmap.Render(tbTemperature, new TranslateTransform()
            {
                X = 124,
                Y = 63
            });
            bitmap.Invalidate();
            ExtendedImage extendImage = bitmap.ToImage();
            using (var stream = store.OpenFile(fullPath, System.IO.FileMode.OpenOrCreate))
            {
                extendImage.WriteToStream(stream, fullPath);
            }
            StandardTileData std = new StandardTileData             {
                BackgroundImage = new Uri("isostore:/" + fullPath),
                Title = "Tile Test22",
                BackTitle = "Secondary",
                BackBackgroundImage = new Uri("isostore:/" + fullPath)
            };
            ShellTile.Create(new Uri("/MainPage.xaml?type=2", UriKind.Relative), std);
        }

    };

}

I have a test on it(Conculsion is prefer to use second way as it’s faster and more stable! )

Tranditonal took 239 ms, Image size 6.11kb 
Render took 10 ms, Image size 5.24kb.

You can find demo source code here. Hope that helps, 微笑.





     本文转自xshf12345 51CTO博客,原文链接:http://blog.51cto.com/alexis/706532,如需转载请自行联系原作者


相关文章
|
IDE 关系型数据库 开发工具
使用Visual Basic进行Windows窗体开发
【4月更文挑战第27天】本文介绍了使用Visual Basic进行Windows窗体(WinForms)开发的步骤,从搭建开发环境到创建、设计用户界面,再到编写事件驱动的代码和数据绑定。Visual Basic结合WinForms提供了一种易学易用的桌面应用开发方案。通过调试、优化、部署和维护,开发者可以构建专业应用程序。随着技术发展,掌握最新UI设计和开发工具对于保持竞争力至关重要。本文为初学者提供了基础指导,鼓励进一步探索和学习。
530 0
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
1984 1
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
527 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
585 0
|
存储 安全 程序员
Windows任务管理器开发原理与实现
Windows任务管理器开发原理与实现
|
算法 Linux Windows
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
在Windows环境下为FFmpeg集成字幕渲染库libass涉及多个步骤,包括安装freetype、libxml2、gperf、fontconfig、fribidi、harfbuzz和libass。每个库的安装都需要下载源码、配置、编译和安装,并更新PKG_CONFIG_PATH环境变量。最后,重新配置并编译FFmpeg以启用libass及相关依赖。完成上述步骤后,通过`ffmpeg -version`确认libass已成功集成。
672 1
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
|
编解码 Linux Windows
FFmpeg开发笔记(十一)Windows环境给FFmpeg集成vorbis和amr
在Windows环境下,为FFmpeg集成音频编解码库,包括libogg、libvorbis和opencore-amr,涉及下载源码、配置、编译和安装步骤。首先,安装libogg,通过配置、make和make install命令完成,并更新PKG_CONFIG_PATH。接着,安装libvorbis,同样配置、编译和安装,并修改pkgconfig文件。之后,安装opencore-amr。最后,重新配置并编译FFmpeg,启用ogg和amr支持,通过ffmpeg -version检查是否成功。整个过程需确保环境变量设置正确,并根据路径添加相应库。
487 1
FFmpeg开发笔记(十一)Windows环境给FFmpeg集成vorbis和amr
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
884 0
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
1693 0
|
Linux 编译器 C语言
FFmpeg开发笔记(二)搭建Windows系统的开发环境
在Windows上学习FFmpeg通常较困难,但通过安装预编译的FFmpeg开发包可以简化流程。首先需要安装MSYS2来模拟Linux环境。下载并执行MSYS2安装包,然后修改msys2_shell.cmd以继承Windows的Path变量。使用pacman安装必要的编译工具。接着,下载预编译的FFmpeg Windows包,解压并配置系统Path。最后,在MSYS2环境中运行`ffmpeg -version`确认安装成功。欲深入学习FFmpeg开发,推荐阅读《FFmpeg开发实战:从零基础到短视频上线》。
522 4
FFmpeg开发笔记(二)搭建Windows系统的开发环境