In the view, I have a ListView that should be shown a with a data binding with an Observable collection of string, but not shown anything
If instead of listview I put a label and the observable collection turns it into a simple string I see the data
In the Main view:
<ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding SerialsPorts}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding SerialPortName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In the Main ViewModel
class BrightnessSerialsPortsViewModel : INotifyPropertyChanged, IBrightnessSerialsPortsViewModel
{
//readonly IPageDialogService pageDialogService;
readonly IBrightnessSerialsPortsManager manager;
public BrightnessSerialsPortsViewModel()
{
//pageDialogService = new PageDialogService();
manager = new BrightnessSerialsPortsManager();
manager.BrightnessInfoUpdated += OnBrightnessInfoUpdated;
manager.DeviceSerialPortsInfoUpdated += OnDeviceSerialsPortsInfoUpdated;
PageAppearingCommand = new Command(OnPageAppearing);
PageDisappearingCommand = new Command(OnPageDisappearing);
}
void OnPageAppearing()
{
//pageDialogService.DisplayAlert("Invoke Command Demo Page", "Appearing event fired.", "OK");
manager.GetBrightness();
manager.GetSerialsPorts();
}
void OnPageDisappearing()
{
//pageDialogService.DisplayAlert("Invoke Command Demo Page", "Disappearing event fired.", "OK");
SerialTest = "";
}
private void OnDeviceSerialsPortsInfoUpdated(object sender, IDeviceSerialsPortsInfoEventArgs e)
{
foreach(string device in e.DeviceSerialsPorts.Devices)
{
ISerialsPortsViewModel serialsPortsViewModel = new SerialsPortsViewModel(device);
SerialsPorts.Add(serialsPortsViewModel);
SerialTest += device + Environment.NewLine;
}
}
private void OnBrightnessInfoUpdated(object sender, IBrightnessInfoEventArgs e)
{
float f = e.DeviceBrightness.Brightness;
decimal dec = new decimal(f);
Brightness = (double) dec;
}
//public ICommand ChangeBrightnessCommand { get; set; }
public ICommand PageAppearingCommand { get; private set; }
public ICommand PageDisappearingCommand { get; private set; }
public ICommand ChangeBrightnessCommand => new RelayCommand(() => ExcecuteChangeBrightnessCommand());
public void ExcecuteChangeBrightnessCommand()
{
}
private ObservableCollection<ISerialsPortsViewModel> serialsPorts = new ObservableCollection<ISerialsPortsViewModel>();
public ObservableCollection<ISerialsPortsViewModel> SerialsPorts { get=> serialsPorts ; set { serialsPorts = value; OnPropertyChanged(nameof(SerialsPorts)); } }
private string serialstest = "";
public string SerialTest { get => serialstest; set {serialstest = value ; OnPropertyChanged(nameof(SerialTest)); } }
private double brightness = 1.0;
public double Brightness { get => brightness; set {brightness = value ; OnPropertyChanged(nameof(Brightness)); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
In the SerialPorts ViewModel:
public SerialsPortsViewModel(string serialPortName)
{
SerialPortName = serialPortName;
}
private string serialPortName;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string SerialPortName { get=> serialPortName; set {serialPortName = value ; OnPropertyChanged(nameof(SerialPortName)); } }
What am I doing wrong?
Solved change the View to this:
<ListView Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding SerialsPorts}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="60">
<StackLayout Orientation="Horizontal">
<BoxView BackgroundColor="Blue" WidthRequest="10" Margin="0,0,0,10" />
<StackLayout BackgroundColor="White" Orientation="Vertical" Margin="5,5,10,5">
<Label Text="{Binding SerialPortName}" FontAttributes="Bold" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.