WPF + Prism + Interaction无法正常工作

时间:2015-04-07 12:52:07

标签: c# wpf mvvm prism

我在设置与Prism的交互时遇到问题。

我想点击按钮打开“编辑”窗口。窗口打开,但它没有任何内容。

我创建了我的Popup视图:

<UserControl x:Class="xxx.ClientEditView"
             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:viewModels="clr-namespace:xxx.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             d:DataContext="{d:DesignInstance viewModels:ClientEditViewModel}">
    <Grid>
        <TextBlock Text="asd"></TextBlock>
    </Grid>
</UserControl>

和后端:

public partial class ClientEditView : UserControl
{
    public ClientEditView(ClientEditViewModel vm)
    {
        InitializeComponent();
        ViewModel = vm;
    }

    public ClientEditView()
    {
        this.ViewModel = new ClientEditViewModel();
    }

    public ClientEditViewModel ViewModel
    {
        get { return (ClientEditViewModel) DataContext; }
        set { DataContext = value; }
    }
}

视图模型:

public class ClientEditViewModel : BindableBase, IInteractionRequestAware
{
    private ClientEditNotification _notification;

    public ClientEditViewModel()
    {
    }


    public INotification Notification
    {
        get { return _notification; }
        set
        {
            if (value is ClientEditNotification)
            {
                this._notification = value as ClientEditNotification;
                this.OnPropertyChanged(() => this.Notification);
            }
        }
    }

    public Action FinishInteraction { get; set; }
}

我在父视图+ ViewModel中创建了交互:

<UserControl x:Class="xxx.ClientDetailsView"
             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:viewModels="clr-namespace:xxx.ViewModels"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:views="clr-namespace:OrtMan.Ribbon.ClientDetails.Views"
             xmlns:prism="http://www.codeplex.com/prism"
             mc:Ignorable="d"
             d:DesignHeight="768" d:DesignWidth="1366"
             d:DataContext="{d:DesignInstance viewModels:ClientDetailsViewModel}">


    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding EditItemRequest, Mode=OneWay}">
            <prism:PopupWindowAction>
                <prism:PopupWindowAction.WindowContent>
                    <views:ClientEditView />
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>


    <UserControl.Resources>
        <BitmapImage x:Key="DefaultImage" UriSource="/xxx;component/Images/noimage.jpg"/>
        </UserControl.Resources>
        <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="200" Width="1*" MaxWidth="400"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <Button Content="Edit" HorizontalAlignment="Right" Margin="5" Command="{Binding EditCommand}"/>
            <Image Width="150" Height="150" Margin="0 10 0 10" Source="{StaticResource DefaultImage}"></Image>
            <TextBlock Text="{Binding SelectedPatient.Name}" Margin="10,5,0,0"></TextBlock>
            <TextBlock Text="{Binding SelectedPatient.LastName}" Margin="10,5,0,0"></TextBlock>
            <TextBlock Text="{Binding SelectedPatient.DateOfBirth, StringFormat=dd-MM-yyyy}" Margin="10,5,0,0"></TextBlock>
       </StackPanel>
    </Grid>
</UserControl>

视图模型:

public class ClientDetailsViewModel : BaseRibbonModule
{


    public ClientDetailsViewModel(IRegionManager regionManager, IApplicationService applicationService, IEventAggregator eventAggregator) : base(regionManager)
    {
        ButtonText = "Client Details";
        EditItemRequest = new InteractionRequest<ClientEditNotification>();
    }


    public InteractionRequest<ClientEditNotification> EditItemRequest { get; private set; }

    public override string MainView
    {
        get { return "ClientDetailsView"; }

    }

    public string MainText { get; set; }

    private DelegateCommand _editCommand;

    public DelegateCommand EditCommand
    {
        get
        {
            return _editCommand ?? (_editCommand = new DelegateCommand(
                () =>
                {
                    EditItemRequest.Raise(new ClientEditNotification() {Title = "a", Value = "xxx"});  
                },
                () =>
                {
                    return true;
                }));
        }
    }

}
  • 创建了单个通知:

    公共类ClientEditNotification:确认 {     public String Value {get;组; } }

结果是,在调用EditCommand之后,新窗口正在打开,但其中没有任何内容。

我做错了吗?也许我忘了什么?

由于

- 编辑 -

经过一些调试后我发现: - 如果我在Notification上向Content属性添加内容,它会在Popup上显示为字符串 - 我已经调试了snoop我的弹出窗口,我发现正确的视图在内容中,但我不知道它为什么没有显示任何内容

Snoop Debug

顺便说一句。 ClassNames可能会有所不同,因为我已经针对这个问题制作了10个不同的项目,但没有一个可以工作。

0 个答案:

没有答案