0

I'm creating a WPF application to manage inventory, but I'm having trouble creating the views. I'm using the MVVM pattern, and I'm getting an error when instantiating the Views XAML file.

It would be very helpful if you could tell me how I can fix this, or if I need to change any part of my code.

<UserControl.Resources> <viewmodels:UserViewModel x:Key="UserViewModel"/> </UserControl.Resources> 

In the user view, I get the following error on the previous line:

XDG0003 Instance error

This prevents me from seeing the view preview and running my application. This error only occurs in the application views, and I don't know what could be causing it.

UserModel:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WPF_CS_InventoryManager.Models { public class UserModel { private string _firstname; private string _lastname; private string _email; private string _id; public string Firstname { get => _firstname; set { if (_firstname != value) { _firstname = value; } } } public string Lastname { get => _lastname; set { if (_lastname != value) { _lastname = value; } } } public string Email { get => _email; set { if (_email != value) { _email = value; } } } public string Id { get => _id; set { if (_id != value) { _id = value; } } } } } 

BaseViewModel:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WPF_CS_InventoryManager.ViewModels { internal class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } 

UserViewModel:

using System.Collections.ObjectModel; using System.Windows.Input; using WPF_CS_InventoryManager.Commands; using WPF_CS_InventoryManager.DataBase; using WPF_CS_InventoryManager.Models; using WPF_CS_InventoryManager.Utils; namespace WPF_CS_InventoryManager.ViewModels { internal class UserViewModel : BaseViewModel { private readonly BD _bd; private ObservableCollection<UserModel> _users; private UserModel _selectedUser; public UserViewModel() { _bd = new BD(); _users = _bd.GetUsers(); _selectedUser = new UserModel(); } public ObservableCollection<UserModel> Users { get => _users; set { if (_users != value) { _users = value; OnPropertyChanged(nameof(Users)); } } } public UserModel SelectedUser { get => _selectedUser; set { if (_selectedUser != value) { _selectedUser = value; OnPropertyChanged(nameof(SelectedUser)); } } } // ========== COMMANDS ========== public ICommand AddCommand => new RelayCommand(AddExecute, CanAddExecute); public ICommand EditCommand => new RelayCommand(EditExecute, CanEditOrDelete); public ICommand DeleteCommand => new RelayCommand(DeleteExecute, CanEditOrDelete); public ICommand ExportPdfCommand => new RelayCommand(_ => ExportPdf()); private void AddExecute(object? _) { _bd.AddUser(SelectedUser); Users = _bd.GetUsers(); SelectedUser = new UserModel(); } private bool CanAddExecute(object? _) { // Aquí podrías validar campos requeridos return !string.IsNullOrWhiteSpace(SelectedUser?.Id); } private void EditExecute(object? _) { _bd.EditUser(SelectedUser); Users = _bd.GetUsers(); } private void DeleteExecute(object? _) { _bd.DeleteUser(SelectedUser); Users = _bd.GetUsers(); SelectedUser = new UserModel(); } private bool CanEditOrDelete(object? _) { return SelectedUser != null && !string.IsNullOrWhiteSpace(SelectedUser.Id); } private void ExportPdf() { PdfExporter.ExportUsers(Users); } } } 

UserView:

<UserControl x:Class="WPF_CS_InventoryManager.Views.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPF_CS_InventoryManager.Views" xmlns:viewmodels="clr-namespace:WPF_CS_InventoryManager.ViewModels" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <viewmodels:UserViewModel x:Key="UserViewModel"/> </UserControl.Resources> <Grid> </Grid> </UserControl> 

Error in UserView:

enter image description here

New contributor
BFD03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
9
  • Please don't leave the default text “enter image description here”, always change it to some descriptive image title. Please don't post textual information in the form of pictures. Please see: Why should I not upload images of code/data/errors? If you still need an image, it can complement the textual information. Commented Nov 23 at 1:40
  • We would need a comprehensive issue report: exception information and the code throwing this exception. Please mark a code line where the exception is thrown with a comment and describe it. You may also need to provide some information of the exception propagation, so provide the same information for more than one stack frame known from the exception stack. And the sample code should be [minimal reproducible]((stackoverflow.com/help/minimal-reproducible-example)). Commented Nov 23 at 1:43
  • 1
    Thank you very much, this is my first time using this platform. Commented Nov 23 at 5:50
  • 1
    One more advice would be helpful for you: when you address a person, mention this person's registered name, prepend it with @. Even better, put this name in the beginning of the comment string. In this case, the one you address will get a notification and everyone will be able to see who are you talking to. For example, to use my name, you concatenate @ and Sergey A Kryukov. It is not needed when we address the author of the main post. In this case, this is you. Commented Nov 23 at 7:19
  • 1
    As a note, you should not initialize the SelectedUser property with new UserModel(). It should either be null or hold an element of the Users collection. You may for example write _selectedUser = _users.FirstOrDefault(); Commented Nov 23 at 13:05

1 Answer 1

1

From the code you provided, the error most likely occurs in this line _bd = new BD();. It is not clear from your explanation whether the error occurs only in development mode, or in runtime mode as well.
Just in case, I'll show you how to separate these modes so you can work in them differently.

 public class UserViewModel : BaseViewModel { private readonly BD _bd; private readonly ObservableCollection<UserModel> _users; private UserModel _selectedUser; private readonly bool isDemoMode; public UserViewModel(bool isDemoMode) { this.isDemoMode = isDemoMode; if (isDemoMode) { _users = new ObservableCollection<UserModel>() { new UserModel() {Firstname="Jon", Lastname="Snow"}, new UserModel() {Firstname="Arya", Lastname="Stark"} }; } else { _bd = new BD(); _users = new ObservableCollection<UserModel>(_bd.GetUsers()); } Users = new ReadOnlyObservableCollection<UserModel>(_users); _selectedUser = new UserModel(); } public UserViewModel() : this(true) { } public ReadOnlyObservableCollection<UserModel> Users { get; } public UserModel SelectedUser { get => _selectedUser; set { if (_selectedUser != value) { _selectedUser = value; OnPropertyChanged(nameof(SelectedUser)); } } } // ========== COMMANDS ========== public ICommand AddCommand => new RelayCommand(AddExecute, CanAddExecute); public ICommand EditCommand => new RelayCommand(EditExecute, CanEditOrDelete); public ICommand DeleteCommand => new RelayCommand(DeleteExecute, CanEditOrDelete); public ICommand ExportPdfCommand => new RelayCommand(_ => ExportPdf()); private void AddExecute(object? _) { if (isDemoMode) { _users.Add(SelectedUser); } else { _bd.AddUser(SelectedUser); _users.Clear(); _bd.GetUsers().ForEach(u => _users.Add(u)); } SelectedUser = new UserModel(); } private bool CanAddExecute(object? _) { // Aquí podrías validar campos requeridos return !string.IsNullOrWhiteSpace(SelectedUser?.Id); } private void EditExecute(object? _) { if (isDemoMode) { int index = _users.IndexOf(SelectedUser); _users[index] = SelectedUser; } else { _bd.EditUser(SelectedUser); _users.Clear(); _bd.GetUsers().ForEach(u => _users.Add(u)); } } private void DeleteExecute(object? _) { if (isDemoMode) { _users.Remove(SelectedUser); } else { _bd.DeleteUser(SelectedUser); _users.Clear(); _bd.GetUsers().ForEach(u => _users.Add(u)); } SelectedUser = new UserModel(); } private bool CanEditOrDelete(object? _) { return SelectedUser != null && !string.IsNullOrWhiteSpace(SelectedUser.Id); } private void ExportPdf() { PdfExporter.ExportUsers(Users); } } 
<UserControl x:Class="WPF_CS_InventoryManager.Views.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPF_CS_InventoryManager.Views" xmlns:viewmodels="clr-namespace:WPF_CS_InventoryManager.ViewModels" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <d:UserControl.Resources> <viewmodels:UserViewModel x:Key="UserViewModel"/> </d:UserControl.Resources> <Grid> </Grid> </UserControl> 
 public partial class UserControl1 : UserControl { public UserControl1() { DataContext = new UserViewModel(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)); InitializeComponent(); } } 

More generally, I advise against creating a DataContext in the UserControl itself.

It typically receives it when initializing an instance from a higher-level ancestor.

One way to organize this is to create all the necessary layers in the App when it starts (the Startup event), where the connections and dependencies between the layers are also created. After creation, the instance is either written to the App resources (often in a locator property) or passed to the DataContext of the required Windows.

In this case, you can set a breakpoint at App startup and then step through the code to check where and why the error occurs.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.