当我在WPF Prism应用程序中使用自定义模式对话框时,“没有为此对象定义的无参数构造函数”错误

时间:2016-10-18 11:28:46

标签: c# wpf xaml mvvm prism-6

在我的Prism 6 WPF MVVM应用程序中,我使用自定义模式对话框登录用户。此对话框(在XAML中)产生以下设计时错误:'没有为此对象定义的无参数构造函数'。下面是对话框的XAML标记:

<UserControl x:Class="FlowmeterConfigurator.Views.LoginView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:prism="http://prismlibrary.com/"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:behavior="clr-namespace:CommonClassLibrary.Behaviors;assembly=CommonClassLibrary"
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
         prism:ViewModelLocator.AutoWireViewModel="True"
         Width="330" Height="175" MaxWidth="400" MaxHeight="300" MinWidth="200" MinHeight="100">

<i:Interaction.Triggers>
    <!-- OK-dialog -->
    <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest, Mode=OneWay}">
        <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <!--UserName-->
    <Label Grid.Row="0" Grid.Column="0" Content="Имя пользователя" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 30 0 0"/>
    <TextBox Grid.Row="0" Grid.Column="1" Height="30" Margin="0 30 5 0" Text="{Binding Username}" AutomationProperties.AutomationId="UserNameTextBox"/>
    <!--Pasword-->
    <Label Grid.Row="1" Grid.Column="0" Content="Пароль" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 10 0 0"/>
    <PasswordBox Grid.Row="1" Grid.Column="1" Height="30" Margin="0 10 5 0" AutomationProperties.AutomationId="UserPasswordBox">
        <i:Interaction.Behaviors>
            <behavior:PasswordBoxBindingBehavior Password="{Binding Password}"/>
        </i:Interaction.Behaviors>
    </PasswordBox>
    <!--Panel with the buttons 'Authrize' and 'Cancel'-->
    <StackPanel  Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
        <telerik:RadButton x:Name="radButtonAuthorize" Content="Авторизоваться" Height="30" Width="Auto" Margin="5 10 20 5"
                           Command="{Binding PerformAuthorizationCommand}" AutomationProperties.AutomationId="AuthrizeUserButton"/>
        <telerik:RadButton x:Name="radButtonCancel" Content="Отмена" Height="30" Width="Auto" Margin="0 10 5 5"
                           Command="{Binding СancelAuthorizationCommand}" AutomationProperties.AutomationId="CancelAuthorizationButton"/>
    </StackPanel>
</Grid>
</UserControl>

对话框的构造函数(来自其ViewModel)请参见下文:

public LoginViewModel(IEventAggregator eventAggregator, IAuthorizationService authorizationService)
{
    this._eventAggregator = eventAggregator;
    this._authorizationService = authorizationService;
    this.NotificationRequest = new InteractionRequest<INotification>();
    this.PerformAuthorizationCommand = new DelegateCommand(this.performAuthorization, this.performAuthorizationCanExecute);
    this.СancelAuthorizationCommand = new DelegateCommand(this.canсelAuthorization, this.canсelAuthorizationCanExecute);
}

正如您在上面的对话框XAML中所看到的,我使用行为(PasswordBoxBindingBehavior)来获取PasswordBox的数据绑定。下面是PasswordBoxBindingBehavior类定义:

public class PasswordBoxBindingBehavior : Behavior<PasswordBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged;
    }

    public SecureString Password
    {
        get { return (SecureString)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBindingBehavior), new PropertyMetadata(null));

    private void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e)
    {
        var binding = BindingOperations.GetBindingExpression(this, PasswordProperty);
        if (binding != null)
        {
            PropertyInfo property = binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path);
            if (property != null)
                property.SetValue(binding.DataItem, AssociatedObject.SecurePassword, null);
        }
    }
}

我从Application主窗口(Shell)调用此对话框。下面是XAML标记(与对话框相关)。此标记来自应用程序主窗口(Shell)标记。请参阅:

<prism:InteractionRequestTrigger SourceObject="{Binding LoginConfirmationRequest, Mode=OneWay}">
        <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
            <prism:PopupWindowAction.WindowContent>
                <views:LoginView/>
            </prism:PopupWindowAction.WindowContent>
            <prism:PopupWindowAction.WindowStyle>
                <Style TargetType="Window">
                    <Setter Property="ResizeMode" Value="NoResize"/>
                    <Setter Property="SizeToContent" Value="WidthAndHeight"/>
                </Style>
            </prism:PopupWindowAction.WindowStyle>
        </prism:PopupWindowAction>
</prism:InteractionRequestTrigger>

在这个标记中

<views:LoginView/>

存在以下错误:“没有为此对象定义无参数构造函数”。我尝试在对话框的ViewModel中添加第二个构造函数(无参数),但不会消除错误。虽然这只是设计时错误,但即便如此也应该被淘汰。如何更正此错误?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并发现如果你使用最近推出的功能here,就会有一个很好的解决方案。

也就是说,在你的xaml中你可以这样做:

<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowContentType="{x:Type views:LoginView}">
    <prism:PopupWindowAction.WindowStyle>
        <Style TargetType="Window">
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="SizeToContent" Value="WidthAndHeight"/>
        </Style>
    </prism:PopupWindowAction.WindowStyle>
</prism:PopupWindowAction>

而不是:

<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
    <prism:PopupWindowAction.WindowContent>
        <views:LoginView/>
    </prism:PopupWindowAction.WindowContent>
    <prism:PopupWindowAction.WindowStyle>
        <Style TargetType="Window">
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="SizeToContent" Value="WidthAndHeight"/>
        </Style>
    </prism:PopupWindowAction.WindowStyle>
</prism:PopupWindowAction>

这也有其他好处,与在引发交互请求时实例化的视图有关。