Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Howto convert from C# code to XAML

$
0
0

Hi,

I am new to Xamarin and implemented some examples in "pure" c# code.
Now I am trying to "convert" my code to XAML.

I have a mainpage that passes an object containing data to several detailpages.
This is done by passing the object to the constructor of the detailpage.
The detailpage contains a grid with two rows.
There I have a slider that changes the "Value"-property of my object and
a label that visualizes the current value.

Here is my model class:

public class ObservableSensorClass : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    SensorClass sensor;

    public ObservableSensorClass() {
        sensor = new SensorClass();
    }


    public string Value {
        set {
            if (!value.Equals(sensor.Value, StringComparison.Ordinal)) {
                sensor.Value = value;
                OnPropertyChanged("Value");
            }
        }
        get {
            return sensor.Value;
        }
    }

    void OnPropertyChanged([CallerMemberName] string propertyName = null) {
        var handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Here is the c# Version of the detailpage:

public SensorValuesDetailPage(ObservableSensorClass sensor) {
    this.Title = "Werte";
    Grid grid = new Grid {
        VerticalOptions = LayoutOptions.FillAndExpand,
        RowDefinitions =  { new RowDefinition { Height = GridLength.Auto }, new RowDefinition { Height = GridLength.Auto } },
        ColumnDefinitions = { new ColumnDefinition { Width = GridLength.Auto } }
    };

    this.BindingContext = sensor;

    Slider slider = new Slider { Minimum = 0, Maximum = 100, Value = Int32.Parse(sensor.Value),
        VerticalOptions = LayoutOptions.CenterAndExpand, WidthRequest = 300  };

    slider.ValueChanged += (sender, e) => {
        sensor.Value = e.NewValue.ToString();
    };

    grid.Children.Add(slider,1 , 1);

    Label valueLabel = new Label { Text = sensor.Value };

    valueLabel.SetBinding(Label.TextProperty, "Value");
    grid.Children.Add(valueLabel, 1, 2);
    Content = grid;
}

And here is the XAML-Version:

CS:

public partial class SensorValueDetailPage : ContentPage {
        ObservableSensorClass sensor;
        public SensorValueDetailPage(ObservableSensorClass sensor) {
            this.sensor = sensor;
            InitializeComponent();
        }

        void SliderValueChanged(object sender, ValueChangedEventArgs e) {
            sensor.Value = e.NewValue.ToString();
        }
  }

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="UIDesign.SensorValueDetailPage" BindingContext="sensor" Title="Values">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Slider x:Name="slider" Minimum="0" Maximum="100" Value="0" VerticalOptions="CenterAndExpand" ValueChanged="SliderValueChanged" WidthRequest="300" Grid.Row="1" Grid.Column="0"></Slider>
    <Label Text="{Binding Value}" VerticalOptions="Center" HorizontalOptions="Center" Grid.Row="2" Grid.Column="0"/>


  </Grid>
</ContentPage>

The problem I have is the Binding. How can I bind the sensor object from the constructor in the XAML-part? "{Binding Value}" is not working.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>