是否可以在 ResourceDictionary 文件中设置 "x:Class" 个以上?
Is it possible to set "x:Class" more than one into a ResourceDictionary file?
我有一个 ResourceDictionary
文件,我还有两个 class 用于它。
其中一个是 IValueConverter
,另一个是用于EventHandlers
与控件有关。
class 名称是 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>
我认为不可能多次设置 x:Class-Attribute(多态性目标)。
如果你只想使用你的 class 转换器(更具体的名称会更好)和你的 EventHandeler 你需要在 RD-Tag 中定义两个 classes 的命名空间(类似于xmlns:YourNamespace=clr-命名空间: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>
我有一个 ResourceDictionary
文件,我还有两个 class 用于它。
其中一个是 IValueConverter
,另一个是用于EventHandlers
与控件有关。
class 名称是 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>
我认为不可能多次设置 x:Class-Attribute(多态性目标)。
如果你只想使用你的 class 转换器(更具体的名称会更好)和你的 EventHandeler 你需要在 RD-Tag 中定义两个 classes 的命名空间(类似于xmlns:YourNamespace=clr-命名空间: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>