【问题标题】:Xamarin forms how to pass Entry value from one to class to another class List?Xamarin 表单如何将条目值从一个类传递到另一个类列表?
【发布时间】:2016-09-22 12:20:39
【问题描述】:

我想将插入的数值从我正在插入值的页面传递到另一个页面列表并使用新值更新 ListView。

这是我的 AddCardPage xaml:

<Entry x:Name="CardNr" Text="" Completed="Entry_Completed" Keyboard="Numeric" />

这里是 AddCardPage xaml.cs 方法:

void Entry_Completed(object sender, EventArgs e)
{
    var text = ((Entry)sender).Text; //cast sender to access the properties of the Entry
    new Cards { Number = Convert.ToInt64(text) };
}

这是我的 CardsPage 类,其中我已经声明了一些数据:

public CardsPage()
{
    InitializeComponent();
    base.Title = "Cards";
    List<Cards> cardsList = new List<Cards>()
    {
        new Cards { Number=1234567891234567, Name="dsffds M", ExpDate="17/08", Security=123 },
        new Cards { Number=9934567813535135, Name="Jason T", ExpDate="16/08", Security=132 },
        new Cards { Number=4468468484864567, Name="Carl S", ExpDate="17/01", Security=987 },
    };
    listView.ItemsSource = cardsList;  
        AddCard.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = new Command(() => OnLabelClicked()),
        });       
}
    private void OnLabelClicked()
    {
        Navigation.PushAsync(new AddCardPage());
    }

和带有 ListView 的 CardsPage xaml:

<ListView x:Name="listView">
  <ListView.Footer> 
    <StackLayout Padding="5,5" Orientation="Horizontal" >
      <Label x:Name="AddCard" Text="Add new Card" FontSize="15" />
    </StackLayout>
  </ListView.Footer>
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
          <Label Text="{Binding Number}" FontSize="15" />
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

如何将插入的Entry值从页面传递到另一个类List并更新ListView内容?

【问题讨论】:

  • 我不完全确定你在这里做什么,但它不只是简单地将EntryText 属性绑定到Number 吗? &lt;Entry x:Name="CardNr" Text="{Binding Number}" Completed="Entry_Completed" Keyboard="Numeric" /&gt;
  • @GeraldVersluis 是的,但是“Number”在另一个类中,所以如果我没有在任何地方声明我的 Cards 类,我该如何绑定?
  • 啊,我明白了,你使用任何 MVVM 框架之类的吗?您现在如何从一个页面导航到另一个页面?请使用代码更新您的问题。
  • @GeraldVersluis 更新

标签: c# list xaml listview xamarin.forms


【解决方案1】:

在您想要获取值更改的页面中,它的构造函数接受该值并从调用页面传递该值。

例如:

class Page1
{
  private async void OnSomeEvent()
  {
     await Navigation.PushAsync(new Page2("xyz"));
  }
}

class Page2
{
  public Page2(string myValue)
  {
  }
}

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2021-02-24
    • 2013-03-16
    • 2011-08-26
    • 2017-10-30
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多