Jx.Cms開發筆記(三)-Views主題動態切換


效果展示

我們可以在后台動態切換主題

theme.png

目前Jx.Cms有兩個主題,其中一個是默認主題,另一個是仿的Blogs主題。

我們可以通過點擊啟用按鈕來動態切換兩個主題。

default.pngblogs.png

實現方法

首先寫一個實現IViewLocationExpander接口的類,我這里命名為TemplateViewLocationExpander.

public class TemplateViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
        context.Values["template"] = ThemeUtil.GetThemeName();
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (!context.AreaName.IsNullOrEmpty())
        {
            return viewLocations;
        }
        var themeName = context.Values["template"] ?? ThemeUtil.PcThemeName;
        string[] locations = { "/Pages/" + themeName + "/{1}/{0}.cshtml", "/Pages/" + themeName + "/{0}.cshtml", "/Pages/" + themeName + "/Shared/{0}.cshtml", "/Pages/Shared/{0}.cshtml" };
        return locations.Union(viewLocations.Where(x => !x.StartsWith("/Views/")));
    }
}

首先我們在PopulateValues中給context.Values設置一個值,context.Values是一個字典,所以我們可以在里面加一個template的key,如果在PopulateValues中context有變化,就會走到ExpandViewLocations方法中。

ExpandViewLocations中,我們首先把有Area的去除,因為Area內肯定不是我們的主題。

然后查找

string[] locations = { "/Pages/" + themeName + "/{1}/{0}.cshtml", "/Pages/" + themeName + "/{0}.cshtml", "/Pages/" + themeName + "/Shared/{0}.cshtml", "/Pages/Shared/{0}.cshtml" };

這里的內容,就可以到對應的主題下獲取內容了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM