[英]UWP: Template for custom control fails with “Failed to create a 'Windows.UI.Xaml.DependencyProperty' from the text” error
I am trying to create a custom ContentDialog
control. 我试图创建一个自定义
ContentDialog
控件。 My custom class likes: 我的自定义类喜欢:
public class ContentDialogEx : ContentDialog
{
public string PrimaryButtonGlyph
{
get => (string)GetValue(PrimaryButtonGlyphProperty);
set => SetValue(PrimaryButtonGlyphProperty, value);
}
public string SecondaryButtonGlyph
{
get => (string)GetValue(SecondaryButtonGlyphProperty);
set => SetValue(SecondaryButtonGlyphProperty, value);
}
public string CloseButtonGlyph
{
get => (string)GetValue(CloseButtonGlyphProperty);
set => SetValue(CloseButtonGlyphProperty, value);
}
public static readonly DependencyProperty PrimaryButtonGlyphProperty = DependencyProperty.Register(nameof(PrimaryButtonGlyph), typeof(string), typeof(ContentDialogEx), new PropertyMetadata(""));
public static readonly DependencyProperty SecondaryButtonGlyphProperty = DependencyProperty.Register(nameof(SecondaryButtonGlyph), typeof(string), typeof(ContentDialogEx), new PropertyMetadata(""));
public static readonly DependencyProperty CloseButtonGlyphProperty = DependencyProperty.Register(nameof(CloseButtonGlyph), typeof(string), typeof(ContentDialogEx), new PropertyMetadata(""));
public ContentDialogEx()
{
Template = PrismUnityApplication.Current.Resources["ContentDialogExTemplate"] as ControlTemplate;
}
}
The control template looks like: 控件模板如下所示:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MyApp.Xaml.Controls">
<ControlTemplate x:Key="ContentDialogExTemplate" TargetType="controls:ContentDialogEx">
...
</ControlTemplate>
</ResourceDictionary>
The contents of the control template are copied from the default template and I have confirmed this works when I apply it to a standard ContentDialog
. 控件模板的内容是从默认模板复制的,当我将其应用于标准
ContentDialog
时,我已经确认可以正常工作。
This compiles fine and I can run the app but when I instantiate the dialog and try to show it I get the following error: 这样编译就可以了,我可以运行应用程序,但是当我实例化对话框并尝试显示该对话框时,出现以下错误:
Failed to create a 'Windows.UI.Xaml.DependencyProperty' from the text 'Background'. [Line: 163 Position: 33]
If I try setting the TargetType
of the ControlTemplate
to ContentDialog
I get the same error but it cannot find my custom property PrimaryButtonGlyph
. 如果我尝试将
ControlTemplate
的TargetType
设置为ContentDialog
,则会遇到相同的错误,但是找不到我的自定义属性PrimaryButtonGlyph
。
It is like it cannot find properties on the base class but if this was a problem then very little would work. 就像它无法在基类上找到属性,但是如果这是一个问题,那么几乎没有用。 What am I doing wrong?
我究竟做错了什么?
(Target version: Windows 10, version 1803 (10.0; Build 17134), Min version: Windows 10 Fall Creators Update (10.0; Build 16299)) (目标版本:Windows 10版本1803(10.0;内部版本17134),最低版本:Windows 10 Fall Creators Update(10.0;内部版本16299))
EDIT: I've created a demo solution here: https://www.dropbox.com/s/a4y7jrtcw3ivkqy/StackOverflow53506051.zip?dl=0 编辑:我在这里创建了一个演示解决方案: https : //www.dropbox.com/s/a4y7jrtcw3ivkqy/StackOverflow53506051.zip?dl=0
From you code, you want to make custom Templated Control that inherit ContentDialog
. 从您的代码中,您想制作继承
ContentDialog
自定义模板控件 。 But you have not initialized the default style. 但是您尚未初始化默认样式。 You could custom
ContentDialog
with following steps. 您可以使用以下步骤自定义
ContentDialog
。
Create new items with Templated Control
template. 使用
Templated Control
模板创建新项目。
After the ContentDialogTest.cs
class created, the project will generate Generic.Xaml
file automatically. 创建
ContentDialogTest.cs
类后,项目将自动生成Generic.Xaml
文件。
Please copy your ContentDialogExTemplate
to Generic.Xaml
file. 请将您的
ContentDialogExTemplate
复制到Generic.Xaml
文件。 Please not I have modified custom content dialog name to ContentDialogTest
. 请不要将我的自定义内容对话框名称修改为
ContentDialogTest
。
<Style TargetType="controls:ContentDialogTest" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:ContentDialogTest">
<Border>
.........
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then create the DependencyProperty
in ContentDialogTest
class. 然后在
ContentDialogTest
类中创建DependencyProperty
。 you will find the there is this.DefaultStyleKey = typeof(ContentDialogTest);
您会发现这里是
this.DefaultStyleKey = typeof(ContentDialogTest);
in the default constructor method. 在默认的构造方法中。 And this line make sure the style could be initialized.
并且此行确保可以初始化样式。
public sealed class ContentDialogTest : ContentDialog
{
public ContentDialogTest()
{
this.DefaultStyleKey = typeof(ContentDialogTest);
}
public string PrimaryButtonGlyph
{
get => (string)GetValue(PrimaryButtonGlyphProperty);
set => SetValue(PrimaryButtonGlyphProperty, value);
}
public string SecondaryButtonGlyph
{
get => (string)GetValue(SecondaryButtonGlyphProperty);
set => SetValue(SecondaryButtonGlyphProperty, value);
}
public string CloseButtonGlyph
{
get => (string)GetValue(CloseButtonGlyphProperty);
set => SetValue(CloseButtonGlyphProperty, value);
}
public static readonly DependencyProperty PrimaryButtonGlyphProperty = DependencyProperty.Register
(nameof(PrimaryButtonGlyph),
typeof(string),
typeof(ContentDialogTest),
new PropertyMetadata(""));
public static readonly DependencyProperty SecondaryButtonGlyphProperty = DependencyProperty.Register
(nameof(SecondaryButtonGlyph),
typeof(string), typeof(ContentDialogTest),
new PropertyMetadata(""));
public static readonly DependencyProperty CloseButtonGlyphProperty = DependencyProperty.Register
(nameof(CloseButtonGlyph),
typeof(string),
typeof(ContentDialogTest),
new PropertyMetadata(""));
}
Usage 用法
private async void OnShowContentDialogExButtonClick(object sender, RoutedEventArgs e)
{
var dialog = new ContentDialogTest
{
Title = "Demo",
Content = new Dialog(),
PrimaryButtonText = "Primary",
SecondaryButtonText = "Secondary",
CloseButtonText = "Close"
};
await dialog.ShowAsync();
}
For better understanding, I have upload the code sample to git hub. 为了更好地理解,我将代码示例上传到git hub。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.