MEF& Silverlight:如何导航到另一个XAP中的页面? [英] MEF & Silverlight: How do I navigate to a page in another XAP?

查看:66
本文介绍了MEF& Silverlight:如何导航到另一个XAP中的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何执行此操作非常迷惑.

I'm very lost on how to do this.

我了解MEF,并且可以使用许多博客文章中普遍使用的DeploymentCatalogService从另一个XAP加载服务和类.我不了解的是如何从另一个XAP实际加载和导航到PAGE.

I understand MEF and can load services and classes from another XAP using the ubiquitous DeploymentCatalogService found in many blog posts. What I don't understand is how to actually load and navigate to a PAGE from another XAP.

我想做的是让我的主应用程序能够调用NavigationService并为其提供应该存在的Page的名称.像:

What I'd like to do is have my main application be able to call the NavigationService and provide it with the name of a Page that should exist. Like:

 NavigationService.Navigate(new Uri("/Test", UriKind.Relative));

要将此页面提供给应用程序,取决于其他XAP文件之一.但是,我不知道如何使这项工作.似乎每个人都在构建一些复杂的基础结构来处理这种情况,这非常烦人且过于复杂.

It's up to one of the other XAP files to provide this page to the application. However, I have no idea how to make this work. It seems like everyone is building up some complicated infrastructure to handle this situation and it's very annoying and overly complicated.

有一种简单的方法吗?

推荐答案

我看到的一些示例比它们所需要的更为复杂,但这并不是一个简单的任务.我花了两天的时间才分解出可以找到的示例,然后才真正了解正在发生的事情.

Some of the examples I've seen are more complicated than they need to be, but this is not a simple task. It took me 2 days to break down the examples I could find before I really understood what is going on.

为此,您需要创建至少两个类. 一个实现INavigationContentLoader的类,它将完成所有工作. 实现IAsyncResult的类.该对象将在INavigationContentLoader周围传递,因此请使用它来跟踪您的工作.

To do this you need to create minimum two classes. A class that implements INavigationContentLoader, this will do all of the work. A class that implements IAsyncResult. This object will passed around the INavigationContentLoader, so use it to keep track of what you are doing.

您的INavgiationContentLoader应该执行以下操作.

Your INavgiationContentLoader should do the following.

BeginLoad

BeginLoad

  1. 测试Uri是属于当前XAP还是已经加载的XAP.
    • 如果没有,请使用DeploymentCatalog下载XAP.将DeploymentCatalog存储在AggregateCatalog中.
  1. Test if the Uri belongs to the current XAP or a XAP that has already been loaded.
    • If not, use DeploymentCatalog to download the XAP. Store the DeploymentCatalog in an AggregateCatalog.

CanLoad

您可以尝试在此处添加一些逻辑,以测试您是否有权访问指定的XAP,或者可以返回true并完成操作.

You can try to put some logic in here to test of you have access to the specified XAP, or you can just return true and be done with it.

取消加载

在IAsyncResult中设置一个状态,以使您知道该操作已被取消.

Set a state within the IAsyncResult to let you know the operation has been cancelled.

EndLoad

  1. 获取存储在IAsyncResult中的页面,将其包装在LoadResult中并返回.


此INavgiationContentLoader将要加载的所有页面都需要标记为ExportAttribute,以便ExportFactory可以找到它们.

All pages that will be loaded by this INavgiationContentLoader will need to be marked with an ExportAttribute so that ExportFactory can find them.

编辑

我的INavigationContentLoader

My INavigationContentLoader

http://pastebin.com/cT1mJ4Ve

我的IAsyncResult

My IAsyncResult

http://pastebin.com/xHWHT4pr

要在页面上使用的ExportAttribute.您需要在所有页面上,甚至在本地XAP中也需要此页面.

ExportAttribute to use on pages. You need this on all pages, even the ones in the local XAP.

http://pastebin.com/nTJ27mWz

IExportPageMetaData.这是MEF使用的合同.

IExportPageMetaData. This is the contract that MEF uses.

http://pastebin.com/8fdwx2Kn

使用方法:

声明您的导航:像这样的框架

Declare your navigation:Frame like this

<navigation:Frame x:Name="ContentFrame"
                  Source="/Home"`
                  Grid.Column="1"> 
    <navigation:Frame.ContentLoader>
        <navUtil:DynamicContentLoader />
    </navigation:Frame.ContentLoader>
</navigation:Frame>

从另一个XAP到页面的超链接按钮.

HyperlinkButton to a page from another XAP.

<HyperlinkButton Content="Page from another XAP"
                 NavigateUri="/NavigateUriFromExportPageAttribute"
                 navUtil:DynamicContentLoader.Xap="UriToOtherXap" />

从此XAP指向页面的超链接按钮.

HyperlinkButton to a page from this XAP.

<HyperlinkButton Content="Page from this XAP"
                 NavigateUri="/NavigateUriFromExportPageAttribute" />

您不需要UriMapper,也不需要放置Page.xaml的路径. MEF将从页面读取ExportPageAttribute并以这种方式找到Uri.

You do not need a UriMapper, nor do you need to put the Path of the Page.xaml. MEF will read the ExportPageAttribute from the page and find the Uri that way.

这篇关于MEF&amp; Silverlight:如何导航到另一个XAP中的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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