为了实现我的应用程序,我使用了很多Blend3.当Blend3想要将资源链接到另一个资源时,它会多次使用链接类型"DynamicResource".正如我所理解的那样(但我可能已经理解得不好),只有当我想在运行时修改链接时,"动态"链接才有意义.在其他情况下,他们使用更多的内存是徒劳的.我不想在运行时修改任何东西,那么问题是:有意义在我的所有应用程序中用"StaticResource"替换"DynamicResource"吗?谢谢!Pileggi
wpf performance dynamicresource expression-blend staticresource
在我的应用程序中,我有一个颜色资源.我有一个元素使用该颜色作为xaml中的动态资源.
<Window x:Class="ResourcePlay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="425">
<Window.Resources>
<Color x:Key="MyColor">Red</Color>
</Window.Resources>
<Grid>
<Rectangle VerticalAlignment="Top" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="TopBrush" Color="{DynamicResource MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle VerticalAlignment="Bottom" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="BottomBrush"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
在代码中,我想复制此资源引用.
using System.Windows;
using System.Windows.Media;
namespace ResourcePlay {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
// I want to copy the resource reference, not the color.
BottomBrush.Color = TopBrush.Color;
// I'd really rather do something like this.
var reference …Run Code Online (Sandbox Code Playgroud) 我有一个简单的“播放/暂停”按钮,在应用程序开始时显示“播放”图标。这是它的代码:
<Button x:Name="playPauseButton" Style="{DynamicResource MetroCircleButtonStyle}"
Content="{DynamicResource appbar_control_play}"
HorizontalAlignment="Left" Margin="77,70,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="Button_Click"/>`
Run Code Online (Sandbox Code Playgroud)
我想要做的是在按下播放图标时将其更改为暂停图标。我所要做的就是将内容更改为{DynamicResource appbar_control_pause}. 但是,当我执行以下操作时:
playPauseButton.Content = "{DynamicResource appbar_control_stop}";
Run Code Online (Sandbox Code Playgroud)
它只显示按钮内的字面字符串。我怎么能改变那个属性?
我有一个用户控件,资源如下:
<UserControl.Resources>
<Image x:Key="IconPrinter" Source="/PrintViewerWPF;component/Resources/Images/printer.png" />
<MenuItem x:Key="PrintDoc"
Header="Print"
Click="PrintDoc_OnClick"
Icon="{StaticResource IconPrinter}" />
<ContextMenu x:Key="MergedContextMenu">
<StaticResource ResourceKey="PrintDoc"/>
</ContextMenu>
<ContextMenu x:Key="SingleContextMenu">
<StaticResource ResourceKey="PrintDoc"/>
</ContextMenu>
<DataTemplate x:Key="mergedDocDisplayGrid">
<StackPanel StackPanel.ContextMenu="{DynamicResource MergedContextMenu}" />
</DataTemplate>
<DataTemplate x:Key="singleDocDisplayGrid">
<StackPanel StackPanel.ContextMenu="{DynamicResource SingleContextMenu}" />
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
所述DataTemplates的一个的DevExpress内使用FlowLayoutControl
在我的场景中,我有两个项目FlowLayoutControl.一个使用模板mergedDocDisplayGrid,另一个使用singleDocDisplayGrid
数据在模板中正确显示,并且已成功实现其他功能,如鼠标活动和拖放
在任一项上显示上下文菜单都有效,但如果我随后尝试再次在另一项上显示上下文菜单,则会出现以下异常:
Message - Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.
Inner - Element already has a logical parent. It must be detached from the old parent before it …Run Code Online (Sandbox Code Playgroud) 我在模板中使用 DynamicResource,并在使用该模板的每个样式中使用 StaticResourceExtensions 作为资源,以便在每个样式中对 DynamicResource 进行不同的评估。
问题是,我收到此错误:
Unable to cast object of type 'System.Windows.Media.Effects.DropShadowEffect' to type 'System.Windows.ResourceDictionary'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
<DropShadowEffect
x:Key="Sombra"
Opacity="0.5"
ShadowDepth="3"
BlurRadius="5"
/>
<ControlTemplate
x:Key="ControleGeometriaTemplate"
TargetType="{x:Type Control}"
>
<Border
x:Name="border"
Background="{TemplateBinding Background}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
/>
<Path
x:Name="ícone"
Fill="{TemplateBinding Foreground}"
Effect="{DynamicResource PathShadow}"
/>
</Border>
</ControlTemplate>
<Style x:Key="BotãoGeometria" TargetType="{x:Type ButtonBase}">
<Setter Property="Template" Value="{StaticResource ControleGeometriaTemplate}"/>
</Style>
<Style
x:Key="BotãoNavegaçãoBase"
TargetType="{x:Type ButtonBase}"
BasedOn="{StaticResource BotãoGeometria}"
>
<Style.Resources>
<StaticResource x:Key="PathShadow" ResourceKey="Sombra"/>
</Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)