Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

templates - UWP: how to setup namespace for templated control

I want to create a templated control and, for my "shared" project, have used the Add New Item dialog to create a default templated control. This builds but, of course, it is not yet used in my project.

How do I reference it in my main page ? I see plenty of examples but none, that I can find, explain how to setup the namespaces.

It created ThemesGeneric.xaml and TitlebarTemplate.cs (the latter being the name I chose for my template).

Here's my Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UnoTest.Shared">

    <Style TargetType="local:TitlebarTemplate" >
      ...
    </Style>
</ResourceDictionary>

And, here's a very condensed main page:

<Page
  x:Class="UnoTest.ListPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:UnoTest"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  >

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"  />
      ...
    </Grid.RowDefinitions>

    ... put template here ...
  </Grid>
</Page>

What namespace to I need to add to reference the template ? And, please include the syntax for actually referencing it.

I'm doing this for the Uno platform, but I think the syntax is the same as for UWP.

EDIT

I put this in my page:

<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Themes/Generic.xaml" />
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

But I still can't find a way to reference TitlebarTemplate.

question from:https://stackoverflow.com/questions/65924336/uwp-how-to-setup-namespace-for-templated-control

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

using:

xmlns:dt="UnoTest.Themes"

Should be sufficient. This would be your ProjectName.AnyDirectoryYouHave

Any xaml file you add to the Shared library will take the global namespace (without the Shared).

Notes:

We usually keep all the Styles related files in the UWP head, grouping the Styles in files by Controls.

enter image description here

And then merging all these Styles into the Application Dictionary found at the App.xaml file.

enter image description here

By doing this you will be able to access your Template and any other Style from any Page in the application without the need of importing it.

This means you could use your General Resource Dictionary and merge it as part of the Application Dictionary in your App and then just use it from your Page.

Hope this helps.-


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...