When trying to add a new user, this error appears.
<Window x:Class="ListUsers.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ListUsers"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition> </ColumnDefinition>
<ColumnDefinition> </ColumnDefinition>
<ColumnDefinition> </ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid
Grid.ColumnSpan="4"
ItemsSource="{Binding Users}"
AutoGenerateColumns="False"
IsReadOnly="True"
CanUserDeleteRows="False"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Имя" Binding="{Binding Name}" />
<DataGridTextColumn Header="Фамилия" Binding="{Binding SecondName}" />
<DataGridTextColumn Header="Адрес электронной почты" Binding="{Binding Email}" />
<DataGridTextColumn Header="Телефон" Binding="{Binding Phone}" />
</DataGrid.Columns>
</DataGrid>
<Button
Height="25"
Width="150"
Margin="10"
Grid.Row="2"
Grid.Column="0"
Content="Add user"
Command="{Binding AddUserCommand}" />
</Grid>
</Window>
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
using ListUsers.Models;
using Newtonsoft.Json;
using ReactiveUI;
namespace ListUsers.ViewModels
{
public class UserViewModel: ReactiveObject
{
private const string path = @"C:\Users\dimab\RiderProjects\ListUsers\users.json";
private ObservableCollection<User> _users;
public ObservableCollection<User> Users
{
get => _users;
set => this.RaiseAndSetIfChanged(ref _users, value);
}
private User _selectedUser;
public User SelectedUser
{
get => _selectedUser;
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
}
public UserViewModel()
{
string json = File.ReadAllText(path);
Users = JsonConvert.DeserializeObject<ObservableCollection<User>>(json);
AddUserCommand = ReactiveCommand.Create(AddUser);
}
public ReactiveCommand<Unit, Unit> AddUserCommand { get; }
public ReactiveCommand<User, Unit> EditUserCommand { get; }
public ReactiveCommand<User, Unit> DeleteUserCommand { get; }
public void AddUser()
{
Users.Add(new User());
}
}
}
I am working on this task and encountered such an error.
Main requirements:
The application should be developed using WPF and ReactiveUI. The list of users should be displayed as a table containing the following fields: 2.1. First name (string) 2.2. Last name (string) 2.3. Email address (string) 2.4. Phone (string) It should be possible to add new users to the list. It should be possible to edit and delete users from the list. Sorting of the list by first name, last name, email address, and phone should be implemented. Filtering of the list by first name, last name, email address, and phone should be implemented. All changes to the list of users should be saved to a file when the application exits and loaded when it is started again. Additional requirements:
Add validation when adding a new user to the list. It is necessary to check whether the user's email address contains the word "spam". If the word "spam" is found in the email address, a warning should be issued to the user and the user should not be added to the list. The list of users should allow drag-and-drop of rows to change their order. This functionality should be implemented using ReactiveUI and without using standard WPF tools for dragging rows in a table.
I tried using Dispatcher.Invoke but it didn't help. This is my first time working with WPF and ReactiveUI. Can you please give me some guidance?
Upd: Stack
System.InvalidOperationException: Вызывающий поток не может получить доступ к данному объекту, так как владельцем этого объекта является другой поток.
в System.Windows.Threading.Dispatcher.VerifyAccess()
в System.Windows.DependencyObject.GetValue(DependencyProperty dp)
в System.Windows.Controls.Primitives.ButtonBase.get_Command()
в System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
в System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged(Object sender, EventArgs e)
в System.Windows.Input.CanExecuteChangedEventManager.HandlerSink.OnCanExecuteChanged(Object sender, EventArgs e)
в ReactiveUI.ReactiveCommandBase`2.OnCanExecuteChanged(Boolean newValue) в /_/src/ReactiveUI/ReactiveCommand/ReactiveCommandBase.cs:строка 219
в System.Reactive.AnonymousSafeObserver`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:строка 43
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.IdentitySink`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Internal/IdentitySink.cs:строка 15
в System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 903
в System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive() в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 765
в System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 270
в System.Reactive.Subjects.ReplaySubject`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 161
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.IdentitySink`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Internal/IdentitySink.cs:строка 15
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value) в /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/DistinctUntilChanged.cs:строка 72
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.Linq.ObservableImpl.CombineLatest`3._.SecondObserver.OnNext(TSecond value) в /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/CombineLatest.cs:строка 177
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.IdentitySink`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Internal/IdentitySink.cs:строка 15
в System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 903
в System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive() в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 765
в System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 270
в System.Reactive.Subjects.ReplaySubject`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Subjects/ReplaySubject.cs:строка 161
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.IdentitySink`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Internal/IdentitySink.cs:строка 15
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value) в /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/DistinctUntilChanged.cs:строка 72
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.IdentitySink`1.OnNext(T value) в /_/Rx.NET/Source/src/System.Reactive/Internal/IdentitySink.cs:строка 15
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) в /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:строка 47
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.Linq.ObservableImpl.Scan`2._.OnNext(TSource value) в /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Scan.cs:строка 48
в System.Reactive.SafeObserver`1.WrappingSafeObserver.OnNext(TSource value) в /_/Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs:строка 30
в System.Reactive.Sink`1.ForwardOnNext(TTarget value) в /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:строка 49
в System.Reactive.ObserveOnObserverLongRunning`1.Drain() в /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:строка 732
в System.Reactive.ObserveOnObserverLongRunning`1.<>c.<.cctor>b__17_0(ObserveOnObserverLongRunning`1 self, ICancelable cancelable) в /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:строка 680
в System.Reactive.Concurrency.DefaultScheduler.LongRunning.LongScheduledWorkItem`1.<>c.<.ctor>b__3_0(Object thisObject) в /_/Rx.NET/Source/src/System.Reactive/Concurrency/DefaultScheduler.cs:строка 181
в System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.<>c.<StartThread>b__8_0(Object itemObject) в /_/Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs:строка 75
в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Threading.ThreadHelper.ThreadStart(Object obj)