If I use a binding in code behind, getting an error after click at change IsBusy
"The calling thread cannot access this object because a different thread owns it"
xaml:
<Button x:Name="AsyncCommand"
Height="20"
Content="PushAsync"/>
<ProgressBar x:Name="IsBusy"
Height="20"/>
cs:
this.Bind(ViewModel, x => x.IsBusy, x => x.IsBusy.IsIndeterminate);
this.BindCommand(ViewModel, x => x.AsyncCommand, x => x.AsyncCommand);
viewmodel:
public class TestViewModel : ReactiveObject
{
public TestViewModel()
{
AsyncCommand = new ReactiveAsyncCommand();
AsyncCommand
.RegisterAsyncFunction(x =>
{ IsBusy = true; Thread.Sleep(3000); return "Ok"; })
.Subscribe(x => { IsBusy = false; });
}
private bool isBusy;
public bool IsBusy
{
get { return isBusy; }
set { this.RaiseAndSetIfChanged(x => x.IsBusy, ref isBusy, value); }
}
public ReactiveAsyncCommand AsyncCommand { get; protected set; }
}
But if I make a bind in xaml all works, like this:
cs:
DataContext = new TestViewModel();
xaml:
<Button x:Name="AsyncCommand"
Height="20"
Content="PushAsync"
Command="{Binding AsyncCommand}"/>
<ProgressBar x:Name="IsBusy"
Height="20"
IsIndeterminate="{Binding IsBusy}"/>
Why is this happening?
WhenAnyValue
? – Ashes