【问题标题】:Windows 8 navigation, passing an URI as parameterWindows 8 导航,将 URI 作为参数传递
【发布时间】:2012-03-28 12:15:23
【问题描述】:

我刚开始为 Windows 8 进行开发,在页面之间的导航中传递参数时遇到了一些困难。

我想要做的是:我有两个页面(page1 和 page2) 在 page1 中,有一个带有按钮的菜单。当我单击此按钮时,单击事件应将可能的 URI 或路径作为参数传递,这将在 page2 中设置图像的来源。

这是page1中的代码:

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    //this.Frame.Navigate(typeof(SplitPage), "ms-appx:/Imgs/1.png");
    this.Frame.Navigate(typeof(SplitPage), new Uri("ms-appx:/Imgs/1.png", UriKind.RelativeOrAbsolute));
}

和page2,用于接收Uri:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var imgSource = e.Parameter as ImageSource;
    this.imgParaPintar.Source = imgSource;
}

我注意到imgSource 什么也没收到,它保持为空。

那么,关于我做错了什么或我错过了什么的任何线索?

【问题讨论】:

  • 您正在传递Uri,然后将其转换为ImageSourceImageSource 不是 Uri,你为什么认为这样会起作用?
  • Image 类是否有 UriSource 参数?
  • @svick,我给出了我正在处理的两个选项,正如您在评论行中看到的那样。两者都不起作用。
  • 是的,stringUri 都不是 ImageSource,所以你不能转换成它。这就是为什么您的代码不起作用的原因。你觉得会怎么样?魔法?
  • “as”运算符将您的对象设置为空。您需要使用“As Uri”(或者更好的是下面的“未测试”答案)。

标签: c# xaml windows-8 microsoft-metro windows-runtime


【解决方案1】:

我认为您需要这样的代码(未经测试):

protected override void OnNavigatedTo(NavigationEventArgs e)
{
     this.imgParaPintar.Source = new BitmapImage((Uri)e.Parameter);
}

您可能需要将using Windows.UI.Xaml.Media.Imaging; 添加到您的文件中,如果您还没有的话。

【讨论】:

    【解决方案2】:

    值得注意的是,正如此处的 cmets 所述:

    http://answers.flyppdevportal.com/categories/metro/csharpvb.aspx?ID=4b847d71-9cd5-4457-add9-f68e457b23ff

    您只能将原始类型作为导航参数传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多