1
votes

I am creating simple app on Silverlight 4. In folder of View I have 2(Silverlight Pages) also in ViewModel I have 2 ViewModels. In Silverligh project I have UserControl that hold one of page. I need simple example of using navigation. For example I click on button, call some method in ViewModel and this method redirect me on another Silverligh Page.Please help me, I'm suffering 3 day, and I found only very hard examples, but I bealive in simple.

<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>

That was a UserControll that hold first Page, I need navigate to another Page. Thank You very much!

1
Does this help? [Navigation alternatives, works in a user control][1] [1]: stackoverflow.com/questions/9931669/… - bperreault

1 Answers

2
votes

u should use a Frame control from Navigation framework(silverlight tookit). then u can try such approach

<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}" />

and the appropriate code for the 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));
    }
}

Also, u should allways use an UriMapper. It is a nice practice:

<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>