gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:29:55 27 4
gpt4 key购买 nike

我正在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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com