我有一个自定义 ListView 控件和自定义滚动条及其在 .Net MAUI 中的渲染器。因为我在 MAUI 中重复使用相同的渲染器。 下面是自定义的 Datagrid.xaml UI 结构
<Grid><AbsoluteLayout Grid.Row="1">
<ScrollView x:Name="Sv" Orientation="Horizontal" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<helpers:DataGridListView x:Name="ListView"></helpers:DataGridListView>
</ScrollView>
<helpers:DataGridVertScrollBar IsVisible="{Binding EnableCustomScroll}" HorizontalOptions="End" x:Name="ssv" HeightRequest="200" WidthRequest="8" AbsoluteLayout.LayoutFlags="HeightProportional,YProportional,XProportional"
AbsoluteLayout.LayoutBounds="1,1,8,1"></helpers:DataGridVertScrollBar>
</AbsoluteLayout>
<ContentView x:Name="NoDataViewElement" Grid.Row="0" Grid.RowSpan="2" VerticalOptions="Center" IsVisible="False"/>
DataGrid.xaml.cs
internal IList<DataGridRowModel> InternalItems
{
get => _internalItems;
set
{
_internalItems = value;
//if(_internalItems == null || !_internalItems.Any()) return;
if (IsSortable && SortedColumnIndex != null)
SortItems(SortedColumnIndex);
else
{
ListView.ItemsSource = _internalItems;
}
if (EnableCustomScroll)
{
var h = (ListView.RowHeight * (_internalItems.Count)) * 1.0;
if (h == 0)
{
ssv.UpdateHeight(1);
}
else
{
double x = ssv.Height / h;
if (x > 1) x = 1;
ssv.UpdateHeight(x);
}
}
Helper类如下
public class DataGridVertScrollBar : AbsoluteLayout
{
BoxView thumb;
double height;
public DataGridVertScrollBar()
{
HorizontalOptions = LayoutOptions.End;
BackgroundColor = Color.FromRgba(0, 0, 0, 0.1);
thumb = new BoxView { WidthRequest = 8, HeightRequest = 40, BackgroundColor = Color.FromRgba(0, 0, 0, 0.5), HorizontalOptions = LayoutOptions.Center };
Children.Add(thumb);
AbsoluteLayout.SetLayoutBounds(thumb, new Rect(1.1, 1, 1, 1));
AbsoluteLayout.SetLayoutFlags(thumb, AbsoluteLayoutFlags.HeightProportional | AbsoluteLayoutFlags.WidthProportional);
}
public void UpdateHeight(double val)
{
IsVisible = val != 1;
height = val;
AbsoluteLayout.SetLayoutBounds(thumb, new Rect(0, 0, 1, height));
}
public void Move(double y)
{
if (y > Height) y = Height;
AbsoluteLayout.SetLayoutBounds(thumb, new Rect(0, y, 1, height));
}
}
我的问题是 ssv.Height 值即将 -1,即使 _internalItems 有数据。 在旧的 xamarin 应用程序中,它工作正常,当 _internalItems.Count>0 ssv.Height 为 215 而不是 -1 时。
我不知道为什么滚动条模板最初没有渲染。
请帮助我,我是毛伊岛的新手。
我测试了你提供的代码,发现在MAUI项目中
ssv.Height
仍然是-1,在MAUI项目中找不到高度。
您可以尝试使用Task并让该方法在布局加载后运行。
Task task = Task.Run(() =>
{
var h = (listView.RowHeight * (9)) * 1.0;
if (h == 0)
{
ssv.UpdateHeight(1);
}
else
{
double x = ssv.Height / h;
if (x > 1) x = 1;
ssv.UpdateHeight(x);
}
});