【问题标题】:Cannot create custom windows phone control无法创建自定义 Windows Phone 控件
【发布时间】:2017-01-22 18:26:39
【问题描述】:

我正在尝试为我的 Windows Phone 8.1 项目创建一个自定义模板控件,但无论我做什么都会给我以下异常:

无法从文本创建“System.Type” 'local2:CustomControl1'。

我非常感谢任何帮助。这是我当前的代码

PivotPage.xaml:

    <Page
        x:Class="App1.PivotPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="using:App1.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:data="using:App1.Data"
        xmlns:local="using:App1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
        DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
        mc:Ignorable="d">
    <Grid>
        <controls:CustomControl1 />
    </Grid>
</Page>

CustomControl1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace App1.Controls
{
    public sealed class CustomControl1 : Control
    {
        public CustomControl1()
        {
            this.DefaultStyleKey = typeof(CustomControl1);
        }
    }
}

Generic.xaml

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

    <Style TargetType="local2:CustomControl1" xmlns:local2="using:App1.Controls">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local2:CustomControl1">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

提前致谢。

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone-8.1 winrt-xaml


    【解决方案1】:

    试试这个:

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1" xmlns:local2="using:App1.Controls">
    
    <Style TargetType="local2:CustomControl1" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local2:CustomControl1">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 虽然这段代码有效,但我不知道为什么上面的代码无效。我的意思是当我单击创建模板控件时它是一个自生成的。这是一个错误还是什么?
    • 我不认为它会自动生成这样的代码,如果是这样,那应该是一个错误。
    • 这是一个全新的解决方案。我刚刚创建了模板控件并运行了应用程序。结果就在这里。所以它可能是你所说的错误。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多