Пример #1
0
        public App()
        {
            var content = new ContentPage ();

            var stream = this.GetType ().GetTypeInfo ().Assembly.GetManifestResourceStream ("DynamicFormsDemo.Main.xaml");
            var xaml = new StreamReader (stream).ReadToEnd ();
            content.LoadFromXaml (xaml);

            stream = this.GetType ().GetTypeInfo ().Assembly.GetManifestResourceStream ("DynamicFormsDemo.Main.json");
            var json = new StreamReader (stream).ReadToEnd ();
            var model = JModel.Parse (json);

            content.BindingContext = model;

            MainPage = content;
        }
        public void when_loading_xaml_then_sets_bound_data()
        {
            // Get Xaml from somewhere (i.e. VS)
            var xaml = @"<ContentPage xmlns='http://xamarin.com/schemas/2014/forms'
             xmlns:x='http://schemas.microsoft.com/winfx/2009/xaml'>
            <Label Text='{Binding Title, Mode=TwoWay}' VerticalOptions='Center' HorizontalOptions='Center' />
            </ContentPage> ";

            var model = new DictionaryModel(new Dictionary<string, object>
            {
                { "Title", "Bar" }
            });

            // Create empty container page for the XAML
            //		TODO: ensure the root element in the Xaml is actually a ContentPage?
            var view = new ContentPage ();

            // Set binding context to dummy data
            view.BindingContext = model;

            // Load the Xaml into the View
            view.LoadFromXaml (xaml);

            // Grab the new label control added to the view via Xaml
            var label = view.Content as Label;
            Assert.NotNull (label);

            // UI properly bound to underlying model!
            Assert.Equal ("Bar", label.Text);

            // Change the label text via the control
            label.Text = "Foo";

            // Underlying model is updated!
            Assert.Equal ("Foo", model["Title"]);

            // Change the underlying model is
            model["Title"] = "Bar";

            // UI label properly reflecting underlying model change!
            Assert.Equal("Bar", label.Text);
        }
        public void when_loading_xaml_then_sets_bound_data_to_complex_model()
        {
            // Get Xaml from somewhere (i.e. VS)
            var xaml = @"<ContentPage xmlns='http://xamarin.com/schemas/2014/forms'
             xmlns:x='http://schemas.microsoft.com/winfx/2009/xaml'>
            <Label Text='{Binding Address.Phone.Prefix, Mode=TwoWay}' VerticalOptions='Center' HorizontalOptions='Center' />
            </ContentPage> ";

            // Get dummy data from somewhere (i.e. VS)
            var model = new DictionaryModel(new Dictionary<string, object>
            {
                { "Name", "Xamarin" },
                { "Address", new Dictionary<string, object>
                    {
                        {  "Street", "Camila" },
                        {  "Phone", new Dictionary<string, object>
                            {
                                { "Prefix", "+54" },
                                { "Area", "11" },
                            }
                        },
                    }
                }
            });

            // Create empty container page for the XAML
            //		TODO: ensure the root element in the Xaml is actually a ContentPage?
            var view = new ContentPage ();

            // Set binding context to dummy data
            view.BindingContext = model;

            // Load the Xaml into the View
            //		TODO: this method is internal in XF.Xaml
            view.LoadFromXaml(xaml);

            // Grab the new label control added to the view via Xaml
            var label = view.Content as Label;
            Assert.NotNull (label);

            // UI properly bound to underlying model!
            Assert.Equal ("+54", label.Text);

            // Change the label text via the control
            label.Text = "+1";

            // Underlying model is updated!
            dynamic data = model;

            Assert.NotNull ((object)data.Address);
            Assert.NotNull ((object)data.Address.Phone);
            Assert.NotNull ((object)data.Address.Phone.Prefix);
            Assert.Equal ("+1", (string)data.Address.Phone.Prefix);

            // Change underlying data model
            data.Address.Phone.Prefix = "+54";

            Assert.Equal ("+54", label.Text);
        }