前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Avalonia使用默认弹窗

Avalonia使用默认弹窗

原创
作者头像
tokengo
发布2023-03-01 10:22:32
7810
发布2023-03-01 10:22:32
举报
文章被收录于专栏:Avalonia

Avalonia使用默认弹窗

Avalonia中使用官方默认弹窗WindowNotificationManager

Views\MainWindow.axaml相关代码

代码语言:javascript
复制
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ApplicationDemo.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="ApplicationDemo.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="ApplicationDemo">

    <Design.DataContext>
        <vm:MainWindowViewModel />
    </Design.DataContext>

    <Button Click="Button_OnClick">
        卧槽你别点我
    </Button>
</Window>

Views\MainWindow.axaml.cs相关代码

代码语言:javascript
复制
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;

namespace ApplicationDemo.Views;

public partial class MainWindow : Window
{
    private WindowNotificationManager? _manager;

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
    {
        base.OnApplyTemplate(e);
        _manager = new WindowNotificationManager(this) { MaxItems = 3 };
    }

    private void Button_OnClick(object? sender, RoutedEventArgs e)
    {
        _manager?.Show(new Notification("错误", "账号或密码错误!", NotificationType.Error));
    }
}

效果如图

在组件中使用WindowNotificationManager

修改Views\MainWindow.axaml相关代码

代码语言:javascript
复制
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ApplicationDemo.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:ApplicationDemo.Views"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="ApplicationDemo.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="ApplicationDemo">

    <Design.DataContext>
        <vm:MainWindowViewModel />
    </Design.DataContext>
    <StackPanel>

        <views:Demo></views:Demo>
        <Button Click="Button_OnClick">
            卧槽你别点我
        </Button>
    </StackPanel>
</Window>

新增Demo的UserControl组件,修改Demo.axaml代码

代码语言:javascript
复制
<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="ApplicationDemo.Views.Demo">
 <Button Click="Button_OnClick">组件点击按钮</Button>
</UserControl>

修改Demo.axaml.cs代码

代码语言:javascript
复制
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace ApplicationDemo.Views;

public partial class Demo : UserControl
{
    
    private WindowNotificationManager? _manager;
    
    public Demo()
    {
        InitializeComponent();
    }
    
    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        var topLevel = TopLevel.GetTopLevel(this);
        _manager = new WindowNotificationManager(topLevel){ MaxItems = 3};
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }

    private void Button_OnClick(object? sender, RoutedEventArgs e)
    {
        _manager?.Show(new Notification("错误", "账号或密码错误!", NotificationType.Error));
    }
}

效果如图

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Avalonia使用默认弹窗
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档