深入探索Silverlight导航框架
立即解锁
发布时间: 2025-08-24 01:26:29 阅读量: 1 订阅数: 3 

### 深入探索Silverlight导航框架
#### 1. 导航框架的优势
Silverlight 3中的导航框架具有显著优势。在之前的版本中,实现类似导航功能需要大量工作,通常还需购买第三方控件或库。而Silverlight 3将此功能内置,减少了实现相同效果所需的代码量,生成的代码更简洁、易维护。此外,它还提供了浏览器历史记录支持和深度链接等额外功能。
深度链接允许直接链接到应用程序的特定状态。例如,一个应用程序加载时显示主页,用户点击主页上的链接可导航到产品列表页面,再点击产品可进入该产品的详细信息页面。借助导航框架,开发者能直接生成指向产品详细信息页面的链接。
#### 2. NavigationService对象的使用
有时候,需要从页面内部访问承载它的导航框架。NavigationService对象就能实现这一功能。以下是使用NavigationService对象的具体步骤:
1. 打开项目 `Ch7_NavAppFromScratch`。
2. 打开 `View1.xaml` 的XAML文件,在 `TextBlock` 下方添加一个按钮:
```xml
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock Text="View 1"
FontSize="60"
Foreground="Green"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Button Click="Button_Click"
Padding="10"
Content="Navigate to Inner View"
HorizontalAlignment="Center" />
</StackPanel>
</Grid>
```
3. 右键点击Silverlight项目,选择“添加” -> “新项”,选择“Silverlight页面”模板,命名为 `InnerView1.xaml`。
4. 在 `InnerView1.xaml` 的XAML文件中添加一个简单的 `TextBlock`:
```xml
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Inner View 1"
FontSize="40"
Foreground="Blue"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
```
5. 在 `View1.xaml` 的代码文件中添加 `Button_Click` 事件处理程序:
```csharp
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(
new Uri("/InnerView1.xaml", UriKind.Relative));
}
```
6. 运行解决方案,点击“View 1”链接,再点击“Navigate to Inner View”按钮,应用程序将在顶部框架中显示 `InnerView1.xaml` 的内容。
#### 3. 向导航页面传递数据
在HTML页面中,通过查询字符串(QueryString)向其他页面传递数据。在Silverlight导航应用程序中,可使用 `NavigationContext` 对象实现相同功能。例如,要获取查询字符串属性 `ProductID`,可使用以下语法:
```csharp
string productId = NavigationContext.QueryString["ProductID"].ToString();
```
以下是使用 `NavigationContext` 对象传递数据的具体步骤:
1. 打开项目 `Ch7_NavAppFromScratch`。
2. 打开 `View1.xaml` 的XAML文件,在按钮下方添加一个 `ComboBox`:
```xml
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock Text="View 1"
FontSize="60"
Foreground="Green"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Button Click="Button_Click"
Padding="10"
Content="Navigate to Inner View"
HorizontalAlignment="Center" />
<ComboBox Padding="10" Margin="10" x:Name="Color" Width="100">
<ComboBoxItem Content="Blue" IsSelected="True" />
<ComboBoxItem Content="Red" />
<ComboBoxItem Content="Green" />
</ComboBox>
</StackPanel>
</Grid>
```
3. 打开 `View1.xaml` 的代码文件,编辑 `Button_Click` 事件处理程序,将所选颜色作为查询字符串传递:
```csharp
private void Button_Click(object sender, RoutedEventArgs e)
{
string color = Color.SelectionBoxItem.ToString();
NavigationService.Navigate(
new Uri(string.Format("/InnerView1.xaml?Color={0}", color),
UriKind.Relative));
}
```
4. 打开 `InnerView1.xaml` 文件,在现有 `TextBlock` 下方添加第二个 `TextBlock`:
```xml
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Orientation="Vertical">
<TextBlock Text="Inner View 1"
x:Name="ViewHeader"
FontSize="40"
Foreground="Blue"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<TextBlock Text="(Blue)"
x:Name="ViewColor"
FontSize="30"
Foreground="Blue"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</StackPanel>
```
5. 打开 `InnerView1.xaml` 的代码文件,使用 `NavigationContext` 对象获取传递的颜色,并根据颜色更改 `TextBlock` 的颜色和文本:
```csharp
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string color = NavigationContext.QueryString["Color"].ToString();
Brush b;
switch (color)
{
case "Red":
b = new SolidColorBrush(Color.FromArgb(255,255,0,0));
ViewHeader.Foreground = b;
ViewColor.Foreground = b;
ViewColor.Text = "(Red)";
break;
case "Green":
b = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
ViewHeader.Foreground = b;
ViewColor.Foreground = b;
ViewColor.Text = "(Green)";
break;
default:
b = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
ViewHeader.Foreground = b;
ViewColor.Foreground = b;
ViewColor.Text = "(Blue)";
break;
}
}
```
6. 运行解决方案,选择颜色并点击“Navigate to Inner View”按钮,`InnerView1.xaml` 的内容将根据所选颜色显示不同的文本颜色。
#### 4. Uri映射
在导航
0
0
复制全文
相关推荐










