我想创build类似于在Windows Phone 8.1中的“敬酒通知”在Android中的东西。 我想要的是一个文本框显示,指示用户发生了一些事件(如“配对”,“连接丢失”等)。 但是文本框会在几秒钟后自动解除,而没有任何用户交互。 我已经通过消息框,并在窗口中popup。 但是两者都不符合我的需求,或者我不得不使用定时器callback来自动解除它,这是不推荐的。
任何人都可以提出一个方法来实现这个在Windows Phone 8.1中
在我看来,最好的解决方案是伊格拉里的解决方案,因为他尊重准则。
但是,如果你想显示Toast相同的Android:
像这样创建userControl:
<Border CornerRadius="50" Background="#CC000000" Height="80" Width="150" Margin="10"> <TextBlock Text="{Binding Message}" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border>
在后面的代码中:
public MyUserControl1() { this.InitializeComponent(); this.DataContext = this; } public string Message { get { return (string)GetValue(MessageProperty); } set { SetValue(MessageProperty,value); } } // Using a DependencyProperty as the backing store for Message. This enables animation,styling,binding,etc... public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message",typeof(string),typeof(MyUserControl1),new PropertyMetadata(string.Empty));
并在你的页面中使用这个userControl:
private MyUserControl1 toast; private dispatcherTimer timer = new dispatcherTimer(); private void Button_Click(object sender,RoutedEventArgs e) { toast = new MyUserControl1(); toast.Message = "Message in my toast"; toast.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom; toast.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center; timer.Interval = new TimeSpan(0,1); timer.Start(); timer.Tick += timer_Tick; layoutRoot.Children.Add(toast); } void timer_Tick(object sender,object e) { if(toast != null) layoutRoot.Children.Remove(toast); timer.Stop(); }
我创建一个按钮,并点击事件。
在这个事件中,我创建我的用户控件,并添加在网格(layoutRoot)。 我使用dispatcherTimer在一个seconde后删除用户控件。
例如,创建一个定义显示时间的枚举。 您还可以创建一个Storyboard来运行动画。
你可以添加一个静态类添加以下内容。 之后,调用showtext()从任何地方显示你的toast.Just传递布局根(父网格)和字符串显示。 您可以根据需要自定义您的外观和感觉。
public static void ShowToast(Grid layoutRoot,string message) { Grid grid = new Grid(); grid.Width = 300; grid.Height = 60; grid.Background = new Windows.UI.Xaml.Media.solidColorBrush(Colors.Transparent); grid.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center; grid.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom; grid.Margin = new Thickness(0,30); TextBlock text = new TextBlock(); text.Text = message; text.VerticalAlignment = VerticalAlignment.Center; text.HorizontalAlignment = HorizontalAlignment.Center; text.FontSize = 22; grid.Children.Add(text); layoutRoot.Children.Add(grid); dispatcherTimer timer = new dispatcherTimer(); timer.Interval = new TimeSpan(0,3); timer.Tick += (sender,args) => { layoutRoot.Children.Remove(grid); timer.Stop(); }; timer.Start(); }
So I used the Coding4fun ToastPrompt to tweak as per android. In your code add the following code: ToastPrompt toast = new ToastPrompt(); toast.Title = "Toast Title"; toast.Message = "Toast Message" toast.MillisecondsUntilHidden = 2000; //duration for toast toast.Background =new SolidColorBrush(Colors.Gray); toast.Foreground = new SolidColorBrush(Colors.White); toast.IsHitTestVisible = false; toast.Margin = new Windows.UI.Xaml.Thickness(0,100); toast.HorizontalContentAlignment = HorizontalAlignment.Center; toast.VerticalContentAlignment = VerticalAlignment.Center; toast.Stretch = Stretch.Uniform; toast.VerticalAlignment = VerticalAlignment.Bottom; toast.HorizontalAlignment = HorizontalAlignment.Center; toast.Template = Application.Current.Resources["ToastPromptStyle"] as ControlTemplate; toast.Show();
在您的Styles.xaml页面或任何您将样式拉开的位置,定义一个ControlTemplate。 我的名字是“ToastPromptStyle”。
目标吐司提示。 在您的样式页面上添加对toolkit的引用。有些事情是这样的:xmlns:toolkit =“using:Coding4Fun.Toolkit.Controls”
<ControlTemplate x:Key="ToastPromptStyle" targettype="toolkit:ToastPrompt"> <Border CornerRadius="15" Background="Gray"> <TextBlock Text="{TemplateBinding Message}" Foreground="White" FontFamily="DengXian" FontSize="20" Margin="10"/> </Border> </ControlTemplate>
正如你所看到的,我已经设置了拐角半径属性,以在类似于android中给出的边缘处给出弯曲。 希望能帮助到你。
Toast由Coding4Fun提供 。
ToastPrompt toast = new ToastPrompt(); toast.Title = "Title"; toast.Message = "Message"; toast.MillisecondsUntilHidden = 4000; //dismissed after 4 seconds toast.Completed += toast_Completed; toast.Show();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。