我正在开发xamarin.forms应用程序,我有一个列表视图,其中有11个堆栈布局,每个stackLayout都有不同的设计。
在这里,堆栈布局中所有控件的所有颜色和可见性都是从viewModel绑定的。
问题是,在加载应用程序时,listview正在为视图加载,但是在那里,从ViewModel绑定列表视图的设计需要花费大约10-30秒的时间。
<ListView x:Name="itemslist" ItemsSource="{Binding listViewItems}" CachingStrategy="RecycleElement" HasUnevenRows="true" SeparatorVisibility="None" HorizontalOptions="FillAndExpand" BackgroundColor="#e7ebee" ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<controls:CustomViewCell/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>我的自定义viewCell是如下结构:
<StackLayout>
<StackLayout x:Name="Type1">
<Frame>
Combination of Buttons, pickers and editor
</Frame>
</StackLayout>
.........................................................
<StackLayout x:Name="Type11">
<Frame>
Combination of Buttons, pickers and editor
</Frame>
</StackLayout>
</StackLayout>这个自定义视图单元由11个堆栈组成,每个堆栈都有不同的设计。
是否有任何方法使设计快速加载到视图。

发布于 2019-01-22 10:50:52
你是正确的。这不是最好的方法,因为在加载列表时,您实例化了许多对最终用户没有用处的可视元素。
我就是这样做的:
公共类ContentView1 : ContentView { public ContentView1 () {.}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
if (BindingContext is MyViewModel vm)
{
_vm = vm;
vm.PropertyChanged += VmOnPropertyChanged;
}如果_vm不是null },请不要忘记分离事件处理程序。
private void VmOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(MyViewModel.MyProperty))
{
switch (_vm.MyProperty)
{
case 1:
Content = new View1();
break;
case 2:
Content = new View2();
break;
[...]
}
}
}}
这样,只有在需要时,才能将视图号"n“固定下来。
https://stackoverflow.com/questions/54301143
复制相似问题