提供哈希表/字典实现,其包含WPF应用程序的组件和其他元素使用的WPF资源(例如,矢量绘图,画笔,颜色,控件模板,数据模板等)。
当我选择一个元素时,我需要更改它的样式。 资源字典: <Setter Property="StrokeShape" ...</desc> <question vote="0"> <p>当我选择一个元素时,我需要更改它的样式。 资源词典:</p> <pre><code><Style TargetType="Border" x:Key="СBorder"> <Setter Property="StrokeShape" Value="RoundRectangle 8" /> <Setter Property="Stroke" Value="#D9D9D9"> </Setter> <Setter Property="StrokeThickness" Value="1"></Setter> </Style> <Style TargetType="Border" x:Key="SelectBorder"> <Setter Property="StrokeShape" Value="RoundRectangle 8" /> <Setter Property="Stroke" Value="#9B6CFF"> </Setter> <Setter Property="StrokeThickness" Value="2"></Setter> </Style> </code></pre> <p>Xaml代码:</p> <pre><code><VerticalStackLayout Spacing="15" Margin="0,20,0,0"> <Border Style="{StaticResource СBorder}" x:Name="LoginBorder"> <Entry Style="{StaticResource CEntry}" Placeholder="E-mail" x:Name="LoginEntry" Completed="LoginEntry_Completed" TextChanged="LoginEntry_TextChanged" ></Entry> </Border> <Border Style="{StaticResource SelectBorder}" x:Name="PasswordBorder"> <Entry Style="{StaticResource CEntry}" Placeholder="Пароль" Completed="PasswordEntry_Completed" TextChanged="LoginEntry_TextChanged" x:Name="PasswordEntry" IsPassword="True"></Entry> </Border> <Button WidthRequest="307" HeightRequest="46" Text="Вход" Style="{StaticResource CButton}"> </Button> </VerticalStackLayout> </code></pre> <p>我尝试使用<pre><code>LoginBorder.Style = (Style)Application.Current.Resources["SelectBorder"];</code></pre>,但导致错误<pre><code>System.Collections.Generic.KeyNotFoundException: 'The resource 'SelectBorder' is not present in the dictionary.'</code></pre>,在网上搜索后,我找不到解决方案。</p> </question> <answer tick="false" vote="0"> <p>您可以使用<a href="https://learn.microsoft.com/en-us/dotnet/maui/user-interface/visual-states?view=net-maui-8.0" rel="nofollow noreferrer">视觉状态</a>:</p> <pre><code><VerticalStackLayout Margin="0,20,0,0" Spacing="15"> <Border x:Name="LoginBorder"> <VisualStateManager.VisualStateGroups> <VisualStateGroupList> <VisualStateGroup Name="CommonStates"> <VisualState Name="Normal"> <VisualState.Setters> <Setter Property="StrokeShape" Value="RoundRectangle 8" /> <Setter Property="Stroke" Value="#D9D9D9" /> <Setter Property="StrokeThickness" Value="1" /> </VisualState.Setters> </VisualState> <VisualState Name="Focused"> <VisualState.Setters> <Setter Property="StrokeShape" Value="RoundRectangle 8" /> <Setter Property="Stroke" Value="#9B6CFF" /> <Setter Property="StrokeThickness" Value="2" /> </VisualState.Setters> </VisualState> <VisualState Name="Disabled"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="DarkGray" /> </VisualState.Setters> </VisualState> <VisualState Name="PointerOver"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="LightBlue" /> </VisualState.Setters> </VisualState> </VisualStateGroup> <VisualStateGroupList> </VisualStateManager.VisualStateGroups> <Entry x:Name="LoginEntry" Placeholder="E-mail" /> </Border> <Border x:Name="PasswordBorder"> <Entry x:Name="PasswordEntry" IsPassword="True" Placeholder="Пароль" /> </Border> <Button HeightRequest="46" Text="Вход" WidthRequest="307" /> </VerticalStackLayout> </code></pre> </answer> </body></html>
我试图在 Common.xaml 中定义一个公共宽度资源,它将由不同的控件共享,如下所示: 20 然后我用它...
Javascript & React - 如何在不使用字典/对象的情况下避免“如果地狱”?
我知道这个问题以前曾被问过,但通常的答案给我带来了另一个(潜在的)问题和疑问。 上下文 我有一个函数,它返回必须撕裂的组件...
Maui App.xaml 基于平板电脑或手机的“OnIdiom”在两个 ResourceDictionary 文件之间加载
我有两个资源字典,一个用于移动设备,一个用于平板电脑,我想使用 OnIdiom 将正确的资源字典合并到 App.xaml 中。 使用 OnPlatform 很简单,但我不明白...
我需要帮助来创建我正在运行的代码的替代方案。非常感谢您的回答、支持和同行评审。 这个答案对我帮助很大。我的按钮形状确实符合我的预期。我的目标...
我的基础程序集包含一些 xaml 资源,这些资源由依赖于此基础 dll 的程序集上的某些自定义 WPF UserControl 使用,为此,我正在使用 我的基础程序集包含一些 xaml 资源,这些资源由依赖于此基础 dll 的程序集上的某些自定义 WPF UserControl 使用,为此,我正在使用 <ResourceDictionary Source="pack://application,,,/assemblyname;component/Resources.xaml"/> 我需要以不同的名称编译这个基础 dll。 由于程序集名称正在更改,因此上面的 URI 在其他 dll 上会被破坏。我想使用条件编译来更改程序集名称,因此我尝试将资源字典的定义移动到后面的代码中,执行类似的操作 ResourceDictionary res = new(); res.Source=new Uri("/assemblyname;component/Resources.xaml", UriKind.Relative); this.Resources.MergedDictionaries.Add(res); 这个想法是将这段代码封装在 #if ... #endif 中,其中程序集的名称将根据编译配置而更改。 我没有成功。有人可以提供实现此动态资源的选项吗?谢谢 一个有效的解决方案是最终在 App.xaml.cs 中加载资源,因此将我的问题后面的代码从控件的构造函数移动到 App.xaml.cs ,它在运行时解决了问题,但随后控件在设计时不太好。我使用以下方法找到了问题的解决方案:将 uri 移动到静态成员,这样问题就可以在运行时和设计时解决了
我有两个资源文件(资源字典) Brushes.xaml:包含一些画笔 Templates.xaml :包含一些静态引用(StaticResource)Brushes.xaml 中的键的模板 ...
是否可以在另一个用户控件中使用我在中定义的资源,而无需使用资源字典的额外文件。 我有一个用户控件“ViewCostumers&
动态解析 MergedDictionaries 中的静态资源
对所有代码感到抱歉,但我觉得我需要它来解释问题。另外,我知道一些问题可以通过使用动态资源来解决,但我也需要它来处理静态资源......
动态解析 MergedDictionaries 中的静态资源
对所有代码感到抱歉,但我觉得我需要它来解释问题。另外,我知道一些问题可以通过使用动态资源来解决,但我也需要它来处理静态资源......
我正在尝试为 MAUI 的初始设置创建 C# 实用程序,以确保设置过程不会分散在 5 个文件中。为此,我需要将以下内容转换为 C# 代码: 我正在尝试为 MAUI 的初始设置创建 C# 实用程序,以确保设置过程不会分散在 5 个文件中。为此,我需要将以下内容转换为 C# 代码: <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 到目前为止我已经尝试了两种方法: var converter = new ResourceDictionary.RDSourceTypeConverter(); try { var c = converter.ConvertFromString(resourcepath); } catch (Exception ex) { // thrown not implemented exception } resourceDictionary.MergedDictionaries.Add(new() { Source = new Uri(resourcepath, UriKind.Relative) // throws exception saying I need to use XAML }); 有没有其他方法可以创建不影响性能的资源字典? 您绝对可以实例化一个 ResourceDictionary 并将其动态添加到应用程序资源的 MergedDictionaries。 但是,这通常可以在某些地方起作用(从我自己的存储库之一中的 App.xaml.cs 文件中获取和修改): if (Current?.Resources.MergedDictionaries is not { } mergedDictionaries) { return; } mergedDictionaries.Clear(); mergedDictionaries.Add(new global::Resources.Styles.Colors()); mergedDictionaries.Add(new global::Resources.Styles.Styles()); 请注意,这些资源字典是编译的。您不能仅将它们作为路径来引用。不过,我不知道这是否真的比默认方式更快。 更新 为此,您需要更新 Colors.xaml 和 Styles.xaml 仅 XAML 资源字典,并使用 x:Class 属性为它们指定类名称: <?xml version="1.0" encoding="UTF-8" ?> <?xaml-comp compile="true" ?> <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Resources.Styles.Colors"> </ResourceDictionary> 然后您可以使用 new global::Resources.Styles.Colors() 和 new global::Resources.Styles.Styles() 实例化它们。 更新2 如果您提供 x:Name 的 ResourceDictionary 一个完全限定的名称(例如 MyProject.Resources.Styles.Colors),然后在不使用 global:: 的情况下实例化它,则可能也有效,如下所示: mergedDictionaries.Add(new Resources.Styles.Colors()); 进一步阅读 官方文档还展示了如何动态添加资源字典(用于为应用程序主题化,但原理是相同的):https://learn.microsoft.com/en-us/dotnet/maui/user-interface/theming ?view=net-maui-7.0#在运行时加载主题 对于那些希望在 MAUI 中使用资源字典而不使用 xaml 页面(仅 C#)的人,这就是我让它工作的方式。 注意:我的项目中唯一的 xaml 页面是默认的 Resources\Styles\Styles.xaml 和 Resources\Styles\Colors.xaml 为 xaml 资源页面创建代码隐藏文件。 namespace MyApp.Resources.Styles; public partial class Styles : ResourceDictionary { public Styles() { InitializeComponent(); } } 然后修改xaml从 <?xml version="1.0" encoding="UTF-8" ?> <?xaml-comp compile="true" ?> <-- REMOVE THIS SO THE CODE-BEHIND FILE WILL COMPLIE <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> 到 <?xml version="1.0" encoding="UTF-8" ?> <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.Resources.Styles.Styles"> <-- ADD FULLY QUALIFIED CLASS NAME 然后添加 App.cs 构造函数(按照 Julian 建议) if (Application.Current?.Resources.MergedDictionaries is not { } mergedDictionaries) { return; } mergedDictionaries.Clear(); mergedDictionaries.Add(new Resources.Styles.Colors()); mergedDictionaries.Add(new Resources.Styles.Styles()); 在 C# 页面构造函数中使用(例如:MainPage.cs) Application.Current.Resources.TryGetValue("BtnStyle", out object btnStyle); 然后到控件 new Button { Text = "Click Me", Style = (Style)btnStyle as Style, } 请记住向 xaml 资源定义中定义的任何样式添加 x:Key。 文件样式.xaml <Style x:Key="BtnStyle" TargetType="Button"> <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Primary}}" /> ************* OMITTED SETTERS *************** </Setter> </Style>
使用 C# 和 WinUI3,如何在 XAML 中创建可重用的弹出窗口并在多个代码隐藏中创建它的多个实例?
我有一个浮出控件,我想在多个类中重用。 我有一个浮出控件,我想在多个类中重复使用。 <CommandBarFlyout x:Name="BasicFlyout" AlwaysExpanded="True" x:Key="ImageContextMenu"> <CommandBarFlyout.SecondaryCommands> <AppBarButton x:Name="1" Label="A"/> <AppBarButton x:Name="2" Label="B"/> <AppBarButton x:Name="3" Label="C"/> <AppBarButton x:Name="4" Label="D"/> </CommandBarFlyout.SecondaryCommands> </CommandBarFlyout> 我需要为最多 120 个图像附加唯一的按钮组合,并且每个图像都有一个关联的状态类,其中包含每个按钮是否可见的布尔标志以及每个按钮的回调(将在代码隐藏。 理想情况下,它看起来像这样,尽管语法是关闭的。 CommandBarFlyout flyout = new Resources["ImageContextMenu"] as CommandBarFlyout; // Set visibility and callbacks FlyoutBase.SetAttachedFlyout(image, flyout); 问题是我不知道如何在 XAML 中声明它。看起来代码必须添加到整个项目的资源中,但我不知道该怎么做。资源字典不允许我使用 CommandBarFlyout 作为根元素。 正如你所说,资源字典似乎不允许我们使用CommandBarFlyout作为根元素。 我建议你可以尝试在StackPanel.Resources中添加CommandBarFlyout。然后你可以尝试使用 {StaticResource} 标记扩展 喜欢: <Button x:Name="myButton" Click="myButton_Click" FlyoutBase.AttachedFlyout="{StaticResource ImageContextMenu}">Click Me</Button> 或者使用 {x:Bind} 标记扩展 喜欢: <Button x:Name="myButton" Click="myButton_Click" FlyoutBase.AttachedFlyout="{x:Bind BasicFlyout}">Click Me</Button>
在 UserControl 库中使用全局 ResourceDictionary,而不在 UserControl 中引用它
我创建了一个包含多个用户控件的 WPF 类库项目。我想定义库中所有用户控件都可以共享和引用的通用样式、模板和资源...
我有一个具有以下结构的项目: 项目 ├──观点 ├ ├──仪表板.xaml ├ ├──仪表板.cs ├ ├──风格 ├──DashboardStyle.xaml 在我的 DashboardStyle.xaml 中,我有以下代码: <
有没有办法预览合并到App.xaml资源中的DynamicResources?
我在 WPF 中有一个应用程序,并创建了一个服务,将我的画笔和颜色添加到 ResourceDictionary 中,然后将此字典与 App.xaml 资源合并。这看起来像这样(显然......
我在应用程序的字典中有 4 个资源文件。每个都在同一个文件夹中,并将构建操作设置为 MauiXaml。 我在应用程序的字典中有 4 个资源文件。每个都在同一个文件夹中,并将构建操作设置为 MauiXaml。 <Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:CMO3" x:Class="CMO3.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/CMOColors.xaml"/> <ResourceDictionary Source="Resources/Styles/Styles.xaml" /> <ResourceDictionary Source="Resources/Styles/CMOStyles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 在 Windows 上运行 .NET MAUI,我可以看到所有这些。 当我将 Visual Studio 指向我的三星 S20 FE 5G 时,同一行代码只能看到其中一个。 显示的是第一个:默认 MAUI 项目附带的 Colors.xaml。我可以自己合并所有这些,但我很困惑为什么 MergedDictionaries 不能在 Android 上运行。 约翰,我想你是说我的三星 S20 FE 5G。
我似乎无法将合并字典添加到 XAML 中的合并字典集合中。 主题.xaml 我似乎无法将合并词典添加到 XAML 中的合并词典集合中。 Theme.xaml <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/> <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/> </ResourceDictionary.MergedDictionaries> 应用资源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/> <!-- <ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/> <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/> --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 注意:如果我将两个 ResourceDictionaries 都放在 Appication.Resources MergedDictionary 中(注释掉 theme.xaml 并取消注释其他两个词典),它们都会正确加载。但是,我们的资源定义方式,这可能意味着将加载相当多的资源,对于动态加载,我希望能够定义模板。 这是一个优化错误,请参阅此链接 关于每一个对象的创建 XAML,如果存在默认样式 (即带有 Type 键的样式) 应应用样式。尽你所能 想象有几种表现 优化(暗示) 查找尽可能轻的重量。一 其中之一就是我们不往里看 资源字典,除非它们是 标记为“包含默认值 样式”。有一个错误:如果你所有的 默认样式嵌套在 merged 中 字典深三层(或 更深)顶级词典没有 被标记以便搜索跳过它。 解决方法是设置一个默认值 对某事、任何事物的风格,在根 词典. 所以向根字典添加虚拟样式可以解决这个问题。例子 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/> </ResourceDictionary.MergedDictionaries> <!-- Dummy Style, anything you won't use goes --> <Style TargetType="{x:Type Rectangle}" /> </ResourceDictionary> </Application.Resources> 您的示例代码在 Palette.Blue.xaml 的 App.xaml 合并资源字典源中有一个双等号。我假设这是您在此处发布的示例的拼写错误,而不是您真正的问题。 弄清楚如何在 XAML 中直接链接所有资源可能很棘手。最简单的方法是使用 Blend 中的资源面板。我用你的例子命名的资源文件创建了一个 Silverlight 应用程序,然后在 Blend 中打开项目并很快将它们链接在一起。 App.xaml <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Theme.xaml" /> <!-- <ResourceDictionary Source="Palette.Blue.xaml"/> <ResourceDictionary Source="Template.xaml"/> --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> Theme.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Palette.Blue.xaml"/> <ResourceDictionary Source="Template.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> Template.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="TextBox"> <Setter Property="Margin" Value="10" /> <Setter Property="Width" Value="250" /> </Style> <Style x:Key="ReadOnlyTextBoxStyle" TargetType="TextBox"> <Setter Property="IsReadOnly" Value="True" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Margin" Value="10" /> <Setter Property="Width" Value="250" /> </Style> </ResourceDictionary> Palette.Blue.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="BlueSolidColorBrush" Color="SkyBlue" /> </ResourceDictionary> MainPage.xaml <UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel x:Name="LayoutRoot" Background="Honeydew"> <TextBox Text="Read Only Textbox" Style="{StaticResource ReadOnlyTextBoxStyle}" /> <TextBox Text="Blue Textbox" Background="{StaticResource BlueSolidColorBrush}" /> <TextBox Text="Read Only, Blue Textbox" Style="{StaticResource ReadOnlyTextBoxStyle}" Background="{StaticResource BlueSolidColorBrush}" /> </StackPanel> </UserControl> 当然,如果您链接来自不同程序集的资源,它看起来会有所不同。实际上,在那种情况下,我建议考虑在后面的代码中合并你的字典。 如果这发生在您自己的一个控件上,我发现另一种解决方案是将 DefaultStyleKey 属性设置为 null: DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(null)); 我不知道为什么这行得通,但似乎行得通!
Generic.xaml 中的 WPF StaticResourceExtension 找不到名为“x”的资源
Handycontrols WPF库提供了一个名为NumericUpDown的控件,它是一个数字字段,带有一个用于增加和减少值的按钮。 我正在制作一个源自这个 co 的控件......
使用 App.Resources 中的字符串作为 ResourceDictionary 的 Source 属性的值 {WPF}
我想在不使用资源文件 (*.resx) 的情况下在我的应用程序中实现多种语言,并且我想为此使用一个单独的 ResourceDictionary,Text/en-US.xaml。因为我想要
我想要几个不同颜色的 SolidColorBrushes,但都应该具有相同的不透明度(基于 x:Name="numCol...中转换器返回的不透明度...