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

このダイアログは、アプリケーションのメイン ウィンドウ (シェル) から呼び出します。以下は XAML マークアップ (ダイアログに関連) です。このマークアップは、アプリケーションのメイン ウィンドウ (シェル) マークアップからのものです。参照してください:

<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 に 2 番目のコンストラクター (パラメーターなし) を追加しようとしましたが、エラーは解消されません。これは設計時のエラーにすぎませんが、それでも排除する必要があります。このエラーを修正するにはどうすればよいですか?

4

1 に答える 1