我有一些使用xamarin形式IOS绑定的问题。 我有绑定当第一页出现时,在屏幕中央显示一个加载标签。绑定问题和OnAppearing问题与Xamarin形式IOS
在listview加载数据后,通过绑定隐藏加载标签,并显示列表视图。
在Android项目中,我有这个回购Xamarin forms IOS Android Test
它工作正常。在repro项目中的ios中,加载标签在应用程序第一次加载时隐藏,如果点击屏幕将显示。 如果你继续加载标签消失,你会看到一个空白屏幕,但如果你再次点击屏幕上出现列表数据视图。
我在想这是一个xamarin错误,但我希望我的绑定不正确。我为这个问题工作,将不胜感激。 我还有两个其他问题。当您单击列表视图项并导航时,数据不会刷新,因为未出现未触发。
在android中,它被触发时没有问题。有时在你像上面提到的步骤一样点击屏幕之后触发。 最后一个问题是,当在标签之间切换时,IOS不会在出现时触发。它使用android。 在这个问题上的任何帮助将不胜感激。我已经对这些问题的解决方案进行了广泛的搜索,但尚未找到答案。 谢谢!
下面是一些代码片段的帮助。如果您想对其进行测试,此代码也位于GitHub仓库中。
在XAML
<Label x:Name="xEmptyListView"
FontSize="16"
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
VerticalOptions="CenterAndExpand"
Text="{Binding ListViewVisibleText, Mode=TwoWay}"
IsVisible="{Binding IsListViewLabelEmptyVisible, Mode=TwoWay }"/>
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected"
IsVisible="{Binding IsListViewVisible, Mode=TwoWay}"
IsEnabled="{Binding IsActivityRunning, Mode=TwoWay, Converter={StaticResource InverseBooleanConverter}}">
视图模型
private bool activityRunning { get; set; }
public bool IsActivityRunning
{
get { return activityRunning; }
set
{
if (activityRunning == value)
return;
activityRunning = value;
OnPropertyChanged("IsActivityRunning");
}
}
private string listViewVisibleText { get; set; }
public string ListViewVisibleText
{
get { return listViewVisibleText; }
set
{
if (listViewVisibleText == value)
return;
listViewVisibleText = value;
OnPropertyChanged("ListViewVisibleText");
}
}
private bool listViewLabelEmptyVisible { get; set; }
public bool IsListViewLabelEmptyVisible
{
get
{
if (Items == null || Items.Count == 0)
{
if (IsBusy)
{
ListViewVisibleText = "Loading...";
}
else
{
ListViewVisibleText = "No Items found";
}
listViewLabelEmptyVisible = true;
}
else
{
ListViewVisibleText = string.Empty;
listViewLabelEmptyVisible = false;
}
OnPropertyChanged("IsListViewLabelEmptyVisible");
return listViewLabelEmptyVisible;
}
}
private bool listViewVisible { get; set; }
public bool IsListViewVisible
{
get
{
if (Items == null || Items.Count == 0)
{
listViewVisible = false;
}
else
{
listViewVisible = true;
}
OnPropertyChanged("IsListViewVisible");
return listViewVisible;
}
}
XAML.cs
protected override void OnAppearing()
{
base.OnAppearing();
viewModel.LoadItemsCommand.Execute(null);
}
我使用的通知属性更改。这是标准的代码
这是我的视图模型继承了notifyproperty改变
public class ItemsViewModel : BaseViewModel
当你创建一个测试xamarin项目,这是该基地视图模型从可观察对象
public class BaseViewModel : ObservableObject
继承它看起来的样子。
public class ObservableObject : INotifyPropertyChanged
{
/// <summary>
/// Sets the property.
/// </summary>
/// <returns><c>true</c>, if property was set, <c>false</c> otherwise.</returns>
/// <param name="backingStore">Backing store.</param>
/// <param name="value">Value.</param>
/// <param name="propertyName">Property name.</param>
/// <param name="onChanged">On changed.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
protected bool SetProperty<T>(
ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Occurs when property changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the property changed event.
/// </summary>
/// <param name="propertyName">Property name.</param>
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
活动运行时得到的物品加载等 这对Android的所有作品集。它没有IOS。
你可以添加一些示例代码? – Mike