2016-04-08 132 views
-1

當我從另一個xaml(菜單)中選擇項目時,我想要做的事情基本上是在一個xaml(Basket)中填充項目名稱的文本框。我的代碼如下:如何填充文本框-Windows Phone 8.1

Menu.xaml

在這裏,當我點擊一個按鈕,我希望它automaticallly出現在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?) 

我正在開發一個購物應用程序,例如如果用戶從菜單中選擇「香草」和「蛋糕」,我該如何讓名稱顯示在籃子文本框中?我對應用程序開發不熟悉,並對長期問題表示歉意。

回答

0

問題是您沒有爲PassedData提供TextChangedEventArgs e。

編譯器是專門在你

  1. 喊說e.Paramenter(TextChangedEventArgs不具有「參數」構件)
  2. 用於使用「數據」,這是因爲未定義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)); 
    } 
} 

}

相關問題