C# ';没有为此对象定义无参数构造函数';在WPF Prism应用程序中使用自定义模式对话框时出现错误

C# ';没有为此对象定义无参数构造函数';在WPF Prism应用程序中使用自定义模式对话框时出现错误,c#,wpf,xaml,mvvm,prism-6,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

在我的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>

对话框的构造函数(从其视图模型)请参见以下内容:

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);
}
public LoginViewModel(IEventAggregator事件聚合器、IAAuthorizationService授权服务)
{
这。_eventAggregator=eventAggregator;
此._authorizationService=authorizationService;
this.NotificationRequest=new InteractionRequest();
this.performAuthorization命令=新的DelegateCommand(this.performAuthorization,this.performAuthorizationCanExecute);
this.Сantelauthorization命令=新的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);
        }
    }
}
公共类PasswordBoxBindingBehavior:Behavior
{
受保护的覆盖无效附加()
{
AssociatedObject.PasswordChanged+=OnPasswordBoxValueChanged;
}
公共安全字符串密码
{
get{return(SecureString)GetValue(PasswordProperty);}
set{SetValue(PasswordProperty,value);}
}
公共静态只读从属属性PasswordProperty=
dependenceProperty.Register(“Password”,type of(SecureString),type of(PasswordBoxBindingBehavior),newpropertymetata(null));
PasswordBoxValueChanged上的私有无效(对象发送方,RoutedEventArgs e)
{
var binding=BindingOperations.GetBindingExpression(此为PasswordProperty);
if(绑定!=null)
{
PropertyInfo property=binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path);
if(属性!=null)
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中添加第二个构造函数(即无参数),但错误没有消除。虽然这只是设计时的错误,但即使如此,它也应该被消除。如何更正此错误?

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

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


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

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

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


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

如果您只在设计器视图中看到此错误消息,但代码已编译且可以显示弹出窗口,那么这只是另一个奇怪的XAML设计器/IntelliSense错误。忽略它。我有另一个自定义弹出窗口,在它的ViewModel中有无参数构造函数,并且没有这样的错误!如果您仅在Designer视图中看到此错误消息,但您的代码已编译并且可以显示弹出窗口,那么这只是另一个奇怪的XAML Designer/IntelliSense错误。忽略它。我有另一个自定义弹出窗口,在它的ViewModel中有无参数构造函数,并且没有这样的错误!