我无法在另一页上获取 Picker SelectedItem 值
I am unable to get a Picker SelectedItem value on another page
我正在尝试获取一个选择器 SelectedItem 值以在另一个页面上使用,但它似乎并不想工作。
我在这里找到了 "solution": 但它不想为我工作。当我尝试使用它时,比如标签,我似乎无法显示它。我可以手动设置字符串,但不能使用实际选中项的字符串。
我按照指示创建了静态 class:
public static class PickerPersist
{
public static string pickerSelectedItem = "test";
}
并包含 xmlns:local="clr-namespace:C971"
和绑定到 Text="{Binding x:static local:PickerPersist.PickerSelectedItem}"
的标签,并且在显示标签时正确显示 "test"。
我的问题是,如何获取要显示的实际 SelectedItem 的字符串?选择器本身在另一个页面上,我能够使用 var pickerItem = (Terms)termpicker.SelectedItem;
使其正确显示
我尝试将 PickerSelectedItem
设置为 Page1.termPicker.SelectedItem;
,但返回错误 "Page1.termPicker is inaccessible due to its protection level"。
通过在上一页 class 上设置 public static string Picked;
,然后将变量 pickerItem
设置为 Picked
,我能够消除错误。但是,它似乎并没有转移到新页面。如果我在将 pickeritem
设置为 Picked
后休息,一切都表明它确实设置正确,但它在 PickerPersist
中显示为空,因此不会显示在新页面上。
我希望这一切都有意义。
你能提供的任何帮助都会很棒!!谢谢!
此处提供完整代码:https://github.com/yax51/C971.git
Xaml Picker 住的地方:
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"
mc:Ignorable="d"
xmlns:local="clr-namespace:C971"
x:Class="C971.Page1"
Title="Term Selector">
<ContentPage.Content>
<StackLayout>
<Picker x:Name="termPicker"
ItemsSource="{Binding Terms}"
ItemDisplayBinding="{Binding TermName}"
Title="Select a term"
IsEnabled="True"
SelectedItem="{x:Static local:PickerPersist.PickerSelectedItem}"/>
Xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public static String Picked;
public Page1()
{
InitializeComponent();
}
...
void TermGoClicked(object sender, EventArgs e)
{
var pickerItem = (Terms)termPicker.SelectedItem;
if (pickerItem == null)
{
DisplayAlert("Error", "Please Select a term", "Ok");
}
else
{
Picked = pickerItem.TermName;
Navigation.PushAsync(new TermPage());
}
}
静态class
public static class PickerPersist
{
public static string PickerSelectedItem = Page1.Picked;
}
新页面上的标签
<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"
mc:Ignorable="d"
xmlns:local="clr-namespace:C971"
x:Class="C971.TermPage">
<ContentPage.Content>
<StackLayout>
<Label x:Name="test" Text="{x:Static local:PickerPersist.PickerSelectedItem}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
使用 Picker 侦听器 SelectedIndexChange,创建一个视图模型,当所选项目发生变化时更新视图模型的 属性,并将带有标签的文本与视图模型 属性 绑定。 – @Muhammad Adeel Shoukat
这样做了并且需要将 ViewCell 添加到 ListView 中并且成功了。
我正在尝试获取一个选择器 SelectedItem 值以在另一个页面上使用,但它似乎并不想工作。
我在这里找到了 "solution":
public static class PickerPersist
{
public static string pickerSelectedItem = "test";
}
并包含 xmlns:local="clr-namespace:C971"
和绑定到 Text="{Binding x:static local:PickerPersist.PickerSelectedItem}"
的标签,并且在显示标签时正确显示 "test"。
我的问题是,如何获取要显示的实际 SelectedItem 的字符串?选择器本身在另一个页面上,我能够使用 var pickerItem = (Terms)termpicker.SelectedItem;
使其正确显示
我尝试将 PickerSelectedItem
设置为 Page1.termPicker.SelectedItem;
,但返回错误 "Page1.termPicker is inaccessible due to its protection level"。
通过在上一页 class 上设置 public static string Picked;
,然后将变量 pickerItem
设置为 Picked
,我能够消除错误。但是,它似乎并没有转移到新页面。如果我在将 pickeritem
设置为 Picked
后休息,一切都表明它确实设置正确,但它在 PickerPersist
中显示为空,因此不会显示在新页面上。
我希望这一切都有意义。 你能提供的任何帮助都会很棒!!谢谢! 此处提供完整代码:https://github.com/yax51/C971.git
Xaml Picker 住的地方:
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"
mc:Ignorable="d"
xmlns:local="clr-namespace:C971"
x:Class="C971.Page1"
Title="Term Selector">
<ContentPage.Content>
<StackLayout>
<Picker x:Name="termPicker"
ItemsSource="{Binding Terms}"
ItemDisplayBinding="{Binding TermName}"
Title="Select a term"
IsEnabled="True"
SelectedItem="{x:Static local:PickerPersist.PickerSelectedItem}"/>
Xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public static String Picked;
public Page1()
{
InitializeComponent();
}
...
void TermGoClicked(object sender, EventArgs e)
{
var pickerItem = (Terms)termPicker.SelectedItem;
if (pickerItem == null)
{
DisplayAlert("Error", "Please Select a term", "Ok");
}
else
{
Picked = pickerItem.TermName;
Navigation.PushAsync(new TermPage());
}
}
静态class
public static class PickerPersist
{
public static string PickerSelectedItem = Page1.Picked;
}
新页面上的标签
<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"
mc:Ignorable="d"
xmlns:local="clr-namespace:C971"
x:Class="C971.TermPage">
<ContentPage.Content>
<StackLayout>
<Label x:Name="test" Text="{x:Static local:PickerPersist.PickerSelectedItem}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
使用 Picker 侦听器 SelectedIndexChange,创建一个视图模型,当所选项目发生变化时更新视图模型的 属性,并将带有标签的文本与视图模型 属性 绑定。 – @Muhammad Adeel Shoukat
这样做了并且需要将 ViewCell 添加到 ListView 中并且成功了。