<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="one">ONE</sys:String>
<sys:String x:Key="two">TWO</sys:String>
<sys:String x:Key="three">THREE</sys:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
现在想要使用 C# 在 WPF 中通过代码隐藏动态创建与上面相同的资源 ResourceDictionary 。可以这样创建吗?
我有一个带有矢量字形集的 XAML ResourceDictionary。
ResourceDictionary 示例(带有单个字形):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Width="32" Height="32">
<Path Fill="#FF444444">
<Path.Data>
<PathGeometry Figures="M27.125 21.688c0-0.438-0.125-0.875-0.5-1.188l-3.688-3.688c-0.313-0.375-0.75-0.5-1.25-0.5s-0.875 0.188-1.25 0.563c0 0 0.125 0.125 0.313 0.313s0.313 0.313 0.375 0.375c0.063 0.063 0.188 0.188 0.313 0.375 0.125 0.125 0.188 0.25 0.188 0.438 0.063 0.125 0.063 0.313 0.063 0.5 0 0.438-0.125 0.875-0.5 1.188-0.313 0.313-0.688 0.5-1.188 0.5-0.188 0-0.313 0-0.5-0.063-0.125-0.063-0.313-0.125-0.438-0.25-0.188-0.063-0.25-0.188-0.375-0.25-0.063-0.063-0.188-0.188-0.375-0.375s-0.25-0.313-0.313-0.313c-0.375 0.313-0.563 0.75-0.563 1.25s0.125 0.938 0.5 1.25l3.625 3.688c0.375 0.313 0.75 0.5 1.25 0.5 0.438 0 0.875-0.188 1.188-0.5l2.625-2.563c0.375-0.375 0.5-0.75 0.5-1.25zM14.563 9.125c0-0.5-0.125-0.875-0.5-1.188l-3.625-3.75c-0.375-0.313-0.75-0.5-1.25-0.5-0.438 0-0.875 0.188-1.188 0.5l-2.625 2.625c-0.375 0.313-0.5 0.75-0.5 1.188 0 …Run Code Online (Sandbox Code Playgroud) 我想在 UWP 中使用 ResourceDictionary 就像我在 WPF 中使用的那样在 WPF 中,我在 ResourceDictionary 文件(*Style.xaml)中执行此操作
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:models="using:NWP.Models">
<Style x:key="MenuContent" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<controls:DockPanel>
<ItemsControl ItemsSource="{x:Bind How-Can-I-Bind-Collection-Here?}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:MenuItemModel">
<RadioButton GroupName="MenuItems" IsChecked="{x:Binding IsChecked, Mode=TwoWay}" MinHeight="0" MinWidth="0" Command="{Binding Command}" CommandParameter="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</controls:DockPanel>
</ControlTemplate>
</Setter.Value>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
然后我可以在我的页面中使用这种样式:
<ContentControl Style="{StaticResource MenuContent}">
<StackPanel Orientation="Vertical">
<TextBox/>
<PasswordBox/>
<Button Content="Login"/>
</StackPanel>
</ContentControl> …Run Code Online (Sandbox Code Playgroud) 我在项目中有ResourceDictionary一个xaml名为 SylesResourceDictionary.xaml 的文件Class Library。这ResourceDictionary通过合并在不同视图中使用。
我是新手ResourceDictionary,不确定我是否以正确的方式做这件事。不同的文章建议不同的“构建操作”Page和“自定义工具”XamlIntelliSenseFileGenerator
Build Action这个 xaml 文件应该是什么?我已将其设置为资源
应该是什么Custom Tool?我将其设置为 MSBuildCompile
我应该删除xaml.cs这个 xaml 文件吗?
我正在尝试在我的 WPF 程序中使用 ResourceDictionary 和 Style。当我只有 ResourceDictionary 时,<Window.Resources>一切工作正常,但一旦我添加一个<Style>程序,就会显示字典的“找不到资源”,并且出现错误“无法解析资源“PlusMinusExpander”。”
<Window.Resources>
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="#dddddd" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<ResourceDictionary x:Key="hello" Source="Assets/PlusMinusExpanderStyles.xaml" />
</Window.Resources>
<Expander Header="Plus Minus Expander" Style="{StaticResource PlusMinusExpander}" HorizontalAlignment="Right" Width="292">
<Grid Background="Transparent">
<TextBlock>Item1</TextBlock>
</Grid>
</Expander>
Run Code Online (Sandbox Code Playgroud)
Style="{StaticResource PlusMinusExpander}"我希望即使在添加CurrencyCellStyle 样式之后也能做到这一点。我在网上看到过类似的问题,但他们的解决方案还没有对我有用。有没有办法同时使用样式和资源字典?
我ResourceDictionary在App.xaml文件中声明如下:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/DefaultSkin.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试在启动时加载不同的皮肤(使用App.xaml.cs构造函数加载最后使用的ResourceDictionary皮肤)时,我发现ResourceDictionary设置Application.Resources覆盖了这个并恢复到 DefaultSkin.xaml 文件 - 即使我Application.Current.Resources.MergedDictionaries.Clear();之前使用过选择所需的皮肤。
当我删除ResourceDictionaryfrom时,我的应用程序运行良好Application.Resources- 但随后所有 xaml 引用在设计时都丢失了。如何在设计时保留此引用但在运行时将其删除,然后才能覆盖我的皮肤选择?
如何使用C#访问资源字典中的内容?
例如,这是我在XAML中的代码:
<system:String x:Key="NewGroup">New Group Name</system:String>
Run Code Online (Sandbox Code Playgroud)
我想在C#中访问它:
private void OnAddGroup(object sender, ExecutedRoutedEventArgs e)
{
BooksGroupInfo group = new BooksGroupInfo();
group.GroupName = "New Group" + TheTabControl.Items.Count;
TabItem tab = AddGroup(group);
_currentLibrary.addGroup(group);
_currentLibrary.CurrentGroup = group;
}
Run Code Online (Sandbox Code Playgroud)
而不是在C#中键入"新组",我想替换它并在XAML中的资源字典中访问.因此该命令将自动获取资源字典中的Name.
我尝试了几个解决方案,如:
(System.String)this.FindResource("NewGroup");
Application.Current.Resources[typeof(System.String)];
Run Code Online (Sandbox Code Playgroud)
等等...但它们似乎不起作用.
我正在使用locbaml进行本地化,它不解析C#上的文本/名称(或者我不知道如何),这是我认为唯一可行的解决方案.
我试图在松散的XAML中使用ResourceDictionary并将其加载到运行时以向WPF应用程序提供模板和样式.我在本地目录中提供了XAML,并在app启动时使用URI向App.Current.Resources.MergedDictionaries添加了一个新的ResourceDictionary.
当XAML进行解析时,它会在模板上爆炸,其中TargetType是来自正在使用它的程序集的自定义控件.
具体信息是:
'无法从文本'controls:CustomType'创建'Type'.'
我已经在顶部的ResourceDictionary中映射了名称空间:
的xmlns:对照= "CLR-名称空间:TEST.UI.WPF.Common.Controls"
有文章说明从松散的XAML加载ResourceDictionaries是可能的,但我找到的那些都没有在那些松散的XAML文件中找到自定义类型.
任何帮助表示赞赏!
为了解决分辨率,我们需要缩放艺术 好的,因此堆栈中提到的常见来源是Syncfusion Metro Studio.
Metro Studio 2为XAML生成:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox x:Key="error">
<Grid Width="64" Height="64" Visibility="Visible">
<Grid Visibility="Visible">
<Rectangle Fill="#FFD21818" Visibility="Visible" />
<Ellipse Fill="#FFD21818" Visibility="Collapsed" />
<Path Data="M50.5,4.7500001C25.232973,4.75 4.75,25.232973 4.7500001,50.5 4.75,75.767029 25.232973,96.25 50.5,96.25 75.767029,96.25 96.25,75.767029 96.25,50.5 96.25,25.232973 75.767029,4.75 50.5,4.7500001z M50.5,0C78.390381,0 101,22.609621 101,50.5 101,78.390381 78.390381,101 50.5,101 22.609621,101 0,78.390381 0,50.5 0,22.609621 22.609621,0 50.5,0z" Stretch="Fill" Fill="#FFD21818" Visibility="Collapsed" />
</Grid>
<Path Data="F1M54.0573,47.8776L38.1771,31.9974 54.0547,16.1198C55.7604,14.4141 55.7604,11.6511 54.0573,9.94531 52.3516,8.23962 49.5859,8.23962 47.8802,9.94531L32.0026,25.8229 16.1224,9.94531C14.4167,8.23962 11.6511,8.23962 9.94794,9.94531 8.24219,11.6511 8.24219,14.4141 9.94794,16.1198L25.8255,32 9.94794,47.8776C8.24219,49.5834 8.24219,52.3477 9.94794,54.0534 11.6511,55.7572 14.4167,55.7585 16.1224,54.0534L32.0026,38.1745 47.8802,54.0534C49.5859,55.7585 52.3516,55.7572 54.0573,54.0534 55.7604,52.3477 …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Xamarin.Forms应用程序.我无法从代码背后设置样式.以下代码位于我的页面的构造函数中.
Style greenButton = new Style(typeof(Button))
{
Setters =
{
new Setter{Property = Button.BackgroundColorProperty, Value = Color.Green },
new Setter{Property = Button.TextColorProperty, Value = Color.Red}
}
};
Resources = new ResourceDictionary();
Resources.Add(greenButton);
Button createSL = new Button();
createSL.Text = "Create Stack Layout";
createSL.Style = (Style) Resources["greenButton"];
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了此错误消息.
未找到密钥异常表示字典中不存在"greenButton".
但是我已经完成了Xamarin.Forms文档中提到的所有内容.请帮我解决一下!
c# ×8
wpf ×7
xaml ×6
.net-4.0 ×1
canvas ×1
localization ×1
locbaml ×1
skinning ×1
syncfusion ×1
uwp ×1
wpf-controls ×1
xamarin ×1
xbind ×1