Maui CollectionView Probleme mit SelectionMode = Single und Zeilen AnzeigeC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Maui CollectionView Probleme mit SelectionMode = Single und Zeilen Anzeige

Post by Anonymous »

Code: Select all

I have a CollectionView with 2 columns.
The SelectionMode=Single.
Problem :
The second column displays only a few colors names,
not all and after a selection no more color names !
< /code>
Ich verwende Visual Studio 2022 17.11.5 Maui Android C#.

Ich habe seit einigen Tagen keine Lösung für dieses [url=viewtopic.php?t=20324]Problem[/url] gefunden.  />  Kann mir jemand erklären, welchen Fehler ich mache, bitte?


































< /code>
 Datei pickColorpage.xaml.cs < /h2>
using System.Collections.ObjectModel;
using Models;

namespace ViewsViewModels
{
public partial class PickColorPage : ContentPage, IQueryAttributable, INotifyPropertyChanged
{
public Setting Setting { get; set; }
public Setting StartSetting { get; set; }

void IQueryAttributable.ApplyQueryAttributes(IDictionary  query)
{
if (query.Values.Count == 0) return;
Setting = query.Values.ElementAt(0) as Setting;
StartSetting = new Setting(Setting);
OnPropertyChanged(nameof(Setting));
InitColorsList();
System.Diagnostics.Debug.WriteLine($"**************** ListofColors => {ListOfColors.Count}");
//            int i = 0; foreach (ClColor c in ListOfColors) { i++; System.Diagnostics.Debug.WriteLine($"{i} => {c.ColorName}"); }
}

public ObservableCollection ListOfColors { get; } = [];

public PickColorPage(MainViewModel vm)
{
InitializeComponent();
BindingContext = vm;
}

public partial class ClColor : ObservableObject
{
[ObservableProperty]
public string colorName;
[ObservableProperty]
public string colorValue;

public ClColor(string _colorName, string _colorValue)
{
ColorName= _colorName;
ColorValue= _colorValue;
}
}

public void InitColorsList()
{
System.Drawing.Color c;
List ls = GetListOfColors();
ListOfColors.Clear();
foreach (string s in ls)
{
c = System.Drawing.Color.FromName(s);
string chex = ToHexString(c);
string settingColor = (string)Setting.SettingValue;
bool rdbtn = chex.Equals(settingColor, StringComparison.InvariantCultureIgnoreCase);
ListOfColors.Add(new ClColor(s, chex));
}
//            int i = 0; foreach(ClColor r in ListOfColors) { Console.WriteLine($"{i} => {r.RdbtnChecked}"); i++; }
}

private static string ToHexString(System.Drawing.Color color)
{
byte r = color.R;
byte g = color.G;
byte b = color.B;
//        byte a = color.A;
//        return string.Format("#{0,2:X2}{1,2:X2}{2,2:X2}{3,2:X2}", r, g, b, a);
return string.Format("#{0,2:X2}{1,2:X2}{2,2:X2}", r, g, b);
}

public static List GetListOfColors()
{
List colorList = [];

// Get type of KnownColor enum
var color = typeof(System.Drawing.KnownColor);

// Enumerate all known color names in enum
var colors = Enum.GetValues(color);

// Remove 27 elements from beginning & 7 elements from the end (windows controls)
var from = 26;
var to = colors.Length - 8;

// Only keep color names and not user interface colors
for (int i = from; i < to; i++) colorList.Add(colors.GetValue(i).ToString());

// Return filtered color list
return colorList;
}

private async void cv_Loaded(object sender, EventArgs e)
{
await Task.Delay(1000);  // it seems a bug exists in ScrollTo(), hence this delay to PARTIALLY solve this problem
ClColor clc = ListOfColors.FirstOrDefault(predicate: cl => cl.ColorValue == (string)Setting.SettingValue);
cv.ScrollTo(clc, group:null, position: ScrollToPosition.Center, animate: false); // false because scroll does work very badly !!!
cv.SelectedItem = clc;
}

ClColor? previousSelection = null;

private void cv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
previousSelection = e.PreviousSelection.FirstOrDefault() as ClColor;
ClColor? x = e.CurrentSelection.FirstOrDefault() as ClColor;
Setting.SettingValue = x.ColorValue;
}

public void Cancel(object sender, EventArgs e)
{
Setting.SettingValue = StartSetting.SettingValue;
OnPropertyChanged(nameof(Setting));
Shell.Current.GoToAsync("..");
}
}
}
< /code>
 Datei configModels.cs < /h2>
using CommunityToolkit.Mvvm.ComponentModel;

namespace Models
{
public class Setting : ObservableObject
{
public Setting() { SettingDescription = ""; SettingType = null; SettingValue = Colors.White;  }
public Setting(Setting s) { SettingDescription = s.SettingDescription; SettingType = s.SettingType; SettingValue = s.SettingValue; }
public Setting(string desc, Type type, object val) { SettingDescription = desc; SettingType = type; SettingValue = val; }

private string settingDescription;
public string SettingDescription
{
get { return settingDescription; }
set { SetProperty(ref settingDescription, value); }
}

private Type? settingType;  // color or double
public Type SettingType
{
get => settingType;
set { SetProperty(ref settingType, value); }
}

private object settingValue;
public object SettingValue
{
get { return settingValue; }
set { SetProperty(ref settingValue, value); }
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post