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:

@. 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@andSergey A Kryukov. It is not needed when we address the author of the main post. In this case, this is you.new UserModel(). It should either benullor hold an element of theUserscollection. You may for example write_selectedUser = _users.FirstOrDefault();