在ViewModel中,我从FirebaseDatabase加载数据,并在CollectionView中显示数据。
public MainViewModel()
{
var collection = firebase
.Child("Foto/")
.AsObservable<Fotos>()
.Subscribe((dbevent) =>
{
if (dbevent.Object != null)
{
Foto.Add(dbevent.Object);
}
});
}但是我想改变它,ToList,所以我使它降序基于BalId在数据库中。
想要使用GetAllFotosDesending(),但我不能使它在MainViewModel中工作。
public async Task<List<Fotos>> GetAllFotosDesending()
{
return (await firebase
.Child("Foto/")
.OnceAsync<Fotos>()).Select(item => new Fotos
{
BalId = item.Object.BalId,
RollNo = item.Object.RollNo,
Foto = item.Object.Foto,
Titel = item.Object.Titel,
Fototekst = item.Object.Fototekst
}).OrderByDescending(x => x.BalId).ToList();
}2选项,或使其在ToList中MainViewModel或添加GetAllFotosDesending()工作在MainViewModel中。最后一个选择也许更好?但是,当添加到MainViewModel时,我无法使它工作。
这是CollectionView和ItemsSource="{Binding Foto}"
<CollectionView
x:Name="Dood"
ItemsSource="{Binding Foto}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid ColumnDefinitions="Auto, *"
RowDefinitions="Auto, Auto, Auto, 1"
ColumnSpacing="10"
RowSpacing="5"
Padding="0,10">
<Image Source="{Binding Pic}"
Margin="20,0,0,10"
HeightRequest="70"
WidthRequest="70"
HorizontalOptions="Center"
VerticalOptions="Center"
Grid.RowSpan="3"
Grid.Row="0"
Grid.Column="0">
<Image.Clip>
<EllipseGeometry
Center="35,35"
RadiusX="35"
RadiusY="35"/>
</Image.Clip>
</Image>
<Label Text="{Binding Titel}"
FontAttributes="Bold"
Grid.Column="1"
Grid.Row="0"/>
<Label Text="{Binding Email}"
Grid.Column="1"
Grid.Row="1"/>
<Label Text="{Binding RollNo}"
Grid.Column="1"
Grid.Row="2"/>
<BoxView Style="{StaticResource SeparatorLine}"
Grid.Column="0"
Grid.Row="3"
Grid.ColumnSpan="2"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>像这样装订
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}发布于 2022-11-10 21:19:26
默认情况下,OrderBy没有字母数字比较,默认的Orderby只比较字母或数字,而不是两者的组合,您需要为您创建自己的字母数字比较器(特别是如果这些是字符串值)。
您可以使用这个基本的比较器来进行升序和降序:
public class AlphanumComparator : IComparer<object>
{
private enum ChunkType { Alphanumeric, Numeric };
private bool InChunk(char ch, char otherCh)
{
ChunkType type = ChunkType.Alphanumeric;
if (char.IsDigit(otherCh))
{
type = ChunkType.Numeric;
}
return (type != ChunkType.Alphanumeric || !char.IsDigit(ch))
&& (type != ChunkType.Numeric || char.IsDigit(ch));
}
public int Compare(object x, object y)
{
string firstString = x as string;
string secondString = y as string;
if (string.IsNullOrWhiteSpace(firstString) || string.IsNullOrWhiteSpace(secondString))
{
return 0;
}
int firstMarker = 0, secondMarker = 0;
while ((firstMarker < firstString.Length) || (secondMarker < secondString.Length))
{
if (firstMarker >= firstString.Length)
{
return -1;
}
else if (secondMarker >= secondString.Length)
{
return 1;
}
char firstCh = firstString[firstMarker];
char secondCh = secondString[secondMarker];
StringBuilder thisChunk = new StringBuilder();
StringBuilder thatChunk = new StringBuilder();
while ((firstMarker < firstString.Length) && (thisChunk.Length == 0 || InChunk(firstCh, thisChunk[0])))
{
thisChunk.Append(firstCh);
firstMarker++;
if (firstMarker < firstString.Length)
{
firstCh = firstString[firstMarker];
}
}
while ((secondMarker < secondString.Length) && (thatChunk.Length == 0 || InChunk(secondCh, thatChunk[0])))
{
thatChunk.Append(secondCh);
secondMarker++;
if (secondMarker < secondString.Length)
{
secondCh = secondString[secondMarker];
}
}
int result = 0;
// If both chunks contain numeric characters, sort them numerically
if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
{
int thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
int thatNumericChunk = Convert.ToInt32(thatChunk.ToString());
if (thisNumericChunk < thatNumericChunk)
{
result = -1;
}
if (thisNumericChunk > thatNumericChunk)
{
result = 1;
}
}
else
{
result = thisChunk.ToString().CompareTo(thatChunk.ToString());
}
if (result != 0)
{
return result;
}
}
return 0;
}
}一旦完成了,您就可以如下所示使用它:
OrderByDescending(x => x.BalId, new AlphanumComparator());
OrderBy(x => x.BalId, new AlphanumComparator());发布于 2022-11-12 07:20:14
就这样解决了。
public async Task<List<Fotos>> GetAllFotosDesending()
{
return (await firebase
.Child("Foto/")
.OnceAsync<Fotos>()).Select(item => new Fotos
{
BalId = item.Object.BalId,
RollNo = item.Object.RollNo,
Foto = item.Object.Foto,
Titel = item.Object.Titel,
Fototekst = item.Object.Fototekst
}).OrderByDescending(x => x.BalId).ToList();
}
public async void InitializeAsync()
{
Fotos = await GetAllFotosDesending();
}
public MainViewModel()
{
InitializeAsync();
}https://stackoverflow.com/questions/74395019
复制相似问题