Prism自定义确认交互

2024-05-09

我使用 Prism Unity、WPF 和 Mvvm 在应用程序中创建了一个自定义确认窗口。我需要有关需要发送回视图模型的通知的帮助。我在详细记录视图中有这个,我们称之为 MyDetailView。

<!-- Custom Confirmation Window -->
<ie:Interaction.Triggers>
  <interactionRequest:InteractionRequestTrigger 
       SourceObject="{Binding ConfirmationRequest, Mode=TwoWay}">
     <mycontrols:PopupWindowAction1 IsModal="True"/>
  </interactionRequest:InteractionRequestTrigger>
</ie:Interaction.Triggers>

如上图,我设置了交互Mode=TwoWay,这样确认弹窗就可以回传OK或Cancel按钮的点击结果。确认窗口按其应有的方式显示,但我不知道如何将按钮单击结果发送回我的视图模型,例如 MyDetailViewModel。这是主要问题。

编辑:这是引发 InteractionRequest 的 MyDetailViewMmodel 方法。

private void RaiseConfirmation()
{ConfirmationRequest
    .Raise(new Confirmation()
    {
        Title = "Confirmation Popup",
        Content = "Save Changes?"
    },  c =>{if (c.Confirmed)
{ UoW.AdrTypeRos.Submit();}

这是 PopupWindow Action1 类。该问题的部分答案可能是如何实现“通知”和“完成交互”方法。

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

我需要在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>

我是否需要在按钮内进行这种类型的交互?如果是这样,我如何对其进行编码,以便它与调用交互的特定视图模型相对应。有什么建议么?谢谢。


最主要的是,当您引发交互时,提供交互完成时触发的回调。此回调会返回通知,并且您的交互应该已将所有可能有趣的返回值存储在那里。

这是一个例子...

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

...从视图来看:

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

此外,我们需要一个类来保存传递的数据,以及一个用于交互本身的 ViewModel/View 对。

这是数据保存类(注意Maximum已通过to交互作用,以及Quantity returned从中):

internal class SelectQuantityNotification : Confirmation
{
    public int Maximum
    {
        get;
        set;
    }

    public int Quantity
    {
        get;
        set;
    }
}

这是交互弹出窗口的视图:

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

这是视图模型:

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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Prism自定义确认交互 的相关文章

随机推荐