我已经创建了一个自定义控件-一个切换按钮,并将其添加到当前项目。在执行此操作时,还创建了Themes/Generic.xaml
文件,我更新了该文件以引用控件的资源字典。运行时一切正常,但当我将切换按钮添加到控件时,我得到错误Cannot locate resource 'resources/customcontrols/togglebuttonresource.xaml'.
我已经将切换按钮的资源字典添加到Control.Resources
中,但这并不能消 debugging 误。我尝试添加Generic.xaml
字典,但这也没有什么作用。错误仍然发生,切换按钮不会显示在设计器中。
我的控制
<Controls:MetroContentControl
x:Class="PATestGUI.AHPA.PAStatusAndControl.Views.PAStatusAndControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:cal="http://www.caliburnproject.org"
xmlns:cc="clr-namespace:PATestGUI.Resources.CustomControls"
xmlns:resources="clr-namespace:PATestGUI.Resources"
mc:Ignorable="d" HorizontalAlignment="Center" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" FontFamily="Calibri" BorderThickness="1">
<Control.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style .../> <!-- other styles here -->
<ResourceDictionary>
</Control.Resources>
<Grid>
<cc:ToggleButtonOnOff />
</Grid
字符串
错误
x1c 0d1x的数据
我如何摆脱这个设计器错误,使我的切换按钮显示在设计器上?
编辑:
这是我的项目结构。所有内容都在一个项目中。ToggleButtonOnOff在PAStatusAndControlView和Generic. xaml中都被引用。
的Generic.xaml
:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Resources/CustomControls/ToggleButtonResource.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
型
编辑2
ToggleButtonOnOff.cs
public class ToggleButtonOnOff : ToggleButton
{
static CornerRadius _defaultCornerRadius = new CornerRadius(12.0);
static Brush _defaultOnBrush = Brushes.Green;
static Brush _defaultOffBrush = Brushes.Red;
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register(nameof(CornerRadius), typeof(CornerRadius), typeof(ToggleButtonOnOff), new PropertyMetadata(_defaultCornerRadius));
public Brush ColorOn
{
get { return (Brush)GetValue(ColorOnProperty); }
set { SetValue(ColorOnProperty, value); }
}
public static readonly DependencyProperty ColorOnProperty =
DependencyProperty.Register(nameof(ColorOn), typeof(Brush), typeof(ToggleButtonOnOff), new PropertyMetadata(_defaultOnBrush));
public Brush ColorOff
{
get { return (Brush)GetValue(ColorOffProperty); }
set { SetValue(ColorOffProperty, value); }
}
public static readonly DependencyProperty ColorOffProperty =
DependencyProperty.Register(nameof(ColorOff), typeof(Brush), typeof(ToggleButtonOnOff), new PropertyMetadata(_defaultOffBrush));
public double SwitchWidth
{
get { return (double)GetValue(SwitchWidthProperty); }
set { SetValue(SwitchWidthProperty, value); }
}
public static readonly DependencyProperty SwitchWidthProperty =
DependencyProperty.Register(nameof(SwitchWidth), typeof(double), typeof(ToggleButtonOnOff), new PropertyMetadata(10.0));
static ToggleButtonOnOff()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleButtonOnOff), new FrameworkPropertyMetadata(typeof(ToggleButtonOnOff)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public ToggleButtonOnOff() { }
}
型
ToggleButtonResource.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PATestGUI.Resources.CustomControls">
<LinearGradientBrush x:Key="ToggleSwitch" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="LightGray" Offset="1" />
</LinearGradientBrush>
<Style x:Key="ToggleBtnBase" TargetType="{x:Type Border}">
<Setter Property="Height" Value="{Binding Height, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="Width" Value="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="CornerRadius" Value="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="BorderThickness" Value="{Binding BordderThickness, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter Property="Background" Value="{Binding ColorOn, RelativeSource={RelativeSource TemplatedParent}}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" Value="False">
<Setter Property="Background" Value="{Binding ColorOff, RelativeSource={RelativeSource TemplatedParent}}" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ToggleBtnSwitch" TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="Background" Value="{StaticResource ToggleSwitch}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter Property="HorizontalAlignment" Value="Right" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" Value="False">
<Setter Property="HorizontalAlignment" Value="Left" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type local:ToggleButtonOnOff}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ToggleButtonOnOff}">
<Border Style="{StaticResource ToggleBtnBase}">
<Grid>
<!--SWITCH-->
<Border Style="{StaticResource ToggleBtnSwitch}" Height="{TemplateBinding SwitchWidth}" Width="{TemplateBinding SwitchWidth}" BorderThickness="1" VerticalAlignment="Center" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
型
1条答案
按热度按时间gkl3eglg1#
只需将
ToggleButtonResource.xaml
的内容直接移动到Generic.xaml
,而不是将其添加为合并字典,然后设计器错误就会消失。也没有必要将包含这些资源的合并字典添加到
PAStatusAndControlView.Resources
。Generic.xaml:
字符串