导航与图形绘制技术解析
立即解锁
发布时间: 2025-08-26 01:38:31 阅读量: 18 订阅数: 30 AIGC 


Silverlight 5编程与应用实例解析
### 导航与图形绘制技术解析
#### 1. 自定义内容加载器
自定义内容加载器是实现 `INavigationContentLoader` 接口的类,需要提供 `BeginLoad()`、`CanLoad()`、`CancelLoad()` 和 `EndLoad()` 方法。为了简化实现,可以使用 `PageResourceContentLoader` 类。以下是一个自定义内容加载器的示例:
```csharp
public class CustomContentLoader : INavigationContentLoader
{
private PageResourceContentLoader loader = new PageResourceContentLoader();
public IAsyncResult BeginLoad(Uri targetUri, Uri currentUri,
AsyncCallback userCallback, object asyncState)
{
return loader.BeginLoad(targetUri, currentUri, userCallback, asyncState);
}
public bool CanLoad(Uri targetUri, Uri currentUri)
{
return loader.CanLoad(targetUri, currentUri);
}
public void CancelLoad(IAsyncResult asyncResult)
{
loader.CancelLoad(asyncResult);
}
public LoadResult EndLoad(IAsyncResult asyncResult)
{
return loader.EndLoad(asyncResult);
}
}
```
这个示例提供了基本结构,可用于创建更复杂的内容加载器,如 `AuthenticatingContentLoader`。`AuthenticatingContentLoader` 类需要添加两个新属性:`LoginPage` 和 `SecuredFolder`,用于指定登录页面和安全页面所在的文件夹。
```csharp
public class AuthenticatingContentLoader : INavigationContentLoader
{
private PageResourceContentLoader loader = new PageResourceContentLoader();
public string LoginPage { get; set; }
public string SecuredFolder { get; set; }
...
}
```
`BeginLoad()` 方法需要进行微调,以确定用户是否已登录。如果用户未登录且请求的是安全文件夹中的页面,则将用户重定向到登录页面。
```csharp
public IAsyncResult BeginLoad(Uri targetUri, Uri currentUri,
AsyncCallback userCallback, object asyncState)
{
if (!App.UserIsAuthenticated)
{
if ((System.IO.Path.GetDirectoryName(targetUri.ToString()).Trim('\\') ==
SecuredFolder) && (targetUri.ToString() != LoginPage))
{
// Redirect the request to the login page.
targetUri = new Uri(LoginPage, UriKind.Relative);
}
}
return loader.BeginLoad(targetUri, currentUri, userCallback, asyncState);
}
```
为了跟踪用户是否登录,需要在 `App` 类中添加一个布尔标志:
```csharp
public partial class App : Application
{
public static bool UserIsAuthenticated { get; set; }
...
}
```
#### 2. 使用自定义内容加载器
使用自定义内容加载器的步骤如下:
1. 为项目命名空间映射 XML 命名空间,以便使用内容加载器。
```xml
<UserControl x:Class="CustomContentLoader.MainPage"
xmlns:local="clr-namespace:CustomContentLoader" ... >
```
2. 设置 `Frame.ContentLoader` 属性,并同时设置自定义内容加载器的属性。
```xml
<navigation:Frame x:Name="mainFrame" UriMapper="{StaticResource UriMapper}">
<navigation:Frame.ContentLoader>
<local:AuthenticatingContentLoader LoginPage="/Login.xaml"
SecuredFolder="SecurePages"></local:AuthenticatingContentLoader>
</navigation:Fram
```
0
0
复制全文
相关推荐










