InteractionRequest

Interaction Request

View 端繫結 ViewModel 端所公布Interaction Request object。 當ViewModel端有需要時,用該Interaction Request object的 Raise函數發出請求,由View端加以處理。

NotificationRequest

ViewModel

public class MyViewModel : BindableBase
{
  public InteractionRequest<INotification> NotificationRequest { get; private set; }
 
  public MyViewModel(){
    ...
    this.NotificationRequest = new InteractionRequest<INotification>();
    ...
  }
  private void DoSomething(){
    ...
    this.NotificationRequest.Raise(
      new Notification { Content = "Notification Message", Title = "Notification" },
        n => { });
    ...
  }
}

View

<UserControl
...
  xmlns:prism="http://prismlibrary.com/"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  ...
>
<i:Interaction.Triggers>
  <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest, Mode=OneWay}">
    <!-- This PopupWindowAction does not have a custom view defined, therefore it will try to use a DefaultConfirmationWindow -->
    <!-- which is a window used by default by Prism to shown IConfirmation implementations -->
    <!-- That window will be show as a modal dialog and centered over this window -->
    <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
  </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
...
</UserControl>

ConfirmationRequest

ViewModel

public class MyViewModel : BindableBase
{
  public InteractionRequest<IConfirmation> ConfirmationRequest { get; private set; }
  public MyViewModel(){
    ...
    this.ConfirmationRequest = new InteractionRequest<IConfirmation>();
    ...
  }
  private void DoSomething(){
    ...
    bool isConfirmed = false;
    this.ConfirmationRequest.Raise(
      new Confirmation {
        Content = "Do you really want to delete PItem " + CurPItem.Name + "?",
        Title = "Confirmation" },
      c => { isConfirmed = c.Confirmed; });
    if (isConfirmed)
    ...
  }
}

View

<UserControl
...
  xmlns:prism="http://prismlibrary.com/"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  ...
>
<i:Interaction.Triggers>
  <prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest, Mode=OneWay}">
    <!-- This PopupWindowAction does not have a custom view defined, therefore it will try to use a DefaultNotificationWindow -->
    <!-- which is a window used by default by Prism to shown INotification implementations -->
    <!-- That window will be show as a modal dialog and centered over this window -->
    <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
  </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
...
</UserControl>

Share this

Previous
Next Post »

技術提供:Blogger.