C# 使用reactiveui按按钮xamarin.forms时出错
我正在Xamarin.Forms项目中使用ReactiveUI,但当我按下一个按钮时,我得到一个错误:“只有创建视图层次结构的原始线程才能接触它的视图。” 这是我的密码 App.xaml.csC# 使用reactiveui按按钮xamarin.forms时出错,c#,xaml,xamarin,xamarin.forms,reactiveui,C#,Xaml,Xamarin,Xamarin.forms,Reactiveui,我正在Xamarin.Forms项目中使用ReactiveUI,但当我按下一个按钮时,我得到一个错误:“只有创建视图层次结构的原始线程才能接触它的视图。” 这是我的密码 App.xaml.cs public App() { InitializeComponent(); MainPage = new AppShell(); } AppShell.xaml.cs public partial class AppShell : Shell { Random
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)
{
}
}
公共部分类AppShell:Shell
{
Random rand=新的Random();
字典路由=新字典();
公共字典路由{get{return Routes;}}
公共AppShell()
{
初始化组件();
注册表项();
BindingContext=这个;
}
无效注册表项()
{
添加(“monkeydetails”,typeof(HomeView));
foreach(路由中的var项)
{
路由.RegisterRoute(item.Key,item.Value);
}
}
导航时无效(对象发送方,ShellNavigatingEventArgs e)
{
//取消任何向后导航
//if(e.Source==ShellNavigationSource.Pop)
//{
//e.取消();
//}
}
已导航无效(对象发送方,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);
}
}
public部分类HomeView:ReactiveContentPage
{
受保护的CompositeDisposable控件绑定{get;}=new CompositeDisposable();
公共反应命令导航{get;private set;}
公共HomeView()
{
初始化组件();
this.ViewModel=新的HomeViewModel();
这个.bind命令(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; }
}
公共类HomeViewModel:ViewModelBase、IRoutableViewModel
{
int-prueb=0;
公共HomeViewModel()
{
Navigate=ReactiveCommand.CreateFromTask(异步()=>
{
等待hola();
});
}
公共异步任务hola()
{
}
公共字符串prueba()
{
返回prueb.ToString();
}
public IObservable NumberStream{get;}
公共字符串UrlPathSegment=>“数字流页面”;
公共IScreen主机屏幕{get;}
公共重写字符串Id=>“传递参数”;
公共反应命令导航{get;private set;}
}
我不明白为什么按下按钮时会出现错误
查看您的代码,我看不出收到此错误的任何原因。在Android中,当您尝试从UI线程以外的其他线程更新UI时,会出现此错误。当您调用
ReactiveCommand
时,您没有在.pinedax内执行任何操作。我要去度假,你可以独自去度假。阿霍拉·比恩,你知道这是一个9.x y se arreglo版本,