1

在我的 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);
        }
    }
}

我从应用程序主窗口(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 中添加第二个构造函数(即无参数),但错误并未消除。虽然这只是设计时错误,但即便如此,它也应该被消除。我该如何纠正这个错误?

4

1 回答 1

0

我遇到了同样的问题,并且发现如果您使用此处最近引入的功能,存在一个很好的解决方案。

也就是说,在您的 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>

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

于 2017-10-27T13:23:11.373 回答