IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Windows Phone Runtime 8.1 如何显示系统状态栏(以前的SystemTray)

    汪宇杰发表于 2014-11-22 12:31:48
    love 0

    首先。。。虽然我文章写的2,但是国内愿意主动分享WP开发经验的博客太少了,大家都是粉,为了这个平台不容易,所以我有点什么都会拿出来分享,觉得文章菜的请不要喷。。。

    开发WP8.1的小伙伴们如果选择了WinRT的runtime,可能会被坑:如果你的手机主题是黑色的,但是你的应用选择了白色主题,就像这样

    RequestedTheme="Light"

    那你会发现,虽然你在写UI配色方面偷懒了许多,但是WP的系统状态栏(显示信号、时间的地方),也就是Silverlight Runtime里称作SystemTray的,配色并不会因为你选择了“Light”而变成黑色这种对比色,它始终是跟着手机系统设置走的(黑色主题则为白色,白色主题则为黑色),于是你的应用就会出现一条白色的状态栏在手机顶端,用户再也不能愉快的看时间了。草草草草草。

    当你想修这个问题的时候,你又会发现微软已经把Silverlight时代在xaml里能直接声明的SystemTray给撸掉了,草草草草草。所以我们需要手工去写代码。

    这个东西现在叫做StatusBar,需要引用这个命名空间

    using Windows.UI.ViewManagement;
    

    使用起来最直接的代码如下,你可以放在首页构造函数最后,这样打开页面就直接有了,并且这个状态栏不会随着页面导航消失,所以随后的页面都不需要重复去做。

    StatusBar statusBar = StatusBar.GetForCurrentView();
    statusBar.BackgroundColor = Colors.CadetBlue; // 背景色
    statusBar.ForegroundColor = Colors.White; // 前景色
    statusBar.BackgroundOpacity = 1; // 透明度
    statusBar.ProgressIndicator.Text = "FontAwesome Offline Reference (v4.2.0)"; // 文本
    statusBar.ProgressIndicator.ShowAsync();

    有两个要注意的地方。

    1. ShowAsync是个Task async方法,所以是可以被await的,看需要的时候使用

    2. 你会发现这样做出来的状态栏上面会有···在动,草草草草:

    解决方法是设置进度值为0:

    statusBar.ProgressIndicator.ProgressValue = 0;

    最终你可以封成一个助手方法,以便日后使用:

    public static async Task ShowSystemTrayAsync(Color backgroundColor, Color foregroundColor,
        double opacity = 1, string text = "", bool isIndeterminate = false)
    {
        StatusBar statusBar = StatusBar.GetForCurrentView();
        statusBar.BackgroundColor = backgroundColor;
        statusBar.ForegroundColor = foregroundColor;
        statusBar.BackgroundOpacity = opacity;
    
        statusBar.ProgressIndicator.Text = text;
        if (!isIndeterminate)
        {
            statusBar.ProgressIndicator.ProgressValue = 0;
        }
        await statusBar.ProgressIndicator.ShowAsync();
    }

    现在,在首页构造函数最下面你就可以:

    Snippet

    UIHelper.ShowSystemTrayAsync(Colors.CadetBlue, Colors.White, text: "FontAwesome Offline Reference (v4.2.0)");
    

    因为构造函数里不能await,所以不要在意这些细节,最终结果是可以用的。



沪ICP备19023445号-2号
友情链接