繁体   English   中英

无法创建自定义Windows Phone控件

[英]Cannot create custom windows phone control

我正在尝试为Windows Phone 8.1项目创建一个自定义模板控件,但是无论如何我都会遇到以下异常:

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

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

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);
        }
    }
}

泛型

<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>

提前致谢。

尝试这个:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM