SystemTray中进行操作提示在wp中应用比较广泛,截图如下。
实现方法也十分简单
1、xaml代码中写入:
shell:SystemTray.IsVisible="True"
shell:SystemTray.Opacity="0"
2、C#代码中写入:
private ProgressIndicator _progressIndicator = newProgressIndicator();private voidShowProgress(String message)
{
_progressIndicator.Text=message;
_progressIndicator.IsVisible= true;
_progressIndicator.IsIndeterminate= true;
SystemTray.SetProgressIndicator(this, _progressIndicator);
}private voidHideProgress()
{
_progressIndicator.IsVisible= false;
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
在需要显示提示文字的地方调用ShowProgress(String message)方法。隐藏的地方调用HideProgress方法即可。
由于SystemTray.SetProgressIndicator(this, _progressIndicator);中this必须是 页面 的实例对象,为此在usercontrol控件想掉用此方法就必须找到usercontrol所在的页面。
写一个TreeExtensions的静态类用于获取页面
public static classTreeExtensions
{public static IEnumerable Ancestors(thisDependencyObject item)
{return item.Ancestors().Where(i => i is T).Cast();
}///
///返回可视树中所有父代元素集合(不包括本身)///
public static IEnumerable Ancestors(thisDependencyObject item)
{var parent =item.ParentEx();while (parent != null)
{yield returnparent;
parent=parent.ParentEx();
}
}///
public static DependencyObject ParentEx(thisDependencyObject item)
{returnVisualTreeHelper.GetParent(item);
}
}
View Code
获取页面代码:
var phonePage = this.Ancestors().FirstOrDefault();
同时using TreeExtensions类所在的命名空间。
把this 替换成phonePage即可。
可在任何地方调用的showtip代码段
当前页面通过PhoneApplicationPage page = (Application.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage;获取
public void showtip(stringtip)
{
PhoneApplicationPage page= (Application.Current.RootVisual as PhoneApplicationFrame).Content asPhoneApplicationPage;if (_progressIndicator == null)
{
_progressIndicator= newProgressIndicator();
}
_progressIndicator.Text=tip;
_progressIndicator.IsVisible= true;
_progressIndicator.IsIndeterminate= true;
SystemTray.SetProgressIndicator(page, _progressIndicator);
}privateProgressIndicator _progressIndicator;public voidHideProgress()
{if (_progressIndicator != null)
{
PhoneApplicationPage page= (Application.Current.RootVisual as PhoneApplicationFrame).Content asPhoneApplicationPage;
_progressIndicator.IsVisible= false;
SystemTray.SetProgressIndicator(page, _progressIndicator);
}
}
View Code
示例:http://files.cnblogs.com/fatlin/TestShowTip.rar