简体   繁体   English

UWP:自定义控件的模板失败,并显示“无法从文本创建'Windows.UI.Xaml.DependencyProperty'”错误

[英]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 . 如果我尝试将ControlTemplateTargetType设置为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("&#xF13E;"));

    public static readonly DependencyProperty SecondaryButtonGlyphProperty = DependencyProperty.Register
        (nameof(SecondaryButtonGlyph),
        typeof(string), typeof(ContentDialogTest),
        new PropertyMetadata("&#xF13D;"));

    public static readonly DependencyProperty CloseButtonGlyphProperty = DependencyProperty.Register
             (nameof(CloseButtonGlyph),
              typeof(string),
              typeof(ContentDialogTest),
              new PropertyMetadata("&#xF13D;"));
}

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.

相关问题 获取Windows.Ui.Xaml.DependencyProperty的名称 - Get Name of Windows.Ui.Xaml.DependencyProperty Windows应用商店-XAML C#-自定义控件未设置DependencyProperty - Windows Store App - XAML C# - Custom Control not setting DependencyProperty 无法从文本&#39;鼠标&#39;创建&#39;Windows.UI.Xaml.Controls.Primitives.PlacementMode&#39; - Failed to create a 'Windows.UI.Xaml.Controls.Primitives.PlacementMode' from the text '鼠标' UWP Xaml控件,用于两列文本+具有自定义编辑器的编辑器 - UWP Xaml control for two column text + editor with custom editor UWP无法在自定义控件中分配属性错误 - UWP Failed to assign to property error in custom control UWP Setter自定义DependencyProperty - UWP Setter custom DependencyProperty UWP-从不同样式的资源字典中引用StaticResource:无法分配给属性“ Windows.UI.Xaml.ResourceDictionary.Source” - UWP - Reference StaticResource from different style resource dictionary: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' 创建从用户代码/ xaml(UWP)接收内容的用户控件 - Create user control that receives content from user code/xaml (UWP) 适用于UWP的Telerik:Windows.UI.Xaml.UnhandledException - Telerik for UWP: Windows.UI.Xaml.UnhandledException 在Windows 10的UWP(通用Windows应用)中创建自定义形状控件 - Create custom Shape Control in UWP (Universal Windows Apps), Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM