Мне нужны пояснения / помощь по двусторонней привязке в Xamarin с базой данных Firebase. У меня есть дочерние «Параметры» Firebase (без ключа), которые были добавлены с помощью PUT. Я храню здесь параметры, которые мое приложение Xamarin или веб-перехватчики микроконтроллера частиц читают и записывают во время работы моего приложения. Поскольку мой контроллер частиц обновляет эти элементы, в любое время мне нужно «Firebase -> привязка клиента для работы для обновления моего приложения Xamarin (свойства ViewParameterModel) в реальном времени. Как« привязать »к дочернему элементу с помощью нет КЛЮЧА, возможно ли просто привязать к дочерней ветке? вот мой код, если это поможет ... У меня Client-Firebase отлично работает в моем коде, но мне нужен двусторонний, чтобы это работало. Любое помощь будет принята с благодарностью.
Мой Xamarin: FirebaseHelper.cs
using Firebase.Database;
using Firebase.Database.Query;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace Chart_sample.Services
{
public class FirebaseHelper
{
public ViewParameterModel ViewParameterModel { get; set; }
public FirebaseHelper()
{
this.ViewParameterModel = new ViewParameterModel();
}
FirebaseClient firebase = new FirebaseClient("https://yourrealtimedatabasename_here.firebaseio.com/");
private readonly string ChildParams = "ControllerData/Pellet_Pirate_1/Parameters";
public void GetParameters()
{
firebase.Child(ChildParams).AsObservable<ViewParameterModel>().Subscribe(snapp =>
{
// Console.WriteLine("F.I.A. is Fan: " + snapp.Object.fan + " Igniter: " + snapp.Object.ign + " Auger: " + snapp.Object.aug + " Mode: " + snapp.Object.mode);
ViewParameterModel.aug = snapp.Object.aug;
ViewParameterModel.fan = snapp.Object.fan;
ViewParameterModel.ign = snapp.Object.ign;
ViewParameterModel.mode = snapp.Object.mode;
ViewParameterModel.target = snapp.Object.target;
});
}
public async Task UpdateFIA()
{
await firebase.Child(ChildParams).PatchAsync(ViewParameterModel);
}
}
}
Мой Xamarin: ViewParameterModel.cs
using System.ComponentModel;
namespace Chart_sample
{
public class ViewParameterModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool Fan = false;
public bool fan
{
get
{
return Fan;
}
set
{
if (Fan != value)
{
Fan = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("fan"));
}
}
}
private bool Igniter = false;
public bool ign
{
get
{
return Igniter;
}
set
{
if (Igniter != value)
{
Igniter = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ign"));
}
}
}
private bool Auger = false;
public bool aug
{
get
{
return Auger;
}
set
{
if (Auger != value)
{
Auger = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("aug"));
}
}
}
private int Target = 200;
public int target
{
get
{
return Target;
}
set
{
if (Target != value)
{
Target = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("target"));
}
}
}
}
}
Мой Xamarin: CookPage.xaml.cs
using Chart_sample.Services;
using Syncfusion.XForms.Buttons;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Chart_sample.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CookPage : ContentPage
{
FirebaseHelper firebaseHelper = new FirebaseHelper();
public CookPage()
{
InitializeComponent();
this.BindingContext = firebaseHelper;
// for Syncfusion SfSwitch pre-populate observable for the F.I.A. toggles and target temp
firebaseHelper.GetParameters();
}
private async void SfSwitchFan_StateChanged(object sender, EventArgs e)
{
SfSwitch sfSwitch = (sender as SfSwitch);
string automationId = sfSwitch.AutomationId;
firebaseHelper.ViewParameterModel.fan = (bool)sfSwitch.IsOn;
await firebaseHelper.UpdateFIA();
}
private async void SfSwitchIgn_StateChanged(object sender, EventArgs e)
{
SfSwitch sfSwitch = (sender as SfSwitch);
string automationId = sfSwitch.AutomationId;
firebaseHelper.ViewParameterModel.ign = (bool)sfSwitch.IsOn;
await firebaseHelper.UpdateFIA();
}
private async void SfSwitchAug_StateChanged(object sender, EventArgs e)
{
SfSwitch sfSwitch = (sender as SfSwitch);
string automationId = sfSwitch.AutomationId;
firebaseHelper.ViewParameterModel.aug = (bool)sfSwitch.IsOn;
// await DisplayAlert(automationId, "Auger Current State : " + (sender as SfSwitch).IsOn, "Ok");
await firebaseHelper.UpdateFIA();
}
private async void Target_Changed(object sender, Syncfusion.SfNumericTextBox.XForms.ValueEventArgs e) // user changed the Cook Mode Setting.
{
string Target = e.Value.ToString();
int targetInt = Int32.Parse(Target);
firebaseHelper.ViewParameterModel.target = targetInt;
await firebaseHelper.UpdateFIA();
}
}
}
Мой Xamarin: CookPage.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"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:syncfusion="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
xmlns:syncNumeric="clr-namespace:Syncfusion.SfNumericTextBox.XForms;assembly=Syncfusion.SfNumericTextBox.XForms"
xmlns:graphics="clr-namespace:Syncfusion.XForms.Graphics;assembly=Syncfusion.Core.XForms"
mc:Ignorable="d"
x:Class="Chart_sample.Views.CookPage">
<StackLayout>
<Label Padding="5" Text="Cook Control" FontAttributes="Bold" FontSize="Title" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
<Grid Padding="10">
<Label Padding="5" Margin="0,0,0,30" Text="Target Temp" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
<syncNumeric:SfNumericTextBox Margin="0,30,0,0" x:Name="targetTextBox" MaximumNumberDecimalDigits="0" FontSize="30" Maximum="450" Minimum="175"
Value="{Binding ViewParameterModel.target, Mode=TwoWay}"
TextColor="Green"
AllowNull="False" Watermark="Target Temp" FormatString="n" TextAlignment="Center" ValueChanged="Target_Changed"/>
</Grid>
<Grid VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Margin="10,5,5,5" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto">
</RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="10*" />
</Grid.ColumnDefinitions>
<syncfusion:SfSwitch x:Name="SfFanSwitch" IsOn="{Binding ViewParameterModel.fan, Mode=TwoWay}" StateChanged="SfSwitchFan_StateChanged" VisualType="Custom" Grid.Row="0" Grid.Column="0" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="On">
<VisualState.Setters>
<Setter Property="SwitchSettings">
<Setter.Value>
<syncfusion:DefaultSwitchSettings x:TypeArguments="syncfusion:OnState"
ThumbImageSource="Fan-On.png" TrackCornerRadius="50" ThumbCornerRadius="50" ThumbHeightRequest="60" ThumbWidthRequest="60"
TrackHeightRequest="45" TrackWidthRequest="100" ThumbBorderWidth="0" >
<syncfusion:DefaultSwitchSettings.ThumbGradient>
<graphics:SfLinearGradientBrush>
<graphics:SfLinearGradientBrush.GradientStops>
<graphics:GradientStopCollection>
<graphics:SfGradientStop Color="LightGray" Offset="0"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="SteelBlue" Offset="0.5"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="LightGray" Offset="1"></graphics:SfGradientStop>
</graphics:GradientStopCollection>
</graphics:SfLinearGradientBrush.GradientStops>
</graphics:SfLinearGradientBrush>
</syncfusion:DefaultSwitchSettings.ThumbGradient>
<syncfusion:DefaultSwitchSettings.TrackGradient>
<graphics:SfLinearGradientBrush>
<graphics:SfLinearGradientBrush.GradientStops>
<graphics:GradientStopCollection>
<graphics:SfGradientStop Color="LightBlue" Offset="0"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="Blue" Offset="0.25"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="Blue" Offset="0.5"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="SteelBlue" Offset="0.75"></graphics:SfGradientStop>
</graphics:GradientStopCollection>
</graphics:SfLinearGradientBrush.GradientStops>
</graphics:SfLinearGradientBrush>
</syncfusion:DefaultSwitchSettings.TrackGradient>
</syncfusion:DefaultSwitchSettings>
</Setter.Value>
</Setter>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Off">
<VisualState.Setters>
<Setter Property="SwitchSettings">
<Setter.Value>
<syncfusion:DefaultSwitchSettings x:TypeArguments="syncfusion:OffState"
TrackCornerRadius="50" ThumbCornerRadius="50" ThumbHeightRequest="60" ThumbWidthRequest="60"
ThumbImageSource="Fan-Off.png" ThumbBorderWidth="0" TrackHeightRequest="45" TrackWidthRequest="100" >
<syncfusion:DefaultSwitchSettings.ThumbGradient>
<graphics:SfLinearGradientBrush>
<graphics:SfLinearGradientBrush.GradientStops>
<graphics:GradientStopCollection>
<graphics:SfGradientStop Color="LightGray" Offset="0"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="DarkGray" Offset="0.5"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="LightGray" Offset="1"></graphics:SfGradientStop>
</graphics:GradientStopCollection>
</graphics:SfLinearGradientBrush.GradientStops>
</graphics:SfLinearGradientBrush>
</syncfusion:DefaultSwitchSettings.ThumbGradient>
<syncfusion:DefaultSwitchSettings.TrackGradient>
<graphics:SfLinearGradientBrush>
<graphics:SfLinearGradientBrush.GradientStops>
<graphics:GradientStopCollection>
<graphics:SfGradientStop Color="LightBlue" Offset="0"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="Gray" Offset="0.25"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="LightGray" Offset="0.5"></graphics:SfGradientStop>
<graphics:SfGradientStop Color="DarkGray" Offset="0.75"></graphics:SfGradientStop>
</graphics:GradientStopCollection>
</graphics:SfLinearGradientBrush.GradientStops>
</graphics:SfLinearGradientBrush>
</syncfusion:DefaultSwitchSettings.TrackGradient>
</syncfusion:DefaultSwitchSettings>
</Setter.Value>
</Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</syncfusion:SfSwitch>
<!-- CODE Truncated here... just 1 of 3 switches shown... -->