简体   繁体   English

Xamarin表格如何将Entry值从一个类传递到另一个类List?

[英]Xamarin forms how to pass Entry value from one to class to another class List?

I want to pass inserted number value from page where I am inserting value to another page list and update ListView with new value. 我想将要插入值的页面的插入数字值传递给另一个页面列表,并用新值更新ListView。

Here is my AddCardPage xaml: 这是我的AddCardPage xaml:

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

here is AddCardPage xaml.cs method: 这是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) };
}

Here is my CardsPage class where I am already declared some data: 这是我的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());
    }

and CardsPage xaml with ListView: 和CardsPage xaml与ListView:

<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>

How to pass inserted Entry value from page to another class List and update ListView content? 如何将插入的Entry值从页面传递到另一个类List并更新ListView内容?

In the page you want to get the value change it's constructor to accept the value and pass that value from the calling page. 在您要获取值更改的页面中,其构造函数接受该值并从调用页面传递该值。

Eg: 例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM