我有一个ResourceDictionary
文件,我也有两个类可供使用。
其中一个是IValueConverter
,另一个是与EventHandlers
相关的控制。
班级名称EventHandlers
设置为x:Class
属性值。
我还需要将Converters
设置为第二个x:Class
。
x:Class is set more than one time。
如何解决这个问题呢?
Converters.cs
class Converters : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double pr = (double)value;
AltoProgressBar bar = parameter as AltoProgressBar;
return pr * bar.Width / bar.Maximum;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
EventHandlers.cs
public partial class EventHandlers
{
private void progressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ProgressBar progressBar = sender as ProgressBar;
var template = progressBar.Template;
//Find the Rectangle in the ControlTemplate
var layer = (Rectangle)(template.FindName("rect", progressBar));
//Calculate the increment amount depending maxValue and Width
double artis = progressBar.Value * progressBar.Width / progressBar.Maximum;
DoubleAnimation anim = new DoubleAnimation(toValue: artis, duration: TimeSpan.FromMilliseconds(100));
layer.BeginAnimation(Rectangle.WidthProperty, anim);
}
}
styles.xaml
<ResourceDictionary xmlns:my="clr-namespace:AltoSS"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AltoSS.Converters"
<!--this doesn't make any sense-->
x:Class="AltoSS.EventHandlers">
<!--All styles in here-->
</ResourceDictionary>
答案 0 :(得分:1)
我认为不可能多次设置x:Class-Attribute(多态性目标)。
如果你只想使用转换器类(更具体的名称会更好)和你的EventHandeler,你需要在RD-Tag中定义两个类的命名空间(类似于xmlns:YourNamespace = clr-namespace:YourProject.NamespaceName )。
然后,您可以使用x:Key定义转换器作为静态资源。
像这样<ResourceDictionary xmlns:my="clr-namespace:AltoSS"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:AltoConverters="AltoSS.NamespaceConverters"
xmlns:AltoEventHandlers="AltoSS.NamespaceEventHandlers">
<!--NamespaceConverters and NamespaceEventHandlers from your cs files -->
<!-- for use as static Resource -->
<AltoConverters:Converters x:Key="YourConverters" />
<!-- example -->
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding ...Path..., Converter={StaticResource YourConverters}" />
</Style>
</ResourceDictionary>