如何在DotVVM项目中实现MVVM Light INavigationService

时间:2018-11-15 18:12:20

标签: c# navigation mvvm-light dotvvm

我想在INavigationService项目中实现MVVM Light DotVVM接口;但我不知道该怎么做。我需要实现的最重要的方法是NavigateTo(string pageKey)方法。

我在SpaContentPlaceHolder中使用了MasterPage,并且我想通过调用SpaContentPlaceHolder方法来更改NavigateTo的内容(RouteName)。

1 个答案:

答案 0 :(得分:0)

如果您在视图模型中,则只需调用Context.RedirectToRoute("YourRoute", new { Param1 = something })

如果要从其他位置重定向,最简单的方法是创建INavigationService接口并实现它以调用IDotvvmRequestContext(已在ASP.NET Core依赖项中注册)上的方法。注射容器):

public interface INavigationService 
{
    void NavigateTo(string routeName, object routeParameters);
}

public class DotvvmNavigationService 
{
    private IDotvvmRequestContext context;

    public DotvvmNavigationService(IDotvvmRequestContext context) {
        this.context = context;
    }

    public void NavigateTo(string routeName, object routeParameters) {
        this.context.RedirectToRoute(routeName, routeParameters);
    }
}

然后,您只需在Startup.cs中将实现注册为作用域依赖项,就可以在任何需要的地方获取它。

services.AddScoped<DotvvmNavigationService>();