项目发布中出现错误“无法同时使用注入和空构造函数构造视图'视图名称'”

本文关键字:视图 构造函数 注入 错误 项目发布 | 更新日期: 2023-09-27 18:33:38

(Use WPF, Catel, Telerik UI)

您好,下一个问题,当我尝试编译项目时程序在某些地方启动时出现以下错误:"无法同时使用注入和空构造函数构造视图'用户视图'"。

同时,当我在调试模式下在Visual Studio本身运行程序或发布程序运行时,没有错误。

可能是什么问题???

以下是调用View的方法,它们是相同的,但在第二种情况下,错误会消失:

private Command _showDashboardView;
        public Command ShowDashboardView
        {
            get
            {
                return _showDashboardView ?? (_showDashboardView = new Command(() =>
                {
                    _pleaseWaitService.Show();
                    CurrentViewModel = new DashboardViewModel();
                    _pleaseWaitService.Hide();
                }));
            }
        }
private Command _showUserView;
        public Command ShowUserView
        {
            get
            {
                return _showUserView ?? (_showUserView = new Command(() =>
                {
                    _pleaseWaitService.Show();
                    CurrentViewModel = new UserViewModel();
                    _pleaseWaitService.Hide();
                }));
            }
        }

用户视图模型:

public class UserViewModel : ViewModelBase
    {
        private readonly IUIVisualizerService _uiVisualizerService;
        private readonly IMessageService _messageService;
        private readonly IPleaseWaitService _pleaseWaitService;
        public olympEntities context
        {
            get { return GetValue<olympEntities>(contextProperty); }
            set { SetValue(contextProperty, value); }
        }
        public static readonly PropertyData contextProperty = RegisterProperty("context", typeof(olympEntities), null);
        public UserViewModel()
        {
            context = new olympEntities();
            Users = new ObservableCollection<User>(context.Users.OrderByDescending(u => u.ID_User));
            AddUser = new TaskCommand(OnAddUserExecuteAsync);
            EditUser = new TaskCommand(OnEditUserExecuteAsync);
            RemoveUser = new TaskCommand(OnRemoveUserExecuteAsync);
            AddTicket = new TaskCommand(OnAddTicketExecuteAsync);
            EditTicket = new TaskCommand(OnEditTicketExecuteAsync, OnEditTicketCanExecute);
            RemoveTicket = new TaskCommand(OnRemoveTicketExecuteAsync, OnEditTicketCanExecute);
            var dependencyResolver = this.GetDependencyResolver();
            _uiVisualizerService = dependencyResolver.Resolve<IUIVisualizerService>();
            _messageService = dependencyResolver.Resolve<IMessageService>();
            _pleaseWaitService = dependencyResolver.Resolve<IPleaseWaitService>();
        }
        public override string Title { get { return "CustomerViewModel"; } }

项目发布中出现错误“无法同时使用注入和空构造函数构造视图'视图名称'”

设计器正在尝试调用构造函数的逻辑。在 vm ctor 的开头使用以下代码:

if (CatelEnvironment.IsInDesignMode)
{
    return;
}

免费专业提示:使用依赖注入而不是解析构造函数中的服务:

public UserViewModel(IUIVisualizerService uiVisualizerService, IMessageService messageService, IPleaseWaitService pleaseWaitService)
{
    Argument.IsNotNull(() => uiVisualizerService);
    Argument.IsNotNull(() => messageService);
    Argument.IsNotNull(() => pleaseWaitService);
    _uiVisualizerService = uiVisualizerService;
    _messageService = messageService;
    _pleaseWaitService = pleaseWaitService;
    if (CatelEnvironment.IsInDesignMode)
    {
        return;
    }
    context = new olympEntities();
    Users = new ObservableCollection<User>(context.Users.OrderByDescending(u => u.ID_User));
    AddUser = new TaskCommand(OnAddUserExecuteAsync);
    EditUser = new TaskCommand(OnEditUserExecuteAsync);
    RemoveUser = new TaskCommand(OnRemoveUserExecuteAsync);
    AddTicket = new TaskCommand(OnAddTicketExecuteAsync);
    EditTicket = new TaskCommand(OnEditTicketExecuteAsync, OnEditTicketCanExecute);
    RemoveTicket = new TaskCommand(OnRemoveTicketExecuteAsync, OnEditTicketCanExecute);
}

看来我已经找到了问题的根源。经过长时间的试验,我意识到问题出在 XAML 标记中,问题是我没有询问元素 telerik 的名称:RadDataPager。设置名称后,错误就消失了。