zoukankan      html  css  js  c++  java
  • 小技巧:SystemTray中进行操作提示

    SystemTray中进行操作提示在wp中应用比较广泛,截图如下。

    实现方法也十分简单

    1、xaml代码中写入:

    shell:SystemTray.IsVisible="True"

    shell:SystemTray.Opacity="0"

    2、C#代码中写入:

    private ProgressIndicator _progressIndicator = new ProgressIndicator();
    
    private void ShowProgress(String message)
            {
                _progressIndicator.Text = message;
                _progressIndicator.IsVisible = true;
                _progressIndicator.IsIndeterminate = true;
                SystemTray.SetProgressIndicator(this, _progressIndicator);
            }
    
    private void HideProgress()
            {
                _progressIndicator.IsVisible = false;
                SystemTray.SetProgressIndicator(this, _progressIndicator);
            }

    在需要显示提示文字的地方调用ShowProgress(String message)方法。隐藏的地方调用HideProgress方法即可。

    由于SystemTray.SetProgressIndicator(this, _progressIndicator);中this必须是  页面 的实例对象,为此在usercontrol控件想掉用此方法就必须找到usercontrol所在的页面。

    写一个TreeExtensions的静态类用于获取页面

    public static class TreeExtensions
        {
            public static IEnumerable<T> Ancestors<T>(this DependencyObject item)
            {
                return item.Ancestors().Where(i => i is T).Cast<T>();
            }
    
            /// <summary>
            /// 返回可视树中所有父代元素集合(不包括本身)
            /// </summary>
            public static IEnumerable<DependencyObject> Ancestors(this DependencyObject item)
            {
                var parent = item.ParentEx();
                while (parent != null)
                {
                    yield return parent;
                    parent = parent.ParentEx();
                }
            }
            /// <returns></returns>
            public static DependencyObject ParentEx(this DependencyObject item)
            {
                return VisualTreeHelper.GetParent(item);
            }
        }
    View Code

    获取页面代码:

    var phonePage = this.Ancestors<PhoneApplicationPage>().FirstOrDefault();

    同时using TreeExtensions类所在的命名空间。

    把this 替换成phonePage即可。

    可在任何地方调用的showtip代码段

    当前页面通过PhoneApplicationPage page = (Application.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage;获取

    public void showtip(string tip) 
            {
                PhoneApplicationPage page = (Application.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage;
                if (_progressIndicator == null)
                {
                    _progressIndicator = new ProgressIndicator();
                }
                _progressIndicator.Text = tip;
                _progressIndicator.IsVisible = true;
                _progressIndicator.IsIndeterminate = true;
                SystemTray.SetProgressIndicator(page, _progressIndicator);
            }
            private ProgressIndicator _progressIndicator;
    
    
    
            public void HideProgress()
            {
                if (_progressIndicator != null)
                {
                    PhoneApplicationPage page = (Application.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage;
                    _progressIndicator.IsVisible = false;
                    SystemTray.SetProgressIndicator(page, _progressIndicator);
                }
            }
    View Code

    示例:https://files.cnblogs.com/fatlin/TestShowTip.rar

  • 相关阅读:
    商城----项目宏观(1)
    Java动态代理-JDK自带实现
    git提交出现remote rejected master -> XX changes closed
    openstack认证实践
    转一篇Git代码回滚技巧
    线段树解决leetcode307. Range Sum Query
    The Skyline Problem leetcode 详解
    编程容易犯错的几个地方
    c++中小项堆声明和使用
    C++中int转为char 以及int 转为string和string 转int和字符串的split
  • 原文地址:https://www.cnblogs.com/fatlin/p/3560265.html
Copyright © 2011-2022 走看看