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;
}