SaTuN February 2016

Error "Failed to construct view 'View Name' with both injection and empty constructor" in the publication of the project

(Used WPF, Catel, Telerik UI)

Hello, the problem in the next, when I try to compile the project when the program starts in certain places gets the following error: "Failed to construct view 'UserView' with both injection and empty constructor".

At the same time when I run the program in Visual Studio itself in Debug mode or Release program runs without errors.

What could be the problem???

Here are the methods call View, they are the same, but in the second case, the error takes off:

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()
             

Answers


Geert van Horrik February 2016

The designer is trying to invoke the logic of your constructor. Use this code at the beginning of your vm ctor:

if (CatelEnvironment.IsInDesignMode)
{
    return;
}

Free pro tip: use dependency injection instead of resolving the services inside your constructor:

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


SaTuN February 2016

It seems I've found the source of the problem. After long trials I realized that the problem is in the XAML markup, the problem was that I did not ask for the name of the element telerik: RadDataPager. Once the name has been set, an error was gone.

Post Status

Asked in February 2016
Viewed 2,033 times
Voted 14
Answered 2 times

Search




Leave an answer