我正在研究 MS .NET WPF;在 XAML 中,我定义了一些静态资源。我想将它们分配到一个也在 XAML 中声明的数组中。
以下是静态资源:
<local:Person x:Key="PersonABC" Name="Hello" Age="29" />
<local:Person x:Key="PersonXYZ" Name="World" Age="55" />
Run Code Online (Sandbox Code Playgroud)
但是我无法通过类似的东西将它们分配到数组中{StaticResource PersonABC}。我必须重新创建资源并将它们分配到数组中:
<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
<local:Person Name="Hello" Age="29" />
<local:Person Name="World" Age="55" />
</x:Array>
Run Code Online (Sandbox Code Playgroud)
但我想要这样的东西:
<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
"{StaticResource PersonABC}"
"{StaticResource PersonXYZ}"
</x:Array>
Run Code Online (Sandbox Code Playgroud) 我在WPF中创建了一个ListBox样式,以便它呈现为一个复选框列表.
当我手动填充ListBox的项目时,样式工作完美.但是,当我将ListBox的ItemsSource绑定到静态资源(包含所需项目的ItemsControl)时,样式将被完全删除.
这是风格:
<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
<Style.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
<ContentPresenter
Grid.Column="1"
Margin="2,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这是ListBox的代码,它正确显示了样式:
<ListBox x:Name="ColumnsList"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1">
<ListBox.Items>
<ListBoxItem>Test</ListBoxItem>
<ListBoxItem>Test2</ListBoxItem>
<ListBoxItem>Test3</ListBoxItem>
</ListBox.Items>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
这是ListBox忽略样式的代码:
<ListBox x:Name="ColumnsList2"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1" …Run Code Online (Sandbox Code Playgroud) 基本上,我如何在静态类中创建自己的一组颜色,或者这样我可以这样做:
什么存在:
<Setter ... Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
Run Code Online (Sandbox Code Playgroud)
我想要的:
<Setter ... Value="{DynamicResource {x:Static MyColors.Color1}}"/>
Run Code Online (Sandbox Code Playgroud) 基本上,我想将所有XAML资源保存在外部程序集中(对于我的样式).我在我的应用程序中引用了这个外部程序集.
有没有关于附属程序集或诸如此类的内容或如何访问这些样式,以便我的应用程序仍然可以具有StaticResource标记而没有编译错误?
我想重用多种UserControl类型的一些样式.
我希望一些Border控件的背景能够闪现,我希望他们都能使用相同的样式,静态资源和动画,这样它们就会同步闪烁.
为此,我在资源字典中定义了一些常用颜色,如下所示:
<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />
Run Code Online (Sandbox Code Playgroud)
...我还在这本词典中定义了一个StoryBoard,如下所示:
<Storyboard x:Key="BackgroundAnimation">
<ColorAnimation
Storyboard.Target="{StaticResource StatusErrorBackground}"
Storyboard.TargetProperty="Color"
From="#440000"
To="#ff0000"
Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="True"/>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)
然后我将以下内容添加到顶级UserControl:
<FrameworkElement.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</FrameworkElement.Resources>
<FrameworkElement.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource BackgroundAnimation}"/>
</EventTrigger>
</FrameworkElement.Triggers>
Run Code Online (Sandbox Code Playgroud)
...然后在其他各种UserControls中,我重新导入ResourceDictionary上面的内容并使用{StaticResource StatusErrorBackground}for a Background.
有问题的元素是红色的(如SolidColorBrush声明中所示),但它们并没有闪烁.
也许这样做不会对有问题的元素引发相应的PropertyChanged通知,所以它们不会被重绘?或类似的东西.该Color物业SolidColorBrush是不是一个依赖项属性,但SolidColorBrush器物IAnimatable,所以很明显有神奇的事情发生在幕后这里后面我不明白.
或者是因为我在两个不同的地方导入相同的资源字典(一次在我的顶级UserControl加一次在我的孩子中)我最终得到两个独立的StaticResource引用?如果您ResourceDictionary在两个不同的控件中导入相同的文件,它是否为每个控件创建独立的资源?在这种情况下,我可以通过在应用程序级别将其拉入来解决这个问题,我猜...
谁能告诉我我做错了什么以及如何解决它?
我在 Button 控件的 VisualStateGroup 中有一个 VisualTransition。Storyboard 属性绑定到 StaticResource Storyboard:
<VisualTransition From="MyLock" To="Unlocked" GeneratedDuration="0:0:2.0" Storyboard="{StaticResource Storyboard_Locked_ToUnlocked}"/>
Run Code Online (Sandbox Code Playgroud)
当我使用 VisualStateManager 进入“解锁”状态时,出现以下异常:“System.InvalidOperationException:类型为‘System.Windows.Media.Animation.Storyboard’的指定值必须将 IsFrozen 设置为 false 才能修改。” 故事板中的任何内容都没有修改故事板本身,所以我不确定这意味着什么。
两件奇怪的事。第一,这种情况直到从 VS 2010 迁移到 VS 2012 并安装 .Net 4.5 框架后才开始发生。其次,如果我复制所有代码并将其移动到标签内的 Storyboard 属性本身,则不会出现错误。所以它似乎不是故事板本身的任何东西,只是为此使用 StaticResource 的问题。
在研究时,我唯一能找到的关于订阅故事板的 Completed 事件的错误处理,我不会在任何地方做,除非 VisualStateManager 以某种方式这样做。
在此先感谢您的帮助。
编辑:我还应该补充一点,我想在两个不同的转换中使用它,这就是为什么我更喜欢它是一个静态资源,这样我就不必复制/粘贴 xaml。我注释掉了其中一个转换,但仍然出现错误,所以这也不是我正在共享它的事实。
我刚刚学习 XAML 和 Xamrin。我正在尝试了解静态样式的工作原理。这是我的 XAML 代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Styles"
x:Class="Styles.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:key="buttonStyle" TargetType="Button">
<Setter Property="HorizontalOptions" Value="Center"/>
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="TextColor" Value="Red"/>
<Setter Property="FontSize" Value="Small"/>
</Style>
<Style TargetType="Label">
<Setter Property="HorizontalOptions" Value="Center"/>
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="TextColor" Value="Blue"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style x:key="baseStyle" TargetType="View">
<Setter Property="HorizontalOptions" Value="Center"/>
<Setter Property="VerticalOptions" Value="Center" />
</Style>
<Style x:key="entryStyle" TargetType="Entry" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="Green"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Padding="20">
<Label Text="This is label 1 …Run Code Online (Sandbox Code Playgroud) 我是否在从.net 3.5到.net 4的步骤中遗漏了一些东西,因为我看到看似错误的行为似乎与系统的目标背道而驰.
我正在尝试使用一些示例来创建一个简单的MVVM库.我正在Twitter客户端应用程序中使用它进行一些额外的学习,并且遇到了一个很大的障碍.
情景是这样的.我的根ViewModel(TwitterClientViewModel)对象被赋予一个DialogViewModel对象的实例以供显示.DialogViewModel添加到集合中,bool HasDialogs设置为true.如有必要,将为集合和标志调用PropertyChanged事件.这部分工作非常棒.
TwitterClientViewModel的视图称为TwitterClientTemplate,并使Visible成为DialogViewTemplate(DialogViewModel的视图)托管的叠加层.托管ContentControl的模板引用了带有DynamicResource扩展的DialogViewTemplate.这在设计人员和运行时都很有用.
这是事情变得奇怪的地方.DialogViewTemplate的"主体"使用绑定到DialogViewModel.Content(类型对象)的其他内容控件来托管对话框内容.希望是使用TemplateSelector(我编写了一个很好的声明式的,但已经注释用于测试目的)我可以显示文本和交互元素.例如,在验证Twitter帐户时向用户请求详细信息.在这种情况下,PIN码.
此时,我有两个嵌套的内容控件用于对话框实现.出于测试目的,DialogViewTemplate正文中的contentcontrol使用staticresource扩展来检索EnterPINDialogTemplate(EnterPINDialogViewModel的视图).EnterPINDialogTemplate和DialogViewTemplate都在同一个文件中(前者当然是先定义的)虽然最初它们是分开的.
在运行时,staticresource扩展会抛出带有消息的XamlParseException; '为'System.Windows.Markup.StaticResourceHolder'提供价值,引发了一个异常.
和内部异常消息;
'找不到名为'EnterPINDialogTemplate'的资源.资源名称区分大小写'
使用dynamicresource返回null并在contentcontrol中显示EnterPINDialogViewModel类型的全名 - 正如资源未解析时所预期的那样.在调用FrameWorkElement.FindResource()时突破我的自定义TemplateSelector会抛出类似的异常(TryFindResource返回null).
我的第一个想法是在构建datatemplate时逻辑树被拆分,我记得早期项目中该区域的问题.我尝试使用ResourceDictionary的MergeDictionaries属性使资源字典可以在DataTemplate中使用,但设计者不喜欢这一点,这里描述了错误:http: //connect.microsoft.com/VisualStudio/feedback/details/498844/WPF的设计师抛出,InvalidCastException的
抓住那个想法.我尝试在Application,Window和TwitterClientTemplate级别合并字典,但没有运气.
以下是xaml文件.
DialogTemplates.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:EpicTweet.ViewModel"
xmlns:ET="clr-namespace:EpicTweet"
xmlns:T="clr-namespace:EpicTweet.Tools"
xmlns:MV="clr-namespace:MVVM;assembly=MVVM"
xmlns:Loc="clr-namespace:EpicTweet.Localization"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<DataTemplate DataType="VM:EnterPINDialogViewModel" x:Key="EnterPINDialogTemplate">
<Grid d:DesignWidth="453.89" d:DesignHeight="78.92" Loc:ResXManagerProperty.ResourceManager="{x:Static ET:Language.ResourceManager}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="{Loc:ResxExtension ResourceName=String_PIN, FallbackValue='<PIN>'}"/>
<TextBox Grid.Column="1"/>
<TextBlock Grid.Row="1" Grid.RowSpan="2"></TextBlock>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DialogViewTemplate" DataType="MV:DialogViewModel">
<Border BorderBrush="Black" BorderThickness="1">
<Grid d:DesignWidth="277.419" d:DesignHeight="74.96" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> …Run Code Online (Sandbox Code Playgroud) Vue.js 2.6.2使用 using构建我的网站后vue-cli,我遇到了静态资源(在这种情况下为图像)的问题。Webpack 将它们捆绑在/img/文件夹中,这很好,但是图像被赋予了哈希值image_demo.25d62d92.png,这在尝试从外部源(例如从另一个网站)访问这些资源时会导致问题。
webpack 有一个选项可以filenameHashing完全禁用,但是如果不能对网站上的所有图像进行缓存控制,那么牺牲太大了。
理想的解决方案是选择只让一些手工挑选的资源使用它们的默认名称而没有额外的哈希值,而其他资源则获得hash缓存控制的默认值。
staticresource ×10
wpf ×8
xaml ×4
animation ×1
assets ×1
brush ×1
c# ×1
colors ×1
datatemplate ×1
listboxitem ×1
resources ×1
storyboard ×1
styling ×1
vue.js ×1
webpack ×1