如何填充文本框 - Windows Phone 8.1

时间:2016-04-08 21:28:32

标签: c# xaml windows-phone-8.1 windows-phone

当我从另一个xaml(菜单)中选择项目时,我想要做的是基本上在一个xaml(Basket)中填充带有项目名称的文本框。我的代码如下:

Menu.xaml

在这里,当我点击一个按钮时,我希望它自动出现在Basket.xaml的文本框中。

    public class PassedData
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    private void Vanbtn_Click(object sender, RoutedEventArgs e)
    {


        Frame.Navigate(typeof(Basket), new PassedData { Name = "Cheese" });
    }

    private void LCbtn_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Basket), new PassedData { Name = "Crisp" });
    }

Basket.xaml

       protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
    /**    ItemChosentxt.Text = data.Name;
        ItemChosentxt2.Text = data.Name;
        ItemChosentxt3.Text = data.Name;
        ItemChosentxt4.Text = data.Name;**/


    }

注释掉的部分会添加所选项目的名称。而不只是添加到一个文本框添加到所有文本框。即如果我在Menu.xaml中选择了vanilla btn,则名称将出现在所有4个文本框中,而不是仅显示在一个文本框中。所以我决定在每个文本框中实现代码:

private void ItemChosentxt_TextChanged(object sender, TextChangedEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
        ItemChosentxt.Text = data.Name;
    }

    private void SecItem_TextChanged(object sender, TextChangedEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
        SecItem.Text = data.Name;
    }

这会在e.Parameter下面显示错误代码 -

Severity    Code    Description Project File    Line    Suppression State
    Error   CS0103  The name 'data' does not exist in the current context    

    Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'TextChangedEventArgs' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'TextChangedEventArgs' could be found (are you missing a using directive or an assembly reference?) 

我正在开发一个购物应用程序,例如如果用户从菜单中选择“Vanilla”和“Cake”,如何将名称显示在Basket文本框中?我是应用程序开发的新手,并为长期问题道歉。

1 个答案:

答案 0 :(得分:0)

问题是你没有用PassedData提供TextChangedEventArgs e。

编译器专门对你大喊大叫

  1. 用于说e.Paramenter(TextChangedEventArgs没有"参数"成员)
  2. 用于使用"数据",由于1而未定义。

  3. 但是,我建议采用完全不同的方法。只需使用listview(和数据绑定)。
    示例:

    <强> Basket.xaml

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding PassedData}" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <TextBox Grid.Column="0" Text="{Binding Name}" />
                        <TextBox Grid.Column="1" Text="{Binding Value}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

    <强> Basket.xaml.cs

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
        // namespace ...
    
    public sealed partial class Basket : Page
    {
        private static ObservableCollection<Menu.PassedData> passedData = new ObservableCollection<Menu.PassedData>();
        public ObservableCollection<Menu.PassedData> PassedData
        {
            get { return passedData; }
            set { passedData = value; }
        }
    
    
        /*Events*/
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        /*Methods*/
    
        /// Fires PropertyChangedEventHandler, for bindables
        protected void OnPropertyChanged(string name)
        {
            var ev = PropertyChanged;
            if (ev != null)
            {
                ev(this, new PropertyChangedEventArgs(name));
            }
        }
    
        public Basket()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Menu.PassedData data = e.Parameter as Menu.PassedData;
            /**    ItemChosentxt.Text = data.Name;
                ItemChosentxt2.Text = data.Name;
                ItemChosentxt3.Text = data.Name;
                ItemChosentxt4.Text = data.Name;**/
            if (data != null)
            {
                PassedData.Add(data);
            }
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(Menu));
        }
    }
    

    }