xaml - "Hello World",ReactiveUI、Xamarin Forms 和 XAML 锁定

标签 xaml xamarin.ios xamarin xamarin.android reactiveui

我正在尝试使用 Reactive UI 和 Xamarin Forms 创建一个 hello world。

我创建了一个 ViewModel(基于 ReactiveObject)、一个自定义页面(基于 ReactiveContentPage)和一些 XAML 标记。

该页面有一个条目和一个标签,绑定(bind)在一起。当我运行它时(在 iOS 和 Android 上),它似乎适用于键入的前几个字符,然后锁定。控制台发出“主线程上发生的事情太多”警告。

我错过了什么?

项目是here .

模型

namespace XamarinFormsReactiveUIExample
{
    public class MyPageModel : ReactiveObject
    {
        private string _userName = "";
        public string UserName {
            get { return _userName; }
            set { this.RaiseAndSetIfChanged (ref _userName, value); }
        }
    }
}

页面

namespace XamarinFormsReactiveUIExample
{
    public partial class MyPage : ReactiveContentPage<MyPageModel>
    {
        public MyPage ()
        {
            InitializeComponent ();
        }
    }
}

XAML

<?xml version="1.0" encoding="UTF-8"?>
<reactive:ReactiveContentPage 
x:TypeArguments="local:MyPageModel"
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="XamarinFormsReactiveUIExample.MyPage"
xmlns:local="clr-namespace:XamarinFormsReactiveUIExample;assembly=XamarinFormsReactiveUIExample"
xmlns:reactive="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms">
    <reactive:ReactiveContentPage.BindingContext>
      <local:MyPageModel />
    </reactive:ReactiveContentPage.BindingContext>

     <reactive:ReactiveContentPage.Padding>
     <OnPlatform x:TypeArguments="Thickness"
                iOS="0, 150, 0, 0" />
     </reactive:ReactiveContentPage.Padding>

    <reactive:ReactiveContentPage.Content>
        <StackLayout>
            <Entry Text = "{Binding UserName}"></Entry>
            <Label Text = "{Binding UserName}"></Label>
        </StackLayout>
    </reactive:ReactiveContentPage.Content>
</reactive:ReactiveContentPage>

最佳答案

我不明白这怎么可能只是 Xamarin 错误,ReactiveUI 在这里什么都没有。无论如何,您始终可以尝试以 RxUI 方式编写这些绑定(bind):

<Entry x:Name="userNameEntry" />
<Label x:Name="userNameLabel" />

然后,在构造函数中:

public MyPage ()
{
    InitializeComponent();
    this.ViewModel = new MyPageModel();

    this.Bind(ViewModel, vm => vm.UserName, v => v.userNameEntry.Text);
    this.OneWayBind(ViewModel, vm => vm.UserName, v => v.userNameLabel.Text);
}

关于xaml - "Hello World",ReactiveUI、Xamarin Forms 和 XAML 锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25529950/

相关文章:

azure - Xamarin 的 WindowsAzure.Storage 库是否使用 NSUrlSession 进行文件上传?

c# - WPF自定义控件库图片资源

c# - Windows Phone 8.0如何切换语言

iphone - 使用 NSData.FromBytes 的 Monotouch

ios - 单点触控 : Symbolicate Crash

c# - 通过 URL 加载图像在 Xamarin Forms 中不起作用

Silverlight 布局最佳实践

c# - Visual Studio 2010 中的虚假 WPF/XAML 错误

ios - 获取 UISwitch 状态 -Xamarin

c# - int.Parse 负数导致异常