【问题标题】:Add Items Dynamically to New Page - Windows Phone 8将项目动态添加到新页面 - Windows Phone 8
【发布时间】:2013-06-22 00:39:34
【问题描述】:

我正在尝试通过询问用户文本框的数量来动态添加文本框。 此代码正在运行,但正在将项目添加到当前位置。

NavigationService.Navigate(new Uri("/Page1.xaml?shouldDownload=true",     UriKind.Relative)); //Naviagte on new page
int num;
int.TryParse(textBox1.Text, out num); //Converting textbox to int
for (int i = 1; i <= num; i++)
{
    TextBox newtext = new TextBox();
    newtext.Text = i.ToString();
    newtext.Margin = new Thickness(300, (i * 80), 0, 0);
    newtext.Width = 124;
    newtext.Height = 68;
    //button.VerticalAlignment = 234;
    //Add contentpanel to your page if there's not one already.
    ContentPanel.Children.Add(newtext);
    newtext.Visibility = System.Windows.Visibility.Visible;
}

但我想将这些项目添加到不在此处的新页面(即:Page1)。

将获得帮助。

【问题讨论】:

    标签: c# windows-phone-7 windows-phone-8 windows-phone


    【解决方案1】:

    尝试通过导航参数将其传递到您的第二页

    string num = textBox1.Text;
    NavigationService.Navigate(new Uri("/Page1.xaml?shouldDownload=true&num="+num,     UriKind.Relative)); //Naviagte on new page
    

    在您的第二页中,您在 OnNavigatedTo 方法中解析结果

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        string numValue;
        if (NavigationContext.QueryString.TryGetValue("num", out numValue))
        {
            int num = Convert.ToInt32(numValue);
            // add 'em here
        }
    }
    

    【讨论】:

    • 我认为您不能以发送整数的方式发送整数。只能发送字符串...
    • 它工作正常,但你不能将它作为整数发送......在你的情况下,它仍然作为字符串接收,所以这就是我所说的你不能发送整数...... .
    • 它将它作为一个字符串发送,然后你通过 int.TryParse 或 Convert.ToInt32 将它解析为一个整数。它确实有效。
    • 看看我提供的样本。如果 QueryString 包含将其转换为 int 的值
    【解决方案2】:

    你可以试试这个……

    NavigationService.Navigate(new Uri("/Page1.xaml?shouldDownload=true&msg=" + extBox1.Text,     UriKind.Relative));
    

    然后在新页面的 onnavigation 方法上执行此操作..

     IDictionary<string, string> parameters = NavigationContext.QueryString;
     string number= parameters["msg"];
    

    然后从 int num =0;等等

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多