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

时间:2016-02-08 14:46:26

标签: c# wpf mvvm catel

(使用WPF,Catel,Telerik UI)

您好,接下来的问题,当我尝试在某些地方启动程序时编译项目时会出现以下错误: “无法使用注入和空构造函数构建视图'UserView'。”

同时我在Visual Studio中以调试模式运行程序或者Release程序运行时没有错误。

可能是什么问题???

以下是调用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();
                }));
            }
        }

UserViewModel:

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

2 个答案:

答案 0 :(得分:0)

设计者正在尝试调用构造函数的逻辑。在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);
}

答案 1 :(得分:0)

我似乎找到了问题的根源。 经过长时间的试验,我意识到问题出在XAML标记中,问题是我没有要求提供元素telerik的名称:RadDataPager。 一旦设置了名称,就会出现错误。