Silverlight导航与图形绘制技术详解
立即解锁
发布时间: 2025-08-26 01:40:47 阅读量: 16 订阅数: 27 AIGC 

### Silverlight 导航与图形绘制技术详解
#### 1. 创建自定义内容加载器
在应用程序开发中,自定义内容加载器是一个实现 `INavigationContentLoader` 接口的类。该接口要求提供以下方法:`BeginLoad()`、`CanLoad()`、`CancelLoad()` 和 `EndLoad()`。虽然正确实现这些方法具有一定难度,但可以采用一种简便的方式。只需将 `PageResourceContentLoader` 的实例定义为成员字段,并在内容加载器的不同阶段调用其方法。
以下是一个自定义内容加载器的示例,它使用 `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);
}
}
```
这个简单的示例为身份验证内容加载器提供了基本结构。身份验证内容加载器需要两个新属性,允许用户指定登录页面和包含安全页面的文件夹:
```csharp
public class AuthenticatingContentLoader : INavigationContentLoader
{
private PageResourceContentLoader loader = new PageResourceContentLoader();
public string LoginPage { get; set; }
public string SecuredFolder { get; set; }
...
}
```
`CanLoad()`、`CancelLoad()` 和 `EndLoad()` 方法保持不变,但 `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. 使用自定义内容加载器
使用自定义内容加载器类似于在 XAML 中使用其他自定义元素。具体操作步骤如下:
1. 为项目命名空间映射一个 XML 命名空间,以便可以使用内容加载器。例如,项目名为 `CustomContentLoader`:
```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:Frame.ContentLoader>
</navigation:Frame>
```
3. 编写登录页面的代码。在示例中,登录页面仅检查硬编码的密码,但更实际的做法是调用某种 Web 服务来在 Web 服务器上对用户进行身份验证。重要的是,一旦用户通过身份验证,必须将 `UserIsAuthenticated` 标志设置为 `true`,并调用 `NavigationService.Refresh()` 方法,该方法将重复整个导航序列。
```csharp
private void cmdLogin_Click(object sender, RoutedEventArgs e)
{
// Use a hard-coded password. A more realistic application would call a remote
// authentication service that runs on an ASP.NET website.
if (txtPassword.Text == "secret")
{
App.UserIsAuthenticated = true;
navigationService.Refresh();
}
}
```
#### 3. Silverlight 2D 绘图基础
Silverlight 的 2D 绘图支持是许多高级功能的基础,如自定义绘制控件、交互式图形和动画。即使不打算为应用程序创建自定义图形,也需要对 Silverlight 的绘图基础知识有扎实的理解。
##### 3.1 基本形状
在 Silverlight 用户界面中绘制 2D 图形内容的最简单方法是使用形状。形状是表示简单线条、椭圆、矩形和多边形的专用类,技术上称为绘图基元。可以将这些基本元素组合起来创建更复杂的图形。
Silverlight 中形状的重要特点是它们都派生自 `FrameworkElem
0
0
复制全文
相关推荐









