Xaml模板未应用于Windows手机中的页面

时间:2014-08-23 07:03:22

标签: c# xaml windows-phone-8.1

我正在制作使用页面导航的Windows手机应用程序。因为每个页面的布局类似,所以我为应用程序中的所有页面创建了一个自定义基页类。问题是模板未应用于模板页面类。

这就是我在generic.xaml文件中为基页定义模板的方法:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TfsExplorer.Client.UI"
xmlns:common="using:TfsExplorer.Client.UI.Common">

<Style TargetType="common:ModuleTemplatePage" x:Key="Omfg">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="common:ModuleTemplatePage">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80*" />
                        <RowDefinition Height="500*" />
                    </Grid.RowDefinitions>
                    <ListView x:Name="LstViewNavigationHistory" Grid.Row="0">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <ItemsStackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                    </ListView>
                    <Frame x:Name="FrameModuleContent" HorizontalAlignment="Left" Margin="0,0,0,0" Grid.Row="1"
                           Width="400" Content="{TemplateBinding ModuleContent}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

所有自定义内容都会进入框架内容属性。我设置了依赖项属性,如果它被更改,则用新值更新框架的内容。

现在,在代码文件中,我定义了基类以及新的依赖项属性:

[TemplatePart(Name = "FrameModuleContent", Type = typeof(Frame))]
public class ModuleTemplatePage : Page
{
    #region Static Fields

    private static void ModuleContentPropertyChangedCallback(
        DependencyObject dependencyObject, 
        DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var templatePage = dependencyObject as ModuleTemplatePage;

        if (templatePage != null)
        {
            var contentFrame = templatePage.GetTemplateChild("FrameModuleContent") as Frame;

            if (contentFrame != null)
            {
                contentFrame.Content = dependencyPropertyChangedEventArgs.NewValue;
            }
        }
    }

    #endregion

    #region Constructors and Destructors

    public ModuleTemplatePage()
    {
        this.DefaultStyleKey = typeof(ModuleTemplatePage);
    }

    #endregion

    #region Public Properties

    public ModuleTemplateContent ModuleContent
    {
        get
        {
            return this.GetValue(ModuleContentProperty) as ModuleTemplateContent;
        }

        set
        {
            this.SetValue(ModuleContentProperty, value);
        }
    }

    #endregion
    }

Bur这不起作用。没有模板应用于页面。我重写了OnTemplateApply方法,以便在应用模板时进行侦听,并且永远不会发生。

我在主窗口手机程序集中将这些页面声明为另一个程序集,我将这个generic.xml的引用添加到App.xaml中,如下所示:

<Application
x:Class="TfsExplorer.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TfsExplorer.Client">

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <ImageBrush x:Key="HubBackgroundImageBrush" ImageSource="Assets/HubBackground.png" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="HighContrast">
                <ImageBrush x:Key="HubBackgroundImageBrush" ImageSource="{x:Null}" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///TfsExplorer.Client.UI/Themes/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

包含generic.xaml文件的程序集名为TfsExplorer.Client.UI,我知道它被正确引用,因为在visual studio中添加generic.xaml时,我得到了程序集中所有元素的下拉列表。

0 个答案:

没有答案
相关问题