微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

闲时扯谈 - Silverlight母版页的实现

一个做sl的朋友到处问Silverlight怎么实现类似于母版页这样的框架功能

查了一下 Silverlight确实没有提供类似的功能……但是 开玩笑 攻城狮耶 这么点小东西非得提供了才能用吗?

以下是解决方案:


新建一个随便什么的用户控件 我的叫做ManageFrame

首先是后台.cs代码 木有神马麻烦的 把继承的类改成 ContentControl


public partial class ManageFrame : ContentControl
    {
        public ManageFrame()
        {
            InitializeComponent();
        }
    }

把继承类也改成


<ContentControl x:Class="IWorld.Admin.Controls.ManageFrame"
    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"
    xmlns:local="clr-namespace:IWorld.Admin.Controls;assembly=IWorld.Admin.Controls"
    mc:Ignorable="d" MinWidth="1020">
</ContentControl>

然后写模版


<ContentControl.Template>
        <ControlTemplate>
            <!--主体框架-->
            <Grid x:Name="root">
                <Grid.Background>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="#FFF0F0F0" Offset="1"/>
                        <GradientStop Color="#FFF6F6F6"/>
                    </LinearGradientBrush>
                </Grid.Background>
                <Grid.ColumnDeFinitions>
                    <ColumnDeFinition Width="220"></ColumnDeFinition>
                    <ColumnDeFinition></ColumnDeFinition>
                </Grid.ColumnDeFinitions>
                <!--以下是侧边栏-->
                <local:Sidetool SelectedText_Menu="{Binding SelectedText_Menu}" 
                                    SelectedText_Page="{Binding SelectedText_Page}"  
                                    Username="{Binding Username}" 
                                    Group="{Binding Group}" 
                                    JumpCommand="{Binding JumpCommand}"
                                    EditaccountCommand="{Binding EditaccountCommand}" 
                                    logoutCommand="{Binding logoutCommand}"/>
                <!--以上是侧边栏-->
                <!--以下是界面框架-->
                <Grid Grid.Column="1">
                    <Grid.RowDeFinitions>
                        <RowDeFinition></RowDeFinition>
                        <RowDeFinition Height="75"></RowDeFinition>
                    </Grid.RowDeFinitions>

                    <!--界面内容将插插入在这里-->
                    <ContentControl Foreground="{TemplateBinding Property=Foreground}">
                        <ContentPresenter/>
                    </ContentControl>

                    <!--以下是页脚-->
                    <!--技术支持图标-->
                    <local:TechnicalSupport_Button Width="75" Height="75" Grid.Row="1"
                                                       HorizontalAlignment="Right" Margin="0,30,0"/>
                    <!--版权信息-->
                    <TextBlock Text="版权所有 盗版必究 © 2013" Margin="0,109,10" Grid.Row="1"
                               HorizontalAlignment="Right" VerticalAlignment="Bottom"
                               Foreground="{TemplateBinding Property=Foreground}"/>
                    <!--以上是页脚-->
                </Grid>
                <!--以上是界面框架-->
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>

注:侧边栏自定义控件 可以无视 大家不用去思考local这个名称空间是哪里来的(可以无视它)

模版随便写 最重要的是 <ContentPresenter/> 

把它放在你想要插入内容的地方 然后 就可以了


用起来 这样用:

<local:ManageFrame>
        <Grid>
            <TextBlock Text="Hrllo world"/>
        </Grid>
    </local:ManageFrame>

它使用时候唯一要注意的 也是唯一的缺点 

它里面只能放一个子元素 理由不解释 请自行查阅相关资料 知道结果就可以了

不过这不影响使用 在所有你想弄的东西之外再套一个grid就可以了——麻烦那么一点点 不过 能用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐