如何填充文本框-Windows Phone 8.1
Posted
技术标签:
【中文标题】如何填充文本框-Windows Phone 8.1【英文标题】:How to populate textboxes-Windows Phone 8.1 【发布时间】:2016-07-30 07:26:07 【问题描述】:当从另一个 xaml(菜单)中选择项目时,我要做的基本上是在一个 xaml(篮子)中使用项目名称填充文本框。我的代码如下:
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?)
我正在开发一个购物应用程序,例如如果用户从菜单中选择“香草”和“蛋糕”,我怎样才能使名称显示在篮子文本框中?我是应用程序开发的新手,并为这个冗长的问题道歉。
【问题讨论】:
【参考方案1】:问题是您没有为 TextChangedEventArgs e 提供 PassedData。
编译器专门对你大喊大叫
-
说 e.Paramenter(TextChangedEventArgs 没有“参数”成员)
用于使用未定义的“数据”,因为 1。
但是,我推荐一种完全不同的方法。只需使用列表视图(和数据绑定)。 例子:
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));
【讨论】:
以上是关于如何填充文本框-Windows Phone 8.1的主要内容,如果未能解决你的问题,请参考以下文章
将数据返回到上一页 Windows Phone 8.1 Windows RunTime
带有图像标签的 Binging 字符串 - Windows Phone 8.1 UWP