选择器位于集合内部并按行呈现,选择起作用并触发命令,但在恢复表单时它不显示所选项目,它显示空项目而不是绑定到 xaml 内的 selectedItem 的地址属性。
这是xaml代码:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:appCtrl="clr-namespace:WS.ORD.Apps.POS.Controls"
xmlns:appVM="clr-namespace:WS.ORD.Apps.POS.ViewModels"
xmlns:appModel="clr-namespace:WS.ORD.Apps.POS.Models"
x:Class="WS.ORD.Apps.POS.Views.PrinterSettingsPage"
x:Name="page">
<ContentPage.BindingContext>
<appVM:PrinterSettingsVM />
</ContentPage.BindingContext>
<Shell.BackButtonBehavior>
<BackButtonBehavior IsVisible="False" />
</Shell.BackButtonBehavior>
<ContentPage.Content>
<VerticalStackLayout BindableLayout.ItemsSource="{Binding Printers}" Spacing="30" Margin="100,50">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="appModel:PrinterModel">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding Name}" VerticalOptions="Center" />
<Picker Grid.Row="0" Grid.Column="1"
ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type appVM:PrinterSettingsVM}},Path=Devices}"
ItemDisplayBinding="{Binding .}"
HorizontalOptions="Fill"
VerticalOptions="Center"
SelectedItem="{Binding Address, Mode=TwoWay}"
Margin="0,0,0,10">
<Picker.Behaviors>
<toolkit:EventToCommandBehavior EventName="SelectedIndexChanged" Command="{Binding Source={x:Reference page}, Path=BindingContext.PrinterSelectCommand}" CommandParameter="{Binding .}" />
</Picker.Behaviors>
</Picker>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</VerticalStackLayout>
</ContentPage.Content>
</ContentPage>
这是隐藏代码:
public partial class PrinterSettingsPage : ContentPage
{
public PrinterSettingsPage()
{
InitializeComponent();
}
}
这是视图模型:
namespace WS.ORD.Apps.POS.ViewModels
{
internal class PrinterSettingsVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ICommand PrinterSelectCommand { get; private set; }
public PrinterSettingsVM()
{
this.Devices[0] = "device1";
this.Devices[1] = "device2";
this.Devices[2] = "device3";
this.Printers = new List<PrinterModel>()
{
new PrinterModel { Id=Guid.NewGuid(), Name="Printer 2", Address="device1" },
new PrinterModel { Id=Guid.NewGuid(), Name="Printer 2", Address="device2" }
};
}
private string[] _devices = new string[3];
public string[] Devices
{
get { return this._devices; }
set
{
this._devices = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Devices)));
}
}
}
private IEnumerable<PrinterModel> _printers;
public IEnumerable<PrinterModel> Printers
{
get { return this._printers; }
set
{
this._printers = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Printers)));
}
}
}
}
}
最后是模型:
namespace WS.ORD.Apps.POS.Models
{
public class PrinterModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Guid Id { get; set; }
public string Name { get; set; }
private string _address;
public string Address
{
get { return this._address; }
set
{
this._address = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Address)));
}
}
}
}
}
在非常简单的情况下,当这个应用程序启动时,预期的行为是 查看选择的选择器,但没有。
您的应用程序关闭后,所有变量都会被释放,因此应用程序重启后数据将不会恢复。
但是您可以使用
local databases
、Preferences
或SecureStorage
来保存数据。并且您可以在重新启动应用程序后恢复它。
NET MAUI local databases
SQLite 数据库引擎允许 .NET MAUI 应用程序在共享代码中加载和保存数据对象。您可以将 SQLite.NET 集成到 .NET MAUI 应用程序中,以在本地数据库中存储和检索信息。
Preferences
Preferences
使用 String 键存储。首选项的值必须是以下数据类型之一:
Boolean
Double
Int32
Single
Int64
String
DateTime
ISecureStorage
ISecureStorage 有助于安全地存储简单的键/值对。
将给定密钥的值保存在安全存储中:
await SecureStorage.Default.SetAsync("oauth_token", "secret-oauth-token-value");
要从安全存储中检索值:
string oauthToken = await SecureStorage.Default.GetAsync("oauth_token");
if (oauthToken == null)
{
// No value is associated with the key "oauth_token"
}