对于使用 avalonia 的时候某些功能需要到一些提示,比如异常或者成功都需要对用户进行提示,所以需要单独实现弹窗功能,并且可以自定义内部组件,这一期将手动实现一个简单的小弹窗,并且很容易自定义
创建项目
实现我们需要创建一个 avalonia MVVM的项目模板
并且取名 PopoverExample
然后一直默认创建。
创建弹窗组件
在 Views 文件夹中创建一个组件,选择 Window模板 ,创建名称 Dialog
然后打开 Dialog.axaml 文件,修改相关代码,
< Windowxmlns= "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>
< GridHorizontalAlignment= "Left">
< TextBlock> 标题 </ TextBlock>
</ Grid>
< GridHorizontalAlignment= "Right">
< ButtonClick= "Close_OnClick"Name= "Close"> 关闭 </ Button>
</ Grid>
</ Grid>
< Grid>
< TextBlockName= "Content"> </ TextBlock>
</ Grid>
</ StackPanel>
</ Window>
以下代码是用于隐藏默认的标题栏的
ExtendClientAreaToDecorationsHint= "True"
ExtendClientAreaChromeHints= "NoChrome"
ExtendClientAreaTitleBarHeightHint= "-1"
打开 DialogBase.axaml.cs ,修改修改代码
usingAvalonia;
usingAvalonia.Controls;
usingAvalonia.Interactivity;
usingAvalonia.Markup.Xaml;
namespaceDialog.Views;
publicpartialclassDialogBase: Window
{
publicDialogBase( )
{
InitializeComponent;
# ifDEBUG
this.AttachDevTools;
# endif
}
privatevoidInitializeComponent( )
{
AvaloniaXamlLoader.Load( this);
}
privatevoidClose_OnClick( object? sender, RoutedEventArgs e )
{
Close;
}
}
创建 Dialog Manage类
创建 Dialog Manage类,用于管理 Dialog 创建 DialogManage.cs ,添加以下代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Threading.Tasks;
usingAvalonia;
usingAvalonia.Controls;
usingAvalonia.Threading;
namespaceDialog.Views;
publicstaticclassDialogManage
{
privatestaticreadonlyDictionary<DialogType, DialogBase> _dialogBases = new;
publicstaticvoidShow( DialogType type, stringcontent, intheight = 100, intwidth = 200, inttiming = 3000)
{
DialogBase dialog;
// 防止并发可自行修改
lock(_dialogBases)
{
if(_dialogBases.Remove(type, outvardialogBase))
{
try
{
dialogBase.Close;
}
catch
{
}
}
dialog = newDialogBase
{
Height = height,
Width = width,
WindowStartupLocation = WindowStartupLocation.Manual // 不设置的话无法修改窗口位置
};
if(timing > 0)
{
// 弹窗定时关闭
_ = Task.Run( async=>
{
awaitTask.Delay(timing);
// 先删除并且拿到删除的value
if(_dialogBases.Remove(type, outvardialogBase))
{
// 操作组件需要使用ui线程
_ = Dispatcher.UIThread.InvokeAsync( =>
{
try
{
// 关闭弹窗组件
dialogBase.Close;
}
// 可能已经被关闭所以可能会出现异常
catch
{
}
});
}
});
}
// 添加到字典中
_dialogBases.TryAdd(type, dialog);
}
// 获取当前屏幕
varbounds = dialog.Screens.ScreenFromVisual(dialog).Bounds;
// 偏移
intskewing = 20;
// window的任务栏高度
inttaskbar = 50;
intx, y;
switch(type)
{
caseDialogType.topLeft:
x = skewing;
y = skewing;
break;
caseDialogType.topCenter:
x = ( int)((bounds.Width - dialog.Width) / 2);
y = skewing;
break;
caseDialogType.topRight:
x = ( int)((bounds.Width - dialog.Width) - skewing);
y = skewing;
break;
caseDialogType.leftLower:
x = 20;
y = ( int)(bounds.Height - dialog.Height) - taskbar - skewing;
break;
caseDialogType.centerLower:
x = ( int)((bounds.Width - dialog.Width) / 2);
y = ( int)(bounds.Height - dialog.Height) - taskbar - skewing;
break;
caseDialogType.rightLower:
x = ( int)(bounds.Width - dialog.Width - skewing);
y = ( int)(bounds.Height - dialog.Height) - taskbar - skewing;
break;
default:
thrownewArgumentOutOfRangeException( nameof(type), type, null);
}
// 设置弹窗的位置
dialog.Position = newPixelPoint(x, y);
// 获取内容显示的组件并且将内容显示上去
varcontentBox = dialog.Find<TextBlock>( "Content");
contentBox.Text = content;
dialog.Show;
}
}
publicenumDialogType
{
///<summary>
///左上
///</summary>
topLeft,
///<summary>
///居中靠上
///</summary>
topCenter,
///<summary>
///右上
///</summary>
topRight,
///<summary>
///左下
///</summary>
leftLower,
///<summary>
///居中靠下
///</summary>
centerLower,
///<summary>
///右下
///</summary>
rightLower
}
对于弹窗组件已经完成,
基本使用弹窗
打开 MainWindow.axaml 文件修改代码
< Windowxmlns= "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>
< StackPanelHorizontalAlignment= "Center">
< ButtonHeight= "40"Name= "OpenDialog"Click= "OpenDialog_OnClick"> 打开新弹窗 </ Button>
</ StackPanel>
</ Window>
打开 MainWindow.axaml.cs 修改相关代码
usingAvalonia.Controls;
usingAvalonia.Interactivity;
namespaceDialog.Views;
publicpartialclassMainWindow: Window
{
publicMainWindow( )
{
InitializeComponent;
}
// 定义枚举开始的值
privateinti = 0;
privatevoidOpenDialog_OnClick( object? sender, RoutedEventArgs e )
{
// 弹窗新窗口
DialogManage.Show((DialogType)i++, "弹窗内容:"+ i);
// 超过枚举值重新赋值
if(i == 6)
{
i = 0;
}
}
}
执行效果
来自token的分享
2、本站永久网址:https://www.yuanmacun.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
源码村资源网 » 如何知道织梦模板中button的链接(织梦网站怎么添加关键词)
1 评论