2016-09-14 30 views
0

我想爲我的Windows Phone 8.1項目中創建一個自定義模板控制,但無論我做什麼它給了我以下異常:無法創建自定義Windows Phone的控制

無法創建從一個「的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> 

提前致謝。

回答

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> 

+0

Altough此代碼的工作,我不知道爲什麼上面的一個沒有。我的意思是,當我點擊創建模板控制時,它是自我生成的。這是一個錯誤還是什麼? –

+0

我不認爲它自動生成這樣的代碼,如果它確實那麼應該是一個錯誤。 –

+0

這是一個全新的解決方案。我剛剛創建了模板控件並運行應用程序。結果就在這裏。所以它可能像你所說的一個錯誤。謝謝你的幫助。 –

相關問題