棱镜自定义确认交互 [英] Prism Custom Confirmation Interaction

查看:90
本文介绍了棱镜自定义确认交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Prism Unity,WPF和应用程序在应用程序中创建了一个自定义确认窗口. Mvvm.我需要将通知发送回ViewModel的帮助.我在详细记录视图中拥有此文件,我们将其称为MyDetailView.

I created a custom confirmation window in an app with Prism Unity, WPF & Mvvm. I need help with the notifications that need to be sent back to the viewmodel. I have this in the detail record view, let's call it 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,以便确认弹出窗口可以将确定或取消按钮的按钮单击结果发送回去.确认窗口会出现,但我不知道如何将按钮单击结果发送回我的视图模型,例如MyDetailViewModel.这是主要问题.

As shown above, I made the interaction Mode=TwoWay so that the confirmation popup window can send back the button click result for the OK or Cancel button. The confirmation window appears as it should, but I don't know how to send the button click result back to my viewmodel, say MyDetailViewModel. That is the main question.

这是引发InteractionRequest的MyDetailViewMmodel方法.

This is MyDetailViewMmodel method that raises the InteractionRequest.

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

这是PopupWindowAction1类.该问题的部分答案可能是如何实现Notification和FinishedInteraction方法.

This is the PopupWindowAction1 class. Part of the answer to the question may be how do I implement the Notification and FinishedInteraction methods.

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中进行一些交互吗?

Is there some interaction I need to put in my ConfirmWindow1, something like this?

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

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

Do I even need to do that type of interaction within the button? If so, how do I code it needs so that it corresponds to the particular viewmodel that invoked the interaction. Any suggestions? Thank you.

推荐答案

主要是,当您引发交互时,提供一个在交互完成时触发的回调.此回调会返回通知,您的交互应该已经在其中存储了所有可能有趣的返回值.

Main thing is, when you raise the interaction, provide a callback that is triggered when the interaction is finished. This callback gets the notification back and your interaction should have stored all potentially interesting return values there.

这是一个例子...

ViewModel的相关部分:

Relevant parts of the 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 ) );
    } );

...并从视图中查看:

... and from the View:

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

Additionally, we need a class to hold the data that's passed around, and a ViewModel/View pair for the interaction itself.

这是数据保存类(请注意,将Maximum传递给 交互,并从中返回Quantity ):

Here's the data holding class (note that Maximum is passed to the interaction, and Quantity returned from it):

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

    public int Quantity
    {
        get;
        set;
    }
}

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

This is the View of the interaction popup:

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

它是ViewModel:

and it's 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
}

这篇关于棱镜自定义确认交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆