小技巧: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

 

 

 

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

 

你可能感兴趣的:(System)