2012-04-03 96 views
1

我正在Silverlight 4上創建簡單的應用程序。 在視圖的文件夾中我也有2個(Silverlight頁面)ViewModel我有2個ViewModels。 在Silverligh項目中,我有擁有其中一頁的UserControl。 我需要使用導航的簡單示例。 例如,我點擊按鈕,在ViewModel中調用一些方法,並將此方法重定向到另一個Silverligh Page.Please幫助我,我正在遭受3天的痛苦,而且我發現只有非常難的示例,但是我很簡單。Silverlight頁面與MVVM之間的導航

<UserControl x:Class="WebServerApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
      xmlns:pagescol="clr-namespace:SilverlightServerLib.View;assembly=SilverlightServerLib" 

    Width="Auto" Height="Auto"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <pagescol:SettingsPage/> 
    </Grid> 
</UserControl> 

這是一個持有第一頁的UserControll,我需要導航到另一頁。 非常感謝!

+0

這有幫助嗎? [導航替代,工作在一個用戶控制] [1] [1]:http://stackoverflow.com/questions/9931669/onnavigatedto-wont-be-triggered/9932057#9932057 – bperreault 2012-04-03 16:46:17

回答

2

你應該使用導航框架中的Frame控件(silverlight takeit)。 然後ü可以試試這樣的方法

<Button Content="Click"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Click"> 
      <i:EventTrigger.Actions> 
       <actions:NavigateAction UriPath="/Home" TargetName="MainFrame" /> 
      </i:EventTrigger.Actions> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Button> 

<navigation:Frame x:Name="MainFrame"       
        UriMapper="{StaticResource UriMapper}" /> 

,爲NavigateAction適當的代碼:

public class NavigateAction : TargetedTriggerAction<Frame> 
{ 
    public static readonly DependencyProperty UriPathProperty = 
     DependencyProperty.Register("UriPath", typeof(string), typeof(NavigateAction), null); 

    public string UriPath 
    { 
     get { return (string)GetValue(UriPathProperty); } 
     set { SetValue(UriPathProperty, value); } 
    } 

    protected override void Invoke(object parameter) 
    { 
     Target.Navigate(new Uri(UriPath, UriKind.RelativeOrAbsolute)); 
    } 
} 

此外,ü應該八方通使用UriMapper。這是一個不錯的做法:

<Application.Resources> 
    <sdk:UriMapper x:Key="UriMapper"> 
     <sdk:UriMapping MappedUri="/Views/TasksView.xaml" Uri="" /> 
     <sdk:UriMapping MappedUri="/Views/{view}View.xaml" Uri="/{view}" /> 
    </sdk:UriMapper> 
</Application.Resources>