WPF Prism MVVM 中 弹出新窗体. 放入用户控件

原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37214567/article/details/79657338

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

WPF Prism MVVM 中 弹出新窗体. 放入用户控件., 传入一些数据,并且可以返回一些数据.

对于一个WPF MVVM 的 萌新来说. 必然会遇到一个 弹出 新窗口 的问题.

今天,就和大家分享一下,这个问题的解决方法.

.

一. 准备

  • .dll
  • .CS 文件 (StylePopupWindowAction.cs)
  • .xaml 文件(BorderlessWindow.xaml)

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

这里是 实例化一个窗口,然后 加载窗口的样式. ( StylePopupWindowAction.cs)

using System;
using System.Windows;
using System.Windows.Input;
using Prism.Interactivity;
using Prism.Interactivity.InteractionRequest; namespace HXDJSsYSTEM.WindowAction
{
public class StylePopupWindowAction : PopupWindowAction
{
private Window _wrapperWindow;
/// <summary>
/// 通过重写PopupWindowAction中的GetWindow方法,设置Window的Style属性。
/// 否则打开的只能是默认窗体,无法设置样式。
/// </summary>
/// <param name="notification"></param>
/// <returns></returns>
protected override Window GetWindow(INotification notification)
{
if (this.WindowContent != null)
{
//初始化窗口
_wrapperWindow = new Window
{
//数据上下文
DataContext = notification,
Title = notification.Title,
//宽高自适应 内容
SizeToContent = SizeToContent.WidthAndHeight, }; _wrapperWindow.KeyDown += WrapperWindow_KeyDown;
ResourceDictionary langRd = null;
try
{
//读取资源文件(样式)
langRd = Application.LoadComponent(new Uri("/WindowAction/BorderlessWindow.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;
}
catch
{
}
//判断资源是否 读取成功.读取成功则不为null
if (langRd != null)
{
//判断是否已经有资源. 如果有 就 clear;
if (_wrapperWindow.Resources.MergedDictionaries.Count > 0)
{
_wrapperWindow.Resources.MergedDictionaries.Clear();
}
//将资源加入到window中
_wrapperWindow.Resources.MergedDictionaries.Add(langRd);
}
//将传入的 notification 放入 window
PrepareContentForWindow(notification, _wrapperWindow);
}
else
{
//如果WindowContent 等于null 则 生成默认窗口
_wrapperWindow = this.CreateDefaultWindow(notification);
} return _wrapperWindow;
} /// <summary>
/// 键盘按下 事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WrapperWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
//按下ESC 关闭窗口
if (e.Key == Key.Escape)
{
_wrapperWindow.Close();
} }
}
}

这里是上面代码 要加载的 样式 ( BorderlessWindow.xaml )

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Name="BorderlessWindowStyle"
TargetType="{x:Type Window}">
<Setter Property="FontFamily"
Value="{DynamicResource DefaultFontFamily}" />
<Setter Property="FontSize"
Value="{DynamicResource DefaultFontSize}" />
<Setter Property="Foreground"
Value="{DynamicResource WindowText}" />
<Setter Property="BorderBrush"
Value="{DynamicResource WindowBorder}" /> <Setter Property="ResizeMode"
Value="CanResizeWithGrip" />
<Setter Property="UseLayoutRounding"
Value="True" />
<Setter Property="TextOptions.TextFormattingMode"
Value="Display" /> <Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border x:Name="WindowBorder"
Margin="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}}">
<Border.Background>
<SolidColorBrush x:Name="WindowBorderBackground"
Color="{DynamicResource WindowBackgroundColor}" />
</Border.Background>
<Border.Resources>
<Storyboard x:Key="BackgroundAnimation">
<ColorAnimation Storyboard.TargetName="WindowBorderBackground"
Storyboard.TargetProperty="Color"
To="{DynamicResource WindowBackgroundColor}"
Duration="0:0:.6" />
</Storyboard>
</Border.Resources> <Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<AdornerDecorator>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="36" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions> <!-- title bar -->
<Grid>
<!-- title -->
<TextBlock Text="{TemplateBinding Title}"
Margin="8,0"
VerticalAlignment="Center"
Visibility="Visible" />
<!-- window system buttons-->
<StackPanel VerticalAlignment="Top"
HorizontalAlignment="Right"
WindowChrome.IsHitTestVisibleInChrome="True">
<Button Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}"
ToolTip="关闭">
<Button.Content>
<Grid Width="13"
Height="12"
RenderTransform="1,0,0,1,0,1">
<Path Data="M0,0 L8,7 M8,0 L0,7 Z"
Width="8"
Height="7"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"
StrokeThickness="1.5" />
</Grid>
</Button.Content>
</Button>
</StackPanel>
</Grid> <Grid Grid.Row="1"
Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="42" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<Grid Grid.Row="2">
<ContentPresenter />
</Grid>
</Grid>
</AdornerDecorator>
</Border>
</Border> <ControlTemplate.Triggers>
<Trigger Property="IsActive"
Value="True">
<Setter Property="BorderBrush"
Value="{DynamicResource WindowBorderActive}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter> <Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CornerRadius="0"
GlassFrameThickness="1"
UseAeroCaptionButtons="False"
NonClientFrameEdges="None" />
</Setter.Value>
</Setter>
</Style>
<Color x:Key="WindowBackgroundColor">#ffffff</Color> <SolidColorBrush x:Key="ButtonBackground"
Color="#ffffff" />
<SolidColorBrush x:Key="ButtonBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="ButtonBackgroundPressed"
Color="BlueViolet" />
<SolidColorBrush x:Key="ButtonBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="ButtonBorderHover"
Color="#cccccc" />
<SolidColorBrush x:Key="ButtonBorderPressed"
Color="BlueViolet" />
<SolidColorBrush x:Key="ButtonText"
Color="#333333" />
<SolidColorBrush x:Key="ButtonTextHover"
Color="#333333" />
<SolidColorBrush x:Key="ButtonTextPressed"
Color="#ffffff" />
<SolidColorBrush x:Key="ButtonTextDisabled"
Color="#a1a1a1" /> <SolidColorBrush x:Key="DataGridBackground"
Color="Transparent" />
<SolidColorBrush x:Key="DataGridForeground"
Color="#333333" />
<SolidColorBrush x:Key="DataGridCellBackground"
Color="Transparent" />
<SolidColorBrush x:Key="DataGridCellBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="DataGridCellBackgroundSelected"
Color="BlueViolet" />
<SolidColorBrush x:Key="DataGridCellForeground"
Color="#333333" />
<SolidColorBrush x:Key="DataGridCellForegroundHover"
Color="#333333" />
<SolidColorBrush x:Key="DataGridCellForegroundSelected"
Color="#ffffff" />
<SolidColorBrush x:Key="DataGridHeaderBackground"
Color="Transparent" />
<SolidColorBrush x:Key="DataGridHeaderBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="DataGridHeaderBackgroundPressed"
Color="BlueViolet" />
<SolidColorBrush x:Key="DataGridHeaderBackgroundSelected"
Color="BlueViolet" />
<SolidColorBrush x:Key="DataGridHeaderForeground"
Color="#333333" />
<SolidColorBrush x:Key="DataGridHeaderForegroundHover"
Color="#333333" />
<SolidColorBrush x:Key="DataGridHeaderForegroundPressed"
Color="#ffffff" />
<SolidColorBrush x:Key="DataGridHeaderForegroundSelected"
Color="#ffffff" />
<SolidColorBrush x:Key="DataGridGridLines"
Color="#cccccc" />
<SolidColorBrush x:Key="DataGridDropSeparator"
Color="BlueViolet" /> <SolidColorBrush x:Key="Hyperlink"
Color="BlueViolet" />
<SolidColorBrush x:Key="HyperlinkHover"
Color="BlueViolet" />
<SolidColorBrush x:Key="HyperlinkDisabled"
Color="#919191" /> <SolidColorBrush x:Key="InputBackground"
Color="#ffffff" />
<SolidColorBrush x:Key="InputBackgroundHover"
Color="#ffffff" />
<SolidColorBrush x:Key="InputBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="InputBorderHover"
Color="#cccccc" />
<SolidColorBrush x:Key="InputText"
Color="#333333" />
<SolidColorBrush x:Key="InputTextHover"
Color="#333333" />
<SolidColorBrush x:Key="InputTextDisabled"
Color="#919191" /> <SolidColorBrush x:Key="ItemBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="ItemBackgroundSelected"
Color="BlueViolet" />
<SolidColorBrush x:Key="ItemBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="ItemText"
Color="#333333" />
<SolidColorBrush x:Key="ItemTextSelected"
Color="#ffffff" />
<SolidColorBrush x:Key="ItemTextHover"
Color="#333333" />
<SolidColorBrush x:Key="ItemTextDisabled"
Color="#919191" /> <SolidColorBrush x:Key="LinkButtonText"
Color="#717171" />
<SolidColorBrush x:Key="LinkButtonTextHover"
Color="#333333" />
<SolidColorBrush x:Key="LinkButtonTextPressed"
Color="#919191" />
<SolidColorBrush x:Key="LinkButtonTextDisabled"
Color="#919191" /> <SolidColorBrush x:Key="MenuText"
Color="#66000000" />
<SolidColorBrush x:Key="MenuTextHover"
Color="#bb000000" />
<SolidColorBrush x:Key="MenuTextSelected"
Color="#333333" /> <SolidColorBrush x:Key="ModernButtonBorder"
Color="#919191" />
<SolidColorBrush x:Key="ModernButtonBorderHover"
Color="#333333" />
<SolidColorBrush x:Key="ModernButtonBorderPressed"
Color="#333333" />
<SolidColorBrush x:Key="ModernButtonBorderDisabled"
Color="#919191" />
<SolidColorBrush x:Key="ModernButtonIconBackgroundPressed"
Color="BlueViolet" />
<SolidColorBrush x:Key="ModernButtonIconForegroundPressed"
Color="#ffffff" />
<SolidColorBrush x:Key="ModernButtonText"
Color="#333333" />
<SolidColorBrush x:Key="ModernButtonTextHover"
Color="#333333" />
<SolidColorBrush x:Key="ModernButtonTextPressed"
Color="#333333" />
<SolidColorBrush x:Key="ModernButtonTextDisabled"
Color="#a1a1a1" /> <SolidColorBrush x:Key="PopupBackground"
Color="#ffffff" /> <SolidColorBrush x:Key="ProgressBackground"
Color="#dddddd" /> <SolidColorBrush x:Key="ScrollBarBackground"
Color="Transparent" />
<SolidColorBrush x:Key="ScrollBarThumbBackground"
Color="Transparent" />
<SolidColorBrush x:Key="ScrollBarThumbBackgroundDragging"
Color="#cccccc" />
<SolidColorBrush x:Key="ScrollBarThumbBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="ScrollBarThumbBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="ScrollBarThumbForeground"
Color="Transparent" />
<SolidColorBrush x:Key="ScrollBarThumbForegroundDragging"
Color="Transparent" />
<SolidColorBrush x:Key="ScrollBarThumbForegroundHover"
Color="Transparent" /> <SolidColorBrush x:Key="SeparatorBackground"
Color="#cccccc" /> <SolidColorBrush x:Key="SliderSelectionBackground"
Color="BlueViolet" />
<SolidColorBrush x:Key="SliderSelectionBorder"
Color="BlueViolet" />
<SolidColorBrush x:Key="SliderThumbBackground"
Color="#ffffff" />
<SolidColorBrush x:Key="SliderThumbBackgroundDragging"
Color="BlueViolet" />
<SolidColorBrush x:Key="SliderThumbBackgroundHover"
Color="#dddddd" />
<SolidColorBrush x:Key="SliderThumbBackgroundDisabled"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderThumbBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderThumbBorderDragging"
Color="BlueViolet" />
<SolidColorBrush x:Key="SliderThumbBorderHover"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderThumbBorderDisabled"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderTrackBackground"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderTrackBorder"
Color="#cccccc" />
<SolidColorBrush x:Key="SliderTick"
Color="#919191" />
<SolidColorBrush x:Key="SliderTickDisabled"
Color="#cccccc" /> <SolidColorBrush x:Key="SubMenuText"
Color="#99000000" />
<SolidColorBrush x:Key="SubMenuTextHover"
Color="#66000000" />
<SolidColorBrush x:Key="SubMenuTextSelected"
Color="#333333" /> <SolidColorBrush x:Key="WindowBackground"
Color="{StaticResource WindowBackgroundColor}" />
<SolidColorBrush x:Key="WindowBorder"
Color="BlueViolet"
Opacity=".5" />
<SolidColorBrush x:Key="WindowBorderActive"
Color="BlueViolet" />
<LinearGradientBrush x:Key="WindowHeaderGradient"
StartPoint="0, 0"
EndPoint="0, 1"
Opacity=".1">
<GradientStop Offset="0"
Color="BlueViolet" />
<GradientStop Offset=".3"
Color="BlueViolet" />
<GradientStop Offset="1"
Color="Transparent" />
</LinearGradientBrush>
<SolidColorBrush x:Key="WindowText"
Color="#333333" />
<SolidColorBrush x:Key="WindowTextReadOnly"
Color="#919191" /> <Rectangle x:Key="WindowBackgroundContent"
x:Shared="false"
Height="96"
Fill="{StaticResource WindowHeaderGradient}"
VerticalAlignment="Top" />
</ResourceDictionary>

二. 我们该怎么用它.

1.窗口的XAML

这里我们需要用到Triggers

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

2.窗口的 ViewModel

在这里我们需要引用命名空间 ,大家没有这个DLL的 可以在NuGet上搜索.安装就可以了.

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

3.这里是ViewModel的具体方法

  • 定义
  • 在构造参数中实例化
  • 传入Content和Title

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

4.要放入Window 中 的 用户控件 的ViewModel

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

5.用户控件的ViewModel 中的具体操作

  • 放入返回值
  • 关闭窗口

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

6.返回值的处理

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

注意:

  • 用户控件 在触发器那里指定的时候,要注意 防止出现死循环. 会报错…
  • 这只是其中一种解决方案… 可能不是最好的.

这些就是 这个 弹出窗体的 具体操作了. 也比较简单. 看不懂的 多看两遍. 然后 动手操作一下.

WPF Prism MVVM 中  弹出新窗体. 放入用户控件

上一篇:WPF实现窗体中的悬浮按钮


下一篇:数据库对象映射为java对象,不使用框架