How to pass parameters C# -> XAML -> Skiasharp controls

f xr

New member
Joined
Jun 25, 2022
Messages
3
Programming Experience
Beginner
Hello everyone!
I have problem with transmit parameter via XAML between C# code and Controls (Skiasharp).
When I make change control parameters in XAML from C# code, I don't get them on the controls.
Please help me understand how to pass parameters correctly C# -> XAML -> Skiasharp controls.

SensorPage.xaml:
<ContentPage x:Class="MobApp.Client.SensorCheck"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:controls="clr-namespace:MobApp.Client.Controls"
             NavigationPage.HasNavigationBar="false"
             Visual="Material"
             mc:Ignorable="d">

    <StackLayout Orientation="Vertical">
        <controls:SensorCheck Grid.Row="1"
                        x:Name="Sensor"
                        VerticalOptions="CenterAndExpand"
                        SensorState="WAIT"
        />
    </StackLayout>
</ContentPage>

SensorPage.xaml.cs:
namespace MobApp.Client
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SensorCheck : ContentPage
    {
        public SensorCheck()
        {
            InitializeComponent();
            Sensor.BindingContext = this;
            SensorState = "READY";
        }
    }
   
        #region Properties
        public static readonly BindableProperty SensorStateProperty =
            BindableProperty.Create("SensorState", typeof(string), typeof(SensorCheck), "");

        public string SensorState
        {
            get { return (string)GetValue(SensorStateProperty); }
            set { SetValue(SensorStateProperty, value); }
        }
        #endregion

SensorCheck.cs (Skiasharp):
namespace MobApp.Client.Controls

{
    public class SensorCheck : SKCanvasView
    {
        public SensorCheck()
        {
         }

        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
               var val = SensorState;
               // some code....
        }
       
        #region Properties
        public static readonly BindableProperty SensorStateProperty =
        BindableProperty.Create("SensorState", typeof(string), typeof(SensorCheck), "");

        public string SensorState
        {
            get { return (string)GetValue(SensorStateProperty); }
            set { SetValue(SensorStateProperty, value); }
        }
        #endregion
       
        protected override void OnPropertyChanged(string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);
            if (propertyName == SensorStateProperty.PropertyName)
            {
                InvalidateSurface();
            }
        }
    }
}
 
You cannot pass parameters from C# code to XAML at runtime. XAML is static declarative text. If doesn't get executed so you can't pass it anything. Now, as for the UI elements/controls that are generated from that XAML, those can be manipulated at runtime. In general, though, the way data is transmitted from C# objects to tjr UI is via data binding, rather than direct manipulation. (Under the covers, the data binding will eventually do direct manipulation, though.)
 
Back
Top Bottom