avalonia实现自定义小弹窗

对于使用avalonia的时候某些功能需要到一些提示,比如异常或者成功都需要对用户进行提示,所以需要单独实现弹窗功能,并且可以自定义内部组件,这一期将手动实现一个简单的小弹窗,并且很容易自定义

创建项目

实现我们需要创建一个avaloniaMVVM的项目模板

2eab4de342f1ec98912a189143482fe4.png

并且取名PopoverExample

20fa352e4f98dc649477851bf03fc005.png

然后一直默认创建。

创建弹窗组件

Views文件夹中创建一个组件,选择Window模板,创建名称Dialog

f31a172113f58196bd926295b0acf9ec.png

然后打开Dialog.axaml文件,修改相关代码,

<Window 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="Dialog.Views.DialogBase"
        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaChromeHints="NoChrome"
        ExtendClientAreaTitleBarHeightHint="-1"
        Title="DialogBase">
    <StackPanel>
        <Grid>
            <Grid HorizontalAlignment="Left">
                <TextBlock>标题</TextBlock>
            </Grid>
            <Grid HorizontalAlignment="Right">
                <Button Click="Close_OnClick" Name="Close">关闭</Button>
            </Grid>
        </Grid>
        <Grid>
            <TextBlock Name="Content"></TextBlock>
        </Grid>
    </StackPanel>
</Window>

以下代码是用于隐藏默认的标题栏的

ExtendClientAreaToDecorationsHint="True" 
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaTitleBarHeightHint="-1"

打开DialogBase.axaml.cs ,修改修改代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace Dialog.Views;

public partial class DialogBase : Window
{
    public DialogBase()
    {
        InitializeComponent();
#if DEBUG
        this.AttachDevTools();
#endif
    }

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

    private void Close_OnClick(object? sender, RoutedEventArgs e)
    {
        Close();
    }
}

创建DialogManage类

创建DialogManage类,用于管理Dialog 创建DialogManage.cs,添加以下代码

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Threading;

namespace Dialog.Views;

public static class DialogManage
{
    private static readonly Dictionary<DialogType, DialogBase> _dialogBases = new();

    public static void Show(DialogType type, string content, int height = 100, int width = 200, int timing = 3000)
    {
        DialogBase dialog;
        // 防止并发可自行修改
        lock (_dialogBases)
        {
            if (_dialogBases.Remove(type, out var dialogBase))
            {
                try
                {
                    dialogBase.Close();
                }
                catch
                {
                }
            }

            dialog = new DialogBase
            {
                Height = height,
                Width = width,
                WindowStartupLocation = WindowStartupLocation.Manual // 不设置的话无法修改窗口位置
            };

            if (timing > 0)
            {
                // 弹窗定时关闭
                _ = Task.Run(async () =>
                {
                    await Task.Delay(timing);
                    // 先删除并且拿到删除的value
                    if (_dialogBases.Remove(type, out var dialogBase))
                    {
                        // 操作组件需要使用ui线程
                        _ = Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            try
                            {
                                // 关闭弹窗组件
                                dialogBase.Close();
                            }
                            // 可能已经被关闭所以可能会出现异常
                            catch
                            {
                            }
                        });
                    }
                });
            }

            // 添加到字典中
            _dialogBases.TryAdd(type, dialog);
        }

        // 获取当前屏幕
        var bounds = dialog.Screens.ScreenFromVisual(dialog).Bounds;

        // 偏移
        int skewing = 20;
        // window的任务栏高度
        int taskbar = 50;
        int x, y;
        switch (type)
        {
            case DialogType.topLeft:
                x = skewing;
                y = skewing;
                break;
            case DialogType.topCenter:
                x = (int)((bounds.Width - dialog.Width) / 2);
                y = skewing;
                break;
            case DialogType.topRight:
                x = (int)((bounds.Width - dialog.Width) - skewing);
                y = skewing;
                break;
            case DialogType.leftLower:
                x = 20;
                y = (int)(bounds.Height - dialog.Height) - taskbar - skewing;
                break;
            case DialogType.centerLower:
                x = (int)((bounds.Width - dialog.Width) / 2);
                y = (int)(bounds.Height - dialog.Height) - taskbar - skewing;
                break;
            case DialogType.rightLower:
                x = (int)(bounds.Width - dialog.Width - skewing);
                y = (int)(bounds.Height - dialog.Height) - taskbar - skewing;
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
        }

        // 设置弹窗的位置
        dialog.Position = new PixelPoint(x, y);
        
        // 获取内容显示的组件并且将内容显示上去
        var contentBox = dialog.Find<TextBlock>("Content");
        contentBox.Text = content;
        dialog.Show();
    }
}

public enum DialogType
{
    /// <summary>
    /// 左上
    /// </summary>
    topLeft,

    /// <summary>
    /// 居中靠上
    /// </summary>
    topCenter,

    /// <summary>
    /// 右上
    /// </summary>
    topRight,

    /// <summary>
    /// 左下
    /// </summary>
    leftLower,

    /// <summary>
    /// 居中靠下
    /// </summary>
    centerLower,

    /// <summary>
    /// 右下
    /// </summary>
    rightLower
}

对于弹窗组件已经完成,

基本使用弹窗

打开MainWindow.axaml文件修改代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:Dialog.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="Dialog.Views.MainWindow"
        
        Height="400"
        Width="400"
        Icon="/Assets/avalonia-logo.ico"
        Title="Dialog">

    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>
    
    <StackPanel HorizontalAlignment="Center">
        <Button Height="40" Name="OpenDialog" Click="OpenDialog_OnClick">打开新弹窗</Button>
    </StackPanel>
</Window>

打开 MainWindow.axaml.cs修改相关代码

using Avalonia.Controls;
using Avalonia.Interactivity;

namespace Dialog.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    // 定义枚举开始的值
    private int i = 0;

    private void OpenDialog_OnClick(object? sender, RoutedEventArgs e)
    {
        // 弹窗新窗口
        DialogManage.Show((DialogType)i++, "弹窗内容:" + i);
        // 超过枚举值重新赋值
        if (i == 6)
        {
            i = 0;
        }
    }
}

执行效果

da16322a3ec14abc120f86899e7aeb6f.gif

来自token的分享

技术交流群:737776595

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!感谢您的提问。让我来为您解答。 首先,您需要创建一个名为`CustomToggleButton`的`TemplateControl`类,并为其添加一个名为`IsCheckedChanged`的`RoutedEvent`。然后,您可以在`TemplateControl`类中添加一个名为`ToggleButton`的`TemplatePart`,并在该元素的`Checked`和`Unchecked`事件中触发`IsCheckedChanged`事件。下面是示例代码: ```csharp public class CustomToggleButton : TemplateControl { public static readonly RoutedEvent IsCheckedChangedEvent = EventManager.RegisterRoutedEvent( "IsCheckedChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<bool>), typeof(CustomToggleButton)); public event RoutedPropertyChangedEventHandler<bool> IsCheckedChanged { add { AddHandler(IsCheckedChangedEvent, value); } remove { RemoveHandler(IsCheckedChangedEvent, value); } } static CustomToggleButton() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomToggleButton), new FrameworkPropertyMetadata(typeof(CustomToggleButton))); } public override void OnApplyTemplate() { var toggleButton = GetTemplateChild("ToggleButton") as System.Windows.Controls.Primitives.ToggleButton; if (toggleButton != null) { toggleButton.Checked += ToggleButton_Checked; toggleButton.Unchecked += ToggleButton_Unchecked; } } private void ToggleButton_Checked(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedPropertyChangedEventArgs<bool>(false, true, IsCheckedChangedEvent)); } private void ToggleButton_Unchecked(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedPropertyChangedEventArgs<bool>(true, false, IsCheckedChangedEvent)); } } ``` 此代码中,我们定义了名为`IsCheckedChanged`的`RoutedEvent`,并在`CustomToggleButton`类的模板中添加了一个名为`ToggleButton`的`TemplatePart`。在`OnApplyTemplate`方法中,我们从模板中获取了`ToggleButton`,并为其`Checked`和`Unchecked`事件添加了事件处理程序。在这些事件处理程序中,我们生成一个新的`RoutedPropertyChangedEventArgs<bool>`对象,并在其中提供新的`IsChecked`值和旧的`IsChecked`值。然后,我们通过调用`RaiseEvent`方法触发`IsCheckedChanged`事件。 希望这可以帮助您。如果您还有任何问题,请随时问我!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值