Я использую ReactiveUI в проекте Xamarin.Forms, но когда я нажимаю кнопку, я получаю ошибку: «Только исходный поток, создавший иерархию представлений, может касаться его представлений». «Вот мой код.
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
AppShell.xaml.cs
public partial class AppShell : Shell
{
Random rand = new Random();
Dictionary<string, Type> routes = new Dictionary<string, Type>();
public Dictionary<string, Type> Routes { get { return routes; } }
public AppShell()
{
InitializeComponent();
RegisterRoutes();
BindingContext = this;
}
void RegisterRoutes()
{
routes.Add("monkeydetails", typeof(HomeView));
foreach (var item in routes)
{
Routing.RegisterRoute(item.Key, item.Value);
}
}
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel any back navigation
//if (e.Source == ShellNavigationSource.Pop)
//{
// e.Cancel();
//}
}
void OnNavigated(object sender, ShellNavigatedEventArgs e)
{
}
}
По умолчанию вы переходите к представлению HomeView
HomeView.xaml
<Button Text="Pulsar" x:Name="Boton"></Button>
HomeView.xaml.cs
public partial class HomeView : ReactiveContentPage<HomeViewModel>
{
protected CompositeDisposable ControlBindings { get; } = new CompositeDisposable();
public ReactiveCommand<Unit, Unit> Navigate { get; private set; }
public HomeView()
{
InitializeComponent();
this.ViewModel = new HomeViewModel();
this.BindCommand(ViewModel, vm => vm.Navigate, view => view.Boton);
}
}
HomeViewModel.cs
public class HomeViewModel : ViewModelBase, IRoutableViewModel
{
int prueb = 0;
public HomeViewModel()
{
Navigate = ReactiveCommand.CreateFromTask(async() =>
{
await hola();
});
}
public async Task hola()
{
}
public string prueba()
{
return prueb.ToString();
}
public IObservable<string> NumberStream { get; }
public string UrlPathSegment => "Number Stream Page";
public IScreen HostScreen { get; }
public override string Id => "Pass Parameter";
public ReactiveCommand<Unit, Unit> Navigate { get; private set; }
}
Не могу понять, почему появляется ошибка при нажатии кнопки