【发布时间】:2016-10-10 15:26:29
【问题描述】:
我正在尝试为我的 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.XamlParseException Markup.StaticResourceHolder 抛出异常..
我的代码有什么问题?
【问题讨论】:
-
你能发布
XamlParseException.InnerException的堆栈跟踪吗? -
图像控件没有可以在样式中继承的基本样式。
标签: wpf xaml custom-controls