Xamarin.窗体iOS选取器在关闭对话框后跳到最后一项



我刚刚在一个正在进行的项目中遇到了这个问题,我被难住了。如下图所示,在iOS(Emulator(上使用"选取器"对话框选择项目时,所选值会在确认后跳到列表中的最后一个项目(无论我是退出对话框还是使用"完成"按钮(。在Android上,相应的对话框运行正常我正在使用Xamarin。表格4.5.0.356和Xamarin。要点1.5.1

最小错误复制

public class PickerItemModel
{
public string TestProp { get; set; }
public PickerItemModel(string t) => TestProp = t;
public override string ToString()
{
return TestProp;
}
}
public class MainViewModel : ComputedBindableBase
{
public List<PickerItemModel> PickerItemModels { get; set; } = new List<PickerItemModel> {
new PickerItemModel("Hello"),
new PickerItemModel("Stackoverflow")
};
public PickerItemModel SelectedModel { get; set; }

ComputedBindableBase未实现INotifyPropertyChanged事件,并在属性更改时自动引发该事件。

<Picker Title="Test"
ItemsSource="{Binding PickerItemModels}"
SelectedItem="{Binding SelectedModel}" />

现在的问题是,我该如何修复这种行为,或者什么是有效的解决方法。我愿意为此使用一些自定义包,但我无法独自实现完整的对话框,因为我对项目的时间有一些限制。

提前感谢您抽出时间。:-(

编辑:

几个小时后,我重新编译了代码,现在它可以工作了。我想这是旧代码没有被正确重新部署的问题。。。我也在Mac上使用VS,所以这可能也是原因,因为它从第一天起就表现出了bug。。。

此外,这里还有BindableBase类(ComputedBindableBase只添加一个实用程序函数(:

public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T target, T value, [CallerMemberName] string propertyName = "")
{
target = value;
RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

我无法使用Xamarin重新解决此问题。表格4.5.0.356和Xamarin。要点1.5.1。我的代码在功能上和您的代码相同,只是我并没有从ComputedBindableBase派生我的视图模型。以下代码按预期工作

查看

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:app2="clr-namespace:App3"
mc:Ignorable="d"
x:Class="App3.MainPage">
<ContentPage.BindingContext>
<app2:MainPageViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<Picker Title="Please select..."
ItemsSource="{Binding PickerItemModels}"
SelectedItem="{Binding SelectedModel}" />
</ContentPage.Content>
</ContentPage>

ViewModel

public class PickerItemModel
{
public string TestProp { get; set; }
public PickerItemModel(string t) => TestProp = t;
public override string ToString() => TestProp;
}
public class MainPageViewModel
{
public List<PickerItemModel> PickerItemModels { get; set; } = new List<PickerItemModel> {
new PickerItemModel("Hello"),
new PickerItemModel("Stackoverflow")
};
public PickerItemModel SelectedModel { get; set; }
}

最新更新