我是Xamarin Forms的新手,我正在一个系统上制作一个应用程序来管理汽车维修服务。到目前为止,我可以保存,修改和显示数据,当我保存新车辆时,我的视图向我显示它们并更新以显示车辆,但当我想编辑车辆时,我可以编辑它们,更改显示在Firebase中,但我的观点仍然是一样的,直到我关闭并再次打开应用程序,我如何才能使这些变化可见?任何帮助将衷心感谢
我看来
<?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="CMP.Vistas.Vehiculos"
NavigationPage.HasNavigationBar="False"
BackgroundColor="White"
Visual="Material">
<StackLayout>
<Frame BackgroundColor="#D48593"
HorizontalOptions="FillAndExpand"
HeightRequest="64"
Margin="0"
Padding="0"
HasShadow="False"
CornerRadius="0">
<Label Text="CalMaP"
HorizontalOptions="End"
VerticalOptions="Center"
FontSize="23"
FontAttributes="Bold"
Margin="0,0,40,0"
TextColor="White" />
</Frame>
<Grid RowDefinitions="*">
<RefreshView RefreshColor="#D48593"
Refreshing="RefreshView_Refreshing"
x:Name="RefreshCar">
<ScrollView>
<StackLayout x:Name="ContainerVehiculos"
VerticalOptions="Start"
Margin="10,12,10,0"
Orientation="Vertical"
Spacing="20">
</StackLayout>
</ScrollView>
</RefreshView>
<ImageButton Source="Iconpause.png"
VerticalOptions="EndAndExpand"
HorizontalOptions="EndAndExpand"
HeightRequest="60"
WidthRequest="60"
Margin="0,0,30,30"
CornerRadius="100"
BackgroundColor="White"
Command="{Binding NavAddVehiculoCommand}" />
</Grid>
</StackLayout>
</ContentPage>
字符串
我的视图C#
using CMP.VistaModelo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CMP.Vistas
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Vehiculos : ContentPage
{
VMVehiculos vm;
public Vehiculos()
{
InitializeComponent();
vm = new VMVehiculos(Navigation, ContainerVehiculos);
BindingContext = vm;
Appearing += Vehiculos_Appearing;
}
private async void Vehiculos_Appearing(object sender, EventArgs e)
{
await vm.Mostrarvehiculos(ContainerVehiculos);
}
}
}
型
我的viewModel:
using CMP.Datos;
using CMP.Modelo;
using CMP.Servicios;
using CMP.VistaModelo.Formularios;
using CMP.Vistas;
using CMP.Vistas.Formularios;
using Firebase.Database.Query;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace CMP.VistaModelo
{
public class VMVehiculos : BaseViewModel
{
#region VARIABLES
Dvehiculos funcion = new Dvehiculos();
List<MVehiculos> _ListaVehiculos;
private ObservableCollection<MVehiculos> _MVehiculos;
#endregion
#region CONSTRUCTOR
public VMVehiculos(INavigation navigation, StackLayout Contenedor)
{
Navigation = navigation;
Mostrarvehiculos(Contenedor);
}
public VMVehiculos()
{
}
#endregion
#region OBJETOS
public List<MVehiculos> ListaVehiculos
{
get { return _ListaVehiculos; }
set { SetValue(ref _ListaVehiculos, value); }
}
#endregion
#region PROCESOS
public void DibujarVehiculos(MVehiculos Item, StackLayout Contenedor)
{
var carril = Contenedor;
var frame = new Frame
{
CornerRadius = 15,
BackgroundColor = Color.FromRgb(240, 220, 230),
BorderColor = Color.FromHex("#EEEDED"),
HasShadow = false,
};
var grid = new Grid
{
ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition{ Width = new GridLength(50) },
new ColumnDefinition{ Width = new GridLength(150) },
new ColumnDefinition{ Width = new GridLength(83) },
},
HorizontalOptions = LayoutOptions.FillAndExpand,
};
var image = new Image
{
Source = Item.Icono,
HeightRequest = 50,
WidthRequest = 50,
Margin = new Thickness(0, 0, 6, 0),
HorizontalOptions = LayoutOptions.Start,
};
Grid.SetRowSpan(image, 2);
Grid.SetColumn(image, 0);
var labelNombre = new Label
{
Text = Item.Nombre,
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
FontAttributes = FontAttributes.Bold,
};
Grid.SetRowSpan(labelNombre, 2);
Grid.SetColumn(labelNombre, 1);
var labelKilometraje = new Label
{
Text = Item.Kilomtraje.ToString(),
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
FontSize = 15,
};
Grid.SetRow(labelKilometraje, 0);
Grid.SetColumn(labelKilometraje, 2);
var labelEstado = new Label
{
Text = Item.Estado,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
FontSize = 15,
TextColor = Color.FromHex(_Color(Item.Estado))
};
Grid.SetRow(labelEstado, 1);
Grid.SetColumn(labelEstado, 2);
grid.Children.Add(image);
grid.Children.Add(labelNombre);
grid.Children.Add(labelKilometraje);
grid.Children.Add(labelEstado);
frame.Content = grid;
var tap = new TapGestureRecognizer();
tap.Tapped += async (Object sender, EventArgs e) =>
{
await Navigation.PushAsync(new DataVehiculo(Item));
};
grid.GestureRecognizers.Add(tap);
carril.Children.Add(frame);
}
public string _Color(string parametro)
{
string color = "";
if (parametro.Equals("En uso"))
{
color = "#9A8200";
}
else if (parametro.Equals("Disponible"))
{
color = "#35B121";
}
else if (parametro.Equals("En servicio"))
{
color = "#CA1D27";
}
return color;
}
public async Task Mostrarvehiculos(StackLayout Contenedor)
{
funcion = new Dvehiculos();
ListaVehiculos = await funcion.ObtenerVehiculos();
foreach (var item in ListaVehiculos)
{
DibujarVehiculos(item, Contenedor);
}
}
public async Task IraAddVehiculo()
{
await Navigation.PushAsync(new AddVehiculo());
}
#endregion
#region COMANDOS
public ICommand NavAddVehiculoCommand => new Command(async () => await IraAddVehiculo());
#endregion
}
}
型
我的Firebase数据:
using CMP.Modelo;
using CMP.Servicios;
using Firebase.Database;
using Firebase.Database.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
namespace CMP.Datos
{
public class ConexionFirebase
{
public static FirebaseClient FBCliente = new FirebaseClient("https://cmpsoft-260301-default-rtdb.firebaseio.com/");
}
internal class Dvehiculos
{
public async Task<List<MVehiculos>> ObtenerVehiculos()
{
return (await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.OnceAsync<MVehiculos>()).Select(Item => new MVehiculos
{
IdVehiculo = Item.Key,
NumeroEconomico = Item.Object.NumeroEconomico,
Modelo = Item.Object.Modelo,
Nombre = Item.Object.Nombre,
Tipo = Item.Object.Tipo,
NumeroDeSerie = Item.Object.NumeroDeSerie,
Icono = Item.Object.Tipo == "pesado" ? "BttnTruck.png" : "BttnCar.png",
Kilomtraje = Item.Object.Kilomtraje,
HoraDeUso = Item.Object.HoraDeUso,
CantLlantas = Item.Object.CantLlantas,
TiempoVidaLlantas = Item.Object.TiempoVidaLlantas,
DatosExtras = Item.Object.DatosExtras,
Observaciones = Item.Object.Observaciones,
Estado = Item.Object.Estado,
}).ToList();
}
public async Task InsertarVehiculo(MVehiculos parametro)
{
await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.PostAsync(new MVehiculos()
{
IdVehiculo = parametro.IdVehiculo,
NumeroEconomico = parametro.NumeroEconomico,
Modelo = parametro.Modelo,
Nombre = parametro.Nombre,
Tipo = parametro.Tipo,
NumeroDeSerie = parametro.NumeroDeSerie,
Kilomtraje = parametro.Kilomtraje,
HoraDeUso = parametro.HoraDeUso,
CantLlantas = parametro.CantLlantas,
TiempoVidaLlantas = parametro.TiempoVidaLlantas,
DatosExtras = parametro.DatosExtras,
Observaciones = parametro.Observaciones,
Estado = parametro.Estado
});
}
public async Task<List<MVehiculos>> MostrarVehiculosxIdVehiculo(string IdVehiculo)
{
return (await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.OnceAsync<MVehiculos>())
.Where(a => a.Object.IdVehiculo == IdVehiculo).Select(Item => new MVehiculos
{
NumeroEconomico = Item.Object.NumeroEconomico
}).ToList();
}
public async Task<List<MVehiculos>> ObtenerVehiculoxId(string IdVehiculo)
{
return (await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.OnceAsync<MVehiculos>())
.Where(a => a.Object.IdVehiculo == IdVehiculo).Select(Item => new MVehiculos
{
IdVehiculo = Item.Key,
NumeroEconomico = Item.Object.NumeroEconomico,
Modelo = Item.Object.Modelo,
Nombre = Item.Object.Nombre,
Tipo = Item.Object.Tipo,
NumeroDeSerie = Item.Object.NumeroDeSerie,
Icono = Item.Object.Tipo == "pesado" ? "BttnTruck.png" : "BttnCar.png",
Kilomtraje = Item.Object.Kilomtraje,
HoraDeUso = Item.Object.HoraDeUso,
CantLlantas = Item.Object.CantLlantas,
TiempoVidaLlantas = Item.Object.TiempoVidaLlantas,
DatosExtras = Item.Object.DatosExtras,
Observaciones = Item.Object.Observaciones,
Estado = Item.Object.Estado,
}).ToList();
}
public async Task EditarVehiculo(MVehiculos parametro)
{
var data = (await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.OnceAsync<MVehiculos>())
.Where(a => a.Object.IdVehiculo == parametro.IdVehiculo)
.FirstOrDefault();
await ConexionFirebase.FBCliente
.Child("Servicios")
.Child("Vehiculos")
.Child(data.Key)
.PutAsync(new MVehiculos()
{
IdVehiculo = parametro.IdVehiculo,
NumeroEconomico = parametro.NumeroEconomico,
Modelo = parametro.Modelo,
Nombre = parametro.Nombre,
Tipo = parametro.Tipo,
NumeroDeSerie = parametro.NumeroDeSerie,
Kilomtraje = parametro.Kilomtraje,
HoraDeUso = parametro.HoraDeUso,
CantLlantas = parametro.CantLlantas,
TiempoVidaLlantas = parametro.TiempoVidaLlantas,
DatosExtras = parametro.DatosExtras,
Observaciones = parametro.Observaciones,
Estado = parametro.Estado
});
}
}
}
型
我的型号:
using System;
using System.Collections.Generic;
using System.Text;
namespace CMP.Modelo
{
public class MVehiculos
{
public string IdVehiculo { get; set; }
public string NumeroEconomico { get; set; }
public string Modelo { get; set; }
public string Nombre { get; set; }
public string Tipo { get; set; }
public string NumeroDeSerie { get; set; }
public string Icono { get; set; }
public int Kilomtraje { get; set; }
public int HoraDeUso { get; set; }
public int CantLlantas { get; set; }
public int TiempoVidaLlantas { get; set; }
public string DatosExtras { get; set; }
public string Observaciones { get; set; }
public string Estado { get; set; }
}
}
型
到目前为止,我已经尝试过更改Firebase代码,还有视图和更改我的视图模型的代码,但我想不出还有什么可以做的。
1条答案
按热度按时间xzlaal3s1#
正如Jason所说,
MVehiculos
需要实现INotifyPropertyChanged
或以其他方式成为Observable
:1.使用Nuget包管理器在项目中添加CommunityToolkit.Mvvm nuget包。
1.将您的MVehiculos类更改为:
字符串
BaseViewModel
类也需要继承ObservableObject
。有关更多信息,请参阅文档:ObservableProperty attribute。