Avaloniaui 消息弹框 ---vm中调用主线程

v

private WindowNotificationManager? _manager;
public MainWindow()
{
    InitializeComponent();
    var vm = new MainWindowViewModel();
    vm.message += ShowMessage;
    this.DataContext = vm;
}

private void ShowMessage(object sender, System.EventArgs e)
{
    var message = (MyMessage)sender;
    _manager?.Show(new Notification(message.MessageTitle, message.Message, message.MessageType));
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
    base.OnApplyTemplate(e);
    _manager = new WindowNotificationManager(this) { MaxItems = 3 };
}
WindowNotificationManager ---弹窗属性
  _manager?.Show(new Notification(message.MessageTitle, message.Message, message.MessageType));-- 显示弹窗 
   vm.message += ShowMessage; --->给message 委托 赋值方法

vm

public ReactiveCommand<Unit, Unit> SubmitCommand { get; }
public MainWindowViewModel()
{
    SubmitCommand = ReactiveCommand.Create(SubmitAction);
}
private void SubmitAction()
{
    MyMessage  messageData= new MyMessage { Message = "初始化成功",MessageType = NotificationType.Information,MessageTitle="提示!"};
     message.Invoke(messageData, null);//调用消息弹窗;


}

消息体

public class MyMessage
{
  public string MessageTitle { get; set; }
  public string  Message { get; set; }

 public NotificationType MessageType { get; set; }
}

效果

在这里插入图片描述

元素隐藏

v

	<Grid Margin="10">
		<Grid.RowDefinitions>
			<RowDefinition Height="1*"/>
			<RowDefinition Height="1*"/>
			<RowDefinition Height="1*"/>
		</Grid.RowDefinitions>
		<DockPanel LastChildFill="True" Grid.Row="0" >
			<Border Height="25" IsVisible="{Binding Visible}" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
				<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
			</Border>
			<Border Height="25" IsVisible="{Binding Visible}" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
				<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
			</Border>
			<Border Height="25" Background="LemonChiffon" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Bottom">
				<TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
			</Border>
			<Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Left">
				<TextBlock Foreground="Black">Dock = "Left"</TextBlock>
			</Border>
			<Border Background="White" BorderBrush="Black" BorderThickness="1">
				<TextBlock Foreground="Black">This content will "Fill" the remaining space</TextBlock>
			</Border>
		</DockPanel>
		<Button Grid.Row="1" Command="{Binding SubmitCommand}">点击出消息弹窗</Button>
		<Button Grid.Row="2" Command="{Binding SubmitCommand}" >元素</Button>
		
</Grid>

vm

 private bool _visible = true;
 public bool Visible
 {
     get => _visible;
     set => this.RaiseAndSetIfChanged(ref _visible, value);
 } 
 private void SubmitAction()
 {
     MyMessage  messageData= new MyMessage { Message = "初始化成功",MessageType = NotificationType.Information,MessageTitle="提示!"};
      message.Invoke(messageData, null);
     Visible = !Visible;
 }

效果在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值