4gu*_*71n 2 listview xamarin xamarin.forms
我正在使用Xamarin.Forms开发一个应用程序,我有一个用户可以发布Feed更新的屏幕,此Feed更新包含文本和一些附加文件.我的XAML视图基本上,一个StackLayout
包含ListView
,并Editor
和一对夫妇的Buttons
.问题是我正试图在方法中设置ItemSource
我ListView
的onAppearing
.该方法被称为正常.我正在设置的集合按预期更改.但由于某种原因,ListView
没有更新.ListView
在设置Source
调用Editor
焦点方法后,使工作正常的解决方法.我之所以这样做,是因为我发现当我在设置集合后点击编辑器时,ListView
正确渲染了.但这是一个非常讨厌的解决方法.
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HalliganTL.View.UpdateFeedPage">
<StackLayout Orientation="Vertical" >
<ListView x:Name="AttachedFilesListView" MinimumHeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Button Text="[X]" />
<StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
<Label Text="{Binding Name}" VerticalTextAlignment="Center" FontSize="Medium" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Editor x:Name="UpdateFeedEditor" FontSize="15" Text="{Binding PostText}" VerticalOptions="FillAndExpand" />
<Button Text="Attach file" TextColor="White" BackgroundColor="#77D065" Clicked="onAttachFileClicked"/>
<Button Text="Post" TextColor="White" BackgroundColor="#77D065" Clicked="onPostClicked"/>
</StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
这是我的页面代码:
public partial class UpdateFeedPage : ContentPage
{
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static string PostText = "";
public UpdateFeedPage()
{
InitializeComponent();
Title = "New Update";
UpdateFeedEditor.Text = PostText;
}
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
async void onPostClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
async void onAttachFileClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new FilePickerPage());
}
}
Run Code Online (Sandbox Code Playgroud)
我玩了很多东西,比如:
从ListView和包含ListView的StackLayout更改HorizontalOptions和VerticalOptions.
删除除StackLayout和子ListView之外的所有其他控件.
但到目前为止没有任何工作,除了以编程方式调用编辑器,Focus方法
注意:
正如您所看到的,我设置的ListView集合是静态的,我直接从另一个页面修改该集合,但我认为这没关系,因为当我在OnAppearing中设置断点时,正在正确地修改集合.
解决该刷新问题的两种方法.
第一路)
List<FileObject>
通过重置以下命令强制ListView从您的集合中刷新ItemsSource
:
当前:
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
Run Code Online (Sandbox Code Playgroud)
改成:
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = null;
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
Run Code Online (Sandbox Code Playgroud)
第二路)
将您的List<FileObject>
收藏替换为,ObservableCollection<FileObject>
以便通过将要收听的NotifyCollectionChangedAction.Add|Remove|...
基于事件发布更改ListView
.
当前:
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
Run Code Online (Sandbox Code Playgroud)
改成:
public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();
Run Code Online (Sandbox Code Playgroud)
注意:还要添加一个 using System.Collections.ObjectModel;
你OnAppearing
成了:
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4678 次 |
最近记录: |