WPF 通知 [英] WPF Notifications

查看:32
本文介绍了WPF 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为我的 WPF 应用程序创建通知,例如在浏览器上通过浏览器顶部的工具栏"显示消息或通过在底部向上/向下滑动的弹出窗口显示MSN"样式通知的通知屏幕右侧.也许在应用程序中心淡入/淡出的面板可以

How can I create notifications for my WPF apps, like those on browsers where they show messages via a "toolbar" at the top of the browser or a "MSN" style notification via a popup that slides up/down on the bottom right of the screen. Maybe a panel that fades in/out at the center of the app will do to

推荐答案

你的问题有点含糊,因为有了 WPF,你在这里的选择真的只受你的想象力的限制.

Your question is a little vague, in that with WPF, your options here are really only limited by your imagination.

这里有一些选项:

消息框
这是最简单的选项 - 如果您想用一条简单的消息通知您的用户他必须确认才能继续,那么只需在 MessageBox.

滚动您自己的对话框
如果 MessageBox 没有做到这一点,并且您想显示更多或不同类型的信息,那么您可以简单地创建一个新窗口,并使用 ShowDialog() 方法,强制用户在继续之前关闭它(确认).

Roll Your Own Dialog
If MessageBox doesn't quite do it, and you want to show more or different kinds of information, then you can simply create a new Window, and open it with the ShowDialog() method, forcing the user to close it (acknowledge it) before proceeding.

状态栏
如果你只是想传达信息,你可以添加一个StatusBar 到对话框的底部.我已经链接到来自 SO'er Kent Boogaart 的一个很好的例子.请注意,您不仅限于 StatusBar 中的文本 - 您可以向其中添加任何 UIElement,因此您可以拥有图像、进度条等.

StatusBar
If you simply want to convey information, you can just add a StatusBar to the bottom of your dialog. I've linked to a nice example from fellow SO'er Kent Boogaart. Note that you aren't limited to just text in a StatusBar - you can add any UIElement to it, so you could have images, progressbars, whatever.

其他一些面板
您还可以有另一个面板(使用您的示例,一个 StackPanel 或应用程序顶部的其他东西),除非需要,否则将 Visibility 设置为 Collapsed.例如,您还可以有一个包含一些内容的边框,它显示在对话框中其余 UIElements 的前面.您可以使用 PopUp 控件.

如果您走额外面板"路线(这可能听起来最符合您的要求),那么用动画做一些技巧来为您的应用程序添加一点闪光可能会很好.诸如将面板滑动到位,或为不透明度设置动画等.如果您将信息放在窗口的其余内容上,您还可以使用不透明度使面板半透明 - 足够暗以供查看和阅读,但也允许用户看到它后面的窗口的一点点.

If you go the "extra panel" route (which perhaps sounds most in line with what you are asking), then it may be nice to do some tricks with animations to add just a little flash to your app. Stuff like sliding the panel into place, or animating the opacity, etc. If you are putting the information over the rest of your window content, you can also play with the Opacity to make the panel semi-transparent - dark enough to see and read, but also allowing the user to see a little bit of the window behind it.

这是我的意思的一个非常基本的例子.我将把它留作练习,让用户添加任何格式、流畅的动画、处理多条消息等.

Here's a very basic example of what I mean. I'll leave it as an exercise for the user to add any formatting, slick animations, handle multiple messages, etc.

<Window ...>
    <Grid x:Name="gridMainLayout">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel x:Name="stackNotificationArea"
                    Grid.Row="0"
                    Orientation="Horizontal"
                    Background="LemonChiffon"
                    Visibility="Collapsed">

            <TextBlock x:Name="txtMessage"
                       Text="{Binding NotificationMessage}" />
            <Button x:Name="btnAcknowledge"
                    Content="Acknowledge" />
        </StackPanel>

        <!-- Rest of your window goes here -->
        <Grid x:Name="gridContent"
              Grid.Row="1">

              <!-- Content of window -->

        </Grid>

</Window>

在上面的例子中,我假设有一个名为 NotificationMessage 的属性返回最新的通知消息.您可以用文本或其他方式对此进行硬编码.根据是否有任何通知,最好也绑定 StackPanel 的可见性.在任何情况下,您都必须根据需要将 StackPanel 切换为 Visible.如您所述,将其设置为 Visible 将自动将窗口内容向下移动.

In the above example, I assume that there is a property called NotificationMessage that returns the latest notification message. You could hard-code this in text, or whatever. It would probably be best to bind the Visibility of the StackPanel as well, based on if there were any notifications. In any case, you will have to toggle the StackPanel to Visible as needed. Setting it to Visible will automatically move the content of your window down, as you described.

确保在确认消息时将可见性设置为折叠.如果您将其设置为 Hidden,则不会显示 StackPanel,但仍会为其保留不动产(即,您的应用程序顶部会有一个空白区域).

Be sure to set Visibility to Collapsed when the message is acknowledged. If you set it to Hidden, the StackPanel will not be shown, but the real estate will still be held for it (i.e. there will be a blank space at the top of your application).

当然,你可以随心所欲——你可以有一个包含所有消息的小列表框,或者几个按钮来滚动消息,或者一个按钮来启动一个包含所有消息的窗口,或者...

Of course, you can be as fancy as you need to be here - you could have a small listbox with all the messages, or a couple of buttons to scroll through messages, or a button to launch a window with all messages, or...

这篇关于WPF 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆