c# - 使用MVVM在Silverlight页面之间导航

标签 c# silverlight mvvm user-controls navigation

我正在Silverlight 4上创建简单的应用程序。
在View的文件夹中,我有2个(Silverlight页面),在ViewModel中,我也有2个ViewModels。
在Silverligh项目中,我具有保存页面之一的UserControl。
我需要使用导航的简单示例。
例如,我单击按钮,在ViewModel中调用某个方法,然后该方法将我重定向到另一个Silverligh Page。请帮助我,我经历了3天的苦难,我发现只有非常困难的示例,但是我很简单。

<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,我需要导航到另一页。
非常感谢!

最佳答案

您应该使用Navigation框架(silverlight Takeit)中的Frame控件。
那么你可以尝试这种方法

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

关于c# - 使用MVVM在Silverlight页面之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9997742/

相关文章:

c# - 使用 Visual Studio 在运行时创建本地数据库

c# - C# 中的乌尔都语语音识别

c# - 如何将 Windows 应用商店中的 Windows Phone 应用程序 (8.1 XAML) 迁移到 8.1 Silverlight?

.net - 将 Silverlight 4 升级到 5 问题 - 混合库?

c# - MVVM + 中介模式 : Registration of Mediator occurs too late

c# - 是否有可能将虚拟打印机与物理打印机区分开来?

c# 将消息发送到 linux 上的本地 syslog 服务

wpf - 以通用方式从可视化树中删除项目

c# - 根据 WPF 中的情况自定义菜单

.net - 始终在WPF应用程序中使用MVVM,或者替代模式仍然实用/有用吗?