我有一個小專案,有兩個頁面顯示串列視圖。但是我的一個頁面無法正常作業,盡管只做了一些小改動(道具名稱)的相同代碼。系結背景關系獲取我需要的資訊(患者串列),但頁面回傳為空。在我使用相同代碼的其他頁面中,Viewlist 可以正常作業。也許我遺漏了一些東西,或者可能是一個錯字錯誤,我真的不知道發生了什么。請幫助我,如果您有改進我的代碼的建議,請告訴我我是 xamarin 表單的初學者。
XAML:
<ListView x:Name="appointmentListView"
SeparatorVisibility="None"
HasUnevenRows="true"
ItemsSource="{Binding Appointments}">
<ListView.Header>
<Grid BackgroundColor="#03A9F4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand" x:DataType="model:Appointment">
<Label VerticalOptions="FillAndExpand" VerticalTextAlignment="Center"
Text="{Binding Patient}"
FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代碼:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AppointmentListViewDetail : ContentPage
{
public AppointmentListViewDetail()
{
InitializeComponent();
BindingContext = new AppointmentListViewModel();
appointmentListView.ItemSelected = (s, e) =>
{
if (e.SelectedItem as Appointment != null)
{
Action goToPage = async () =>
{
await PopupNavigation.Instance.PushAsync(new AppointmentDetailsPopup(e.SelectedItem as Appointment));
appointmentListView.SelectedItem = null;
};
goToPage.Invoke();
}
};
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<AppointmentListViewModel>(this, "Reload", (p) =>
{
BindingContext = p;
});
MessagingCenter.Subscribe<AppointmentFilterViewModel>(this, "Filter", (p) =>
{
string? nameFilter = p.NameFilter.Text;
DateTime? startDateFilter = DateTime.ParseExact(p.StartDateFilter.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime? finalDateFilter = DateTime.ParseExact(p.FinalDateFilter.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Status? statusFilter = (Status)Enum.Parse(typeof(Status), p.StatusFilter.Text);
BindingContext = new AppointmentListViewModel(nameFilter, startDateFilter, finalDateFilter, statusFilter);
});
}
視圖模型:
public class AppointmentListViewModel
{
ObservableCollection<Appointment> _appointments;
public ObservableCollection<Appointment> Appointments
{
get { return _appointments; }
set
{
_appointments = value;
OnPropertyChanged(nameof(Appointments));
}
}
public ICommand Filter { get; set; }
public AppointmentListViewModel()
{
var getAppointmentsList = new Action(async () =>
{
Appointments = new ObservableCollection<Appointment>(await Startup.ServiceProvider.GetService<AppointmentService>().ToListAsync());
});
getAppointmentsList.Invoke();
}
public AppointmentListViewModel(string? patient, DateTime? startDate, DateTime? finalDate, Status? status)
{
var getPatientList = new Action(async () =>
{
Appointments = new ObservableCollection<Appointment>(await Startup.ServiceProvider.GetService<AppointmentService>().FilterSearchAsync(patient, startDate, finalDate, status));
});
getPatientList.Invoke();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
模型:
public class Appointment
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public float Price { get; set; }
public DateTime Date { get; set; }
public Status PaymentStatus { get; set; }
public string Patient { get; set; }
}
uj5u.com熱心網友回復:
我使用 Task.Run().Wait() 解決了這個問題。如果您嘗試從視圖模型上的資料庫中獲取資料,我建議使用它。
視圖模型:
public AppointmentListViewModel()
{
Task.Run(async () =>
{
Appointments = new ObservableCollection<Appointment>(await Startup.ServiceProvider.GetService<AppointmentService>().ToListAsync());
}).Wait();
}
包含與資料庫相關的所有操作的服務:
public AppointmentService()
{
string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "patient.db3");
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Appointment>().Wait();
}
public Task<List<Appointment>> ToListAsync()
{
return _database.Table<Appointment>().ToListAsync();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515953.html
標籤:C#xmlxamarinxamarin.forms虚拟机
