"Hello World" with ReactiveUI, Xamarin Forms and XAML blocking

I am trying to make the world hello with Reactive UI and Xamarin Forms.

I created a ViewModel (based on ReactiveObject), a custom page (based on ReactiveContentPage) and some XAML markup.

The page has a post and a shortcut linked together. When I run it (on iOS and Android) it seems to work to enter the first few characters and then it blocks. The console gives a warning "too much on the main thread".

What am I missing?

The project is here .

Model

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

      

Page

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>

      

+3


source to share


2 answers


I don't see how it could be something like a Xamarin bug, ReactiveUI doesn't do anything here. In any case, you can always try to write these bindings in RxUI way:

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

      



Then in the constructor:

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);
}

      

+6


source


The only thing I didn't see was that the ViewModel was never created and the binding context was never set. I don't see what your old code did before you changed it to the new style. Personally, I try to avoid using code as much as possible. Thus, you should be able to change your constructor to something like

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

      



And go back to using XAML binding.

0


source







All Articles