在MainLayout.razor或App.razor中添加MatBlazor MatThemeProvider

时间:2020-05-18 09:20:56

标签: blazor matblazor

MatBlazor文档说:“要对所有应用程序应用主题,可以在MainLayout.razor或App.razor中使用MatThemeProvider”

该示例在信息方面非常薄。

将代码段添加到MainLayout.razor或App.razor时,它只是不起作用。

我得到的错误是 “ InvalidOperationException:路由器组件需要为找到的参数提供一个值。”

您如何为整个应用应用主题?

App.razor

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">

        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <Authorizing>
                <p>Determining session state, please wait...</p>
            </Authorizing>
            <NotAuthorized>
                <Unauthorised />
            </NotAuthorized>
        </AuthorizeRouteView>

        <MatThemeProvider Theme="@theme">
            <Router AppAssembly=typeof(Pages.Dashboard).Assembly />
        </MatThemeProvider>

    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

@code
    {
    MatTheme theme = new MatTheme()
    {
        Primary = MatThemeColors.Orange._500.Value,
        Secondary = MatThemeColors.BlueGrey._500.Value
    };
}

1 个答案:

答案 0 :(得分:3)

你可以试试吗?

<MatThemeProvider Theme="@theme">
<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">

        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <Authorizing>
                <p>Determining session state, please wait...</p>
            </Authorizing>
            <NotAuthorized>
                <Unauthorised />
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>  
    <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</MatThemeProvider>

@code
    { MatTheme theme = new MatTheme()
        {
         Primary = MatThemeColors.Orange._500.Value,
         Secondary = MatThemeColors.BlueGrey._500.Value
        }; 
    }

请告诉我它是否有效。 :)