2016-10-10 56 views
0

我试图为我的WPF应用程序创建自定义控件,并且无法理解它为什么不能正常工作。来自WPF应用程序中的自定义控件的异常

我有两个项目 - 一个是我的自定义控件库,其中一个自定义控件源自Image,只有很少的方法,第二个项目我想使用我的控件。我不想做任何样式或绑定,所以我使用默认值。

所以,我的自定义控件主题(generic.xaml):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:RTPlayer"> 
    <Style 
    TargetType="{x:Type local:RTPlayer}" BasedOn="{StaticResource {x:Type Image}}"> 
    </Style> 

和代码部分:

namespace RTPlayer 
{ 
    public class RTPlayer : Image 
    { 
    static RTPlayer() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(RTPlayer), new FrameworkPropertyMetadata(typeof(RTPlayer))); 
    } 

    public void Start() 
    { 
     // .... 
    } 
    } 
} 

在我的主要项目中,我已经添加引用到我的图书馆。 DLL文件,我试图使用控制:

<Window x:Class="rtspWPF.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:rtspWPF" 
    xmlns:CustomControls="clr-namespace:RTPlayer;assembly=RTPlayer" 
    xmlns:CustomControls="clr-namespace:RTPlayer;assembly=RTPlayer" 
    Title="MainWindow" Height="auto" Width="auto"> 
<CustomControls:RTPlayer x:Name="image" Margin="10,10,10,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="auto" Height="auto"/> 

问题是 - 1)xaml CustomControl:RTPlayer字符串警告我:“无法找到名为”System.Windows.Controls.Image“的资源。资源名称是区分大小写的“; 2)如果我尝试启动应用其抛出Markup.XamlParseExceptionMarkup.StaticResourceHolder抛出异常..

什么是错我的代码

+0

你可以发布'XamlParseException.InnerException'的堆栈跟踪吗? – haindl

+1

图像控件没有可以在样式中继承的基础样式。 – 2016-10-10 18:27:23

回答

0

由于在评论丹尼尔回答,我。已经发现了,我应该怎么做才能让我的应用程序工作

丹尼尔告诉我之后,图像组件行得有一个基本样式,我发现呈三角问题家伙:How to inherit type-based styles in WPF?

比我刚rewrited我一般.xaml文件到此:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:RTPlayer"> 

    <Style TargetType="Image" x:Key="ImageStyle"> 

    </Style> 

    <Style TargetType="{x:Type local:RTPlayer}" BasedOn="{StaticResource ImageStyle}"> 
    </Style> 
</ResourceDictionary> 

一切正常!