Prohor
Prohor

Reputation: 151

'No parameterless constructor defined for this object' error has place when I use custom modal dialog in WPF Prism application

In my Prism 6 WPF MVVM application I use custom modal dialog for login of the users. This dialog produces (in XAML) the following design-time error: 'No parameterless constructor defined for this object'. Below is XAML markup of the dialog:

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

The constructor of the dialog (from its ViewModel) please see below:

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

As you can see from dialog's XAML above, I use the behavior (PasswordBoxBindingBehavior) to get PasswordBox' databinding possible. Below is PasswordBoxBindingBehavior class definition:

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

I call this dialog from Application main window (Shell). Below is XAML markup (related to the dialog). This markup is from Application main window (Shell) markup. Please see:

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

And in this markup in the line

<views:LoginView/>

there is the following error: "No parameterless constructor defined for this object". I try to add the second constructor (that is parameterless) in ViewModel of the dialog but the error is not eliminated. Though this is only design-time error, but even so it should be eliminated. How can I correct this error?

Upvotes: 1

Views: 2106

Answers (1)

Adam Goodwin
Adam Goodwin

Reputation: 4071

I was having the same problem, and found that a nice solution exists if you use the recently introduced feature here.

That is, in your xaml you can do this:

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

Instead of this:

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

This has other benefits, to do with the view being instantiated at the time of the interaction request being raised, as well.

Upvotes: 1

Related Questions