我如何从“外部"导航Silverlight 中的 .cs 文件 [英] How can I navigate from an "external" .cs file in Silverlight

查看:45
本文介绍了我如何从“外部"导航Silverlight 中的 .cs 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道标题不是很清楚,我会试着更好地解释自己.

I know the title isn't very clear, I'll try to explain myself better.

我的 MainPage.xaml 中有一个列表框,我想以某种方式填充它.为此,我有一个没有附加 xaml 的 Home.cs 文件,其构造函数是这样的:

I've got in my MainPage.xaml a Listbox that I want to populate in a certain way. On that purpose, I have a Home.cs file with no xaml attached, whose constructor is like this:

public Home(string sid, ListBox lb){...}

在这个构造函数中,我使用 Webclient 执行了几个 POST 请求,并且在 UploadStringCompleted 方法中,我创建了几个元素、堆栈面板、文本块和一个超链接按钮.

In this constructor I do several POST requests with Webclient and, in the UploadStringCompleted method, I create several elements, stackpanels, textblocks and a hyperlinkbutton.

StackPanel bigPanel = new StackPanel();
bigPanel.Width = 456;

HyperlinkButton hyperlink = new HyperlinkButton();
hyperlink.Content = friend.name + " " + person.surname;
hyperlink.Click += new RoutedEventHandler(hyperlink_Click);
bigPanel.Children.Add(hyperlink);

lb.Items.Add(bigPanel);

问题来了.我想让这个超链接导航到另一个页面 (FriendPage.xaml),但我无法实现,因为如果我尝试添加 NavigationService.Navigate(new Uri("/FriendPage.xaml", Urikind.Relative)) 那么它失败,它说非静态方法 Navigate 需要一个对象引用".

And here comes the problem. I want this hyperlink to navigate to another page (FriendPage.xaml), but I can't achieve it, because if I try to add NavigationService.Navigate(new Uri("/FriendPage.xaml", Urikind.Relative)) then it fails, it says "An object reference is required for the non-static method Navigate".

据我所知,这行代码应该在 MainPage.xaml 中,那么,有没有办法做到这一点?或者将 hyperlink.Click 事件处理程序放在 MainPage.xaml.cs 中并将其绑定到尚不存在的超链接按钮?

As far as I know, this code line should be in MainPage.xaml, so, is there a way to do this? Or to place the hyperlink.Click event handler in MainPage.xaml.cs and bind it to a hyperlinkbutton that does not exist yet?

谢谢

推荐答案

您不能使用 NavigationService.Navigate 函数,因为 NavigationService 在这里是一个类,而不是一个对象.我认为您必须将 NavigationService 添加到 home 的构造函数中的参数中,然后您可以使用它.像这样:

You can't use the function NavigationService.Navigate because NavigationService is a class here, not an object. I think you have to add NavigationService to the parameters in your constructor for home, which you can use then. Like this:

public void nav(NavigationService ANavigationService)
{
   ANavigationService.Navigate(new Uri(""));
}

MainPage 应该有一个 NavigationService 的实例,你可以把它传给 Home

MainPage should have an instance of NavigationService, which you can pass to Home

您可以通过以下方式在 MainPage 中获取导航服务的实例:

You can get the an instance of the navigationservice in MainPage with this:

//in mainpage
NavigationService navService = NavigationService.GetNavigationService(this);

这篇关于我如何从“外部"导航Silverlight 中的 .cs 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆