har*_*nis 3 wpf notifications interaction prism mvvm
我在Prism Unity,WPF和Mvvm的应用程序中创建了一个自定义确认窗口.我需要有关需要发送回viewmodel的通知的帮助.我在详细记录视图中有这个,我们称之为MyDetailView.
<!-- Custom Confirmation Window -->
<ie:Interaction.Triggers>
<interactionRequest:InteractionRequestTrigger
SourceObject="{Binding ConfirmationRequest, Mode=TwoWay}">
<mycontrols:PopupWindowAction1 IsModal="True"/>
</interactionRequest:InteractionRequestTrigger>
</ie:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
如上所示,我进行了交互Mode = TwoWay,以便确认弹出窗口可以发回OK或取消按钮的按钮点击结果.确认窗口应该出现,但我不知道如何将按钮点击结果发送回我的viewmodel,比如MyDetailViewModel.这是主要问题.
编辑:这是引发InteractionRequest的MyDetailViewMmodel方法.
private void RaiseConfirmation()
{ConfirmationRequest
.Raise(new Confirmation()
{
Title = "Confirmation Popup",
Content = "Save Changes?"
}, c =>{if (c.Confirmed)
{ UoW.AdrTypeRos.Submit();}
Run Code Online (Sandbox Code Playgroud)
这是PopupWindowAction1类.问题的部分答案可能是如何实现Notification和FinishedInteraction方法.
class PopupWindowAction1 : PopupWindowAction, IInteractionRequestAware
{
protected override Window GetWindow(INotification notification)
{ // custom metrowindow using mahapps
MetroWindow wrapperWindow = new ConfirmWindow1();
wrapperWindow.DataContext = notification;
wrapperWindow.Title = notification.Title;
this.PrepareContentForWindow(notification, wrapperWindow);
return wrapperWindow;
}
public INotification Notification
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public Action FinishInteraction
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
}
Run Code Online (Sandbox Code Playgroud)
我需要在我的ConfirmWindow1中添加一些交互,这样的话吗?
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<ei:CallMethodAction
TargetObject="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
Path=DataContext}"
MethodName="DataContext.ValidateConfirm"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
我甚至需要在按钮内进行这种类型的互动吗?如果是这样,我如何对其进行编码以使其对应于调用交互的特定视图模型.有什么建议?谢谢.
主要的是,当您提升交互时,提供在交互完成时触发的回调.此回调会返回通知,您的交互应该存储所有可能有趣的返回值.
这是一个例子......
ViewModel的相关部分:
public InteractionRequest<SelectQuantityNotification> SelectQuantityRequest
{
get;
}
// in some handler that triggers the interaction
SelectQuantityRequest.Raise( new SelectQuantityNotification { Title = "Load how much stuff?", Maximum = maximumQuantity },
notification =>
{
if (notification.Confirmed)
_worldStateService.ExecuteCommand( new LoadCargoCommand( sourceStockpile.Stockpile, cartViewModel.Cart, notification.Quantity ) );
} );
Run Code Online (Sandbox Code Playgroud)
......并从视图:
<i:Interaction.Triggers>
<interactionRequest:InteractionRequestTrigger
SourceObject="{Binding SelectQuantityRequest, Mode=OneWay}">
<framework:FixedSizePopupWindowAction>
<interactionRequest:PopupWindowAction.WindowContent>
<views:SelectSampleDataForImportPopup/>
</interactionRequest:PopupWindowAction.WindowContent>
</framework:FixedSizePopupWindowAction>
</interactionRequest:InteractionRequestTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
另外,我们需要一个类来保存传递的数据,以及一个用于交互本身的ViewModel/View对.
这是数据保持类(注意Maximum传递给交互,并从中Quantity 返回):
internal class SelectQuantityNotification : Confirmation
{
public int Maximum
{
get;
set;
}
public int Quantity
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
这是交互弹出视图:
<UserControl x:Class="ClientModule.Views.SelectSampleDataForImportPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
<TextBlock>
Amount: <Run Text="{Binding Quantity}"/>
</TextBlock>
<Slider Orientation="Horizontal" Minimum="0" Maximum="{Binding Maximum}" Value="{Binding Quantity}" TickPlacement="BottomRight"/>
<Button Content="Ok" Command="{Binding OkCommand}"/>
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
它是ViewModel:
internal class SelectSampleDataForImportPopupViewModel : BindableBase, IInteractionRequestAware
{
public SelectSampleDataForImportPopupViewModel()
{
OkCommand = new DelegateCommand( OnOk );
}
public DelegateCommand OkCommand
{
get;
}
public int Quantity
{
get { return _notification?.Quantity ?? 0; }
set
{
if (_notification == null)
return;
_notification.Quantity = value;
OnPropertyChanged( () => Quantity );
}
}
public int Maximum => _notification?.Maximum ?? 0;
#region IInteractionRequestAware
public INotification Notification
{
get { return _notification; }
set
{
SetProperty( ref _notification, value as SelectQuantityNotification );
OnPropertyChanged( () => Maximum );
OnPropertyChanged( () => Quantity );
}
}
public Action FinishInteraction
{
get;
set;
}
#endregion
#region private
private SelectQuantityNotification _notification;
private void OnOk()
{
if (_notification != null)
_notification.Confirmed = true;
FinishInteraction();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)