我正在使用ResourceDictionary,但我希望能够使用其他项目查找值或键.每个都是独一无二的,所以这不是问题.是否有具有此双面查找功能的类型?
我有以下,很容易重现问题:我正在创建一个使用其他文件资源的xaml应用程序.要做的就是创建一个MergedDictionaries-tag来合并本地和全局资源,如下所示:
<Window>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="path.to.xaml.file"/>
<ResourceDictionary>
<Style TargetType="{x:Type Border}" x:Key="TypeBlock">
</Style>
<Style TargetType="{x:Type Border}" x:Key="SetBlock">
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
....
</Window>
Run Code Online (Sandbox Code Playgroud)
如果运行它,这段代码会崩溃:
Item has already been added. Key in dictionary: 'System.Windows.Controls.Border' Key being added: 'System.Windows.Controls.Border'
Run Code Online (Sandbox Code Playgroud)
如果我们删除MergedDictionaries-tag,代码将按预期运行:
<Window>
<Window.Resources>
<Style TargetType="{x:Type Border}" x:Key="TypeBlock">
</Style>
<Style TargetType="{x:Type Border}" x:Key="SetBlock">
</Style>
</Window.Resources>
</Window>
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它在我们使用Merged Resources时抛出异常.当然,修复现在很容易(将资源调到较低的水平).很高兴知道这是否是"正常"行为......
我在Silverlight 4页面上有四个数据网格.我正在尝试为每个网格设置不同的列标题样式.我发现这个XAML在我将其嵌入每个DataGrid里面的<sdk:DataGrid.ColumnHeaderStyle>标签时有效:
<Style TargetType="primitives:DataGridColumnHeader" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:DataGridColumnHeader">
<Grid Name="Root">
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="SortStates" >
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition GeneratedDuration="00:00:0.1" />
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="Unsorted" />
<vsm:VisualState x:Name="SortAscending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="SortDescending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
<DoubleAnimation Storyboard.TargetName="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="BackgroundRectangle" …Run Code Online (Sandbox Code Playgroud) silverlight datagrid styles resourcedictionary silverlight-4.0
我有一个WPF 4.0应用程序,它在菜单命令之类的东西中使用了一些自定义的16x16图标.我想(现在)有两组图标,默认的Vista/7-ish和一些XP-ish.我想要的是让当前的操作系统确定使用哪些图标.
现在,我已经在主题资源字典(即Aero.NormalColor.xaml等)中定义了指向特定PNG资源的BitmapImage资源.
<!-- Aero.NormalColor.xaml -->
<BitmapImage x:Key="IconSave" UriSource="/MyWPFApp;component/Resources/Icons16/Aero/disk.png"/>
<!-- Luna.NormalColor.xaml -->
<BitmapImage x:Key="IconSave" UriSource="/MyWPFApp;component/Resources/Icons16/Luna/disk.png"/>
Run Code Online (Sandbox Code Playgroud)
在我的应用中想要显示图标的任何地方都将Image/Icon的source属性设置为StaticResource到其中一个BitmapImages.
<Image Source="{StaticResource IconSave}"/>
Run Code Online (Sandbox Code Playgroud)
我们的想法是,由于WPF根据当前的操作系统和主题自动加载主题词典,因此只会加载一组BitmapImage资源,并且图标会神奇地成为合适的图标.
但是,这不起作用,我在运行时遇到了可怕的"无法找到资源"异常.我的预感是,这是因为主题文件只搜索自定义控件,Image不是.
Blend 4对这些没有任何问题,但它已经定义了其特殊的DesignTimeResources.xaml文件,并在Aero.NormalColor.xaml上进行了合并.VS2010扼流圈,但它也没有使用像DesignData文件这样的东西,所以我并不感到惊讶.我目前还有一个单独的资源字典文件(MainSkin.xaml),它被合并到Application资源中.引用样式等在运行时可以正常工作.
我是在正确的轨道上,只是有一些轻微的错误?我是否需要做一些完全不同的事情才能获得理想的效果,如果有,那该怎么办?
如何在运行时更改WPF静态资源的值?
我有以下资源
<UserControl.Resources>
<sys:String x:Key="LengthFormat">#.# mm</sys:String>
<sys:String x:Key="AreaFormat">#.# mm²</sys:String>
<sys:String x:Key="InertiaFormat">#.# mm?</sys:String>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
一些文字块参考
<TextBlock Grid.Row="2" Grid.Column="1"
Text="{Binding Path=Breadth, StringFormat={StaticResource ResourceKey=LengthFormat}}" />
Run Code Online (Sandbox Code Playgroud)
然后根据要绑定到控件的对象,我想改变格式.我在控件中设置了如下属性:
public string LengthFormat
{
set
{
this.Resources["LengthFormat"] = value;
}
}
public string AreaFormat
{
set
{
this.Resources["AreaFormat"] = value;
}
}
public string InertiaFormat
{
set
{
this.Resources["InertiaFormat"] = value;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在绑定之前我设置每个字符串.
但它不起作用,有人建议whynot?
干杯
编辑:当使用标准.NET ResourceDictionary时也会出现此问题,并且似乎是在控件或数据模板中使用资源字典的问题.
我有一个自定义资源字典遵循共享资源实例的常见方法.
http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html
public class SharedResourceDictionary : ResourceDictionary
{
static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();
Uri _sourceUri;
public new Uri Source
{
get
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
return base.Source;
return this._sourceUri;
}
set
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
{
base.Source = value;
return;
}
this._sourceUri = value;
WeakReference<ResourceDictionary> cached;
if (SharedDictionaries.TryGetValue(value, out cached))
{
ResourceDictionary rd;
if (cached.TryGetTarget(out rd))
{
this.MergedDictionaries.Add(rd);
return; …Run Code Online (Sandbox Code Playgroud) 有没有办法获得特定WPF资源的价值变化的通知?
我们需要在WPF应用程序中动态调整内容字体大小...对于WPF控件,我们设置Control.FontSize为动态资源,字体自动调整大小.不幸的是,我们还有一个嵌入式winforms控件,其字体大小不能这样设置.我们的想法是订阅在每个资源值更改时触发的事件,并实现winforms控件的自定义刷新.有什么建议吗?
先感谢您!
我有一些我想从App.xaml中的代码访问的字符串,如:
<Application.Resources>
<ai:TelemetryContext x:Key="ApplicationInsightsBootstrapper" xmlns:ai="using:Microsoft.ApplicationInsights"/>
<ResourceDictionary x:Key="rd">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlobalStylesResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources. -->
<x:String x:Key="AppName">Platypus</x:String>
<x:String x:Key="BingMapsNamespace">http://schemas.microsoft.com/search/local/ws/rest/v1</x:String>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我想以编程方式获取这些值,所以我编写了这个实用程序函数:
internal static String GetResourceString(String keyStr)
{
var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
return resldr.GetString(keyStr);
}
Run Code Online (Sandbox Code Playgroud)
但如果失败," 在Platypus.exe WinRT信息中发生了'System.Exception'类型的第一次机会异常:找不到ResourceMap. "
我以为我在这里找到了问题的解决方案,据说这样做的方式已被弃用,我应该这样做:
internal static String GetResourceString(String keyStr)
{
try
{
//var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
//return resldr.GetString(keyStr);
var resldr = ResourceLoader.GetForCurrentView();
return resldr.GetString(keyStr);
}
catch (Exception ex)
{
Debug.WriteLine("Something wrong with the keyStr? Exception in GetResourceString(): {0}", ex.Message);
} …Run Code Online (Sandbox Code Playgroud) c# resourcedictionary app.xaml winrt-xaml windows-store-apps
我有一个在XAML文件中定义的矢量图像
Image.xaml
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_power" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="38" Height="41.1667" Canvas.Left="19" Canvas.Top="17.4167" Stretch="Fill" Fill="#FFFFFFFF" Data="F1 M 36.4167,36.4167L 36.4167,17.4167L 41.1667,17.4167L 41.1667,36.4167L 36.4167,36.4167 Z M 57,39.5833C 57,50.0767 48.4934,58.5833 38,58.5833C 27.5066,58.5833 19,50.0767 19,39.5833C 19,30.7301 25.0552,23.2911 33.25,21.1819L 33.25,27.8374C 28.6079,29.7165 25.3333,34.2675 25.3333,39.5833C 25.3333,46.5789 31.0044,52.25 38,52.25C 44.9956,52.25 50.6667,46.5789 50.6667,39.5833C 50.6667,34.8949 48.1194,30.8014 44.3333,28.6113L 44.3333,21.6645C 51.7129,24.2728 57,31.3106 57,39.5833 Z "/>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
如果我修改此图像的XAML代码(例如Path的Fill属性),则会立即在Visual Studio 2015的"设计"窗口中显示更改.
现在我想创建一个引用此图像的ResourceDictionary.我将xaml代码直接包含在ResourceDictionary中,但在这种情况下我失去了预览的能力(Visual Studio中没有设计窗口,我得到"MyResourceDictionary.xaml无法在设计视图中编辑").
MyResourceDictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Project.XamlResources">
<Canvas x:Key="appbar_power" x:Name="appbar_power" Width="76" Height="76" Clip="F1 M …Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用ResourceDictionaries其他程序集的应用程序,但在使用字体时遇到了问题。
有一个名为MyFontAssembly的程序集将字体以及对它们的引用存储为ResourceDictionary. 它的结构如下:
MyFontAssembly
- FontDictionary.xaml - (stores references to fonts)
- Fonts
- FontA.ttf
- FontB.ttf
...
Run Code Online (Sandbox Code Playgroud)
还有另一个程序集用于存储ResourceDictionaries样式控件,它称为MyStylesAssembly。然后将 MyStylesAssembly 中的 ResourceDictionaries 合并到应用程序的 App.xaml 中,以提供可重用的样式。
问题是我的样式确实识别字体资源(代码没有崩溃,因为它无法通过其键找到资源),但看起来没有应用存储为 ttf 文件的字体。
我在我的FontDictionary.xaml.
<FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/Fonts/#MyFontName</FontFamily>
Run Code Online (Sandbox Code Playgroud)
笔记:
<FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily>在具有相同结构的单个 exe 项目中使用实现,一切都很好,当我在重构期间将实现拆分为几个程序集时出现问题,就像我描述的那样。MyFontAssembly已MyStylesAssembly正确合并。我在这里只是用那个名字来称呼它,在实际代码中,它还有一些ResourceDictionaries由MyStylesAssembly. 问题似乎只是<FontFamily>标签无法识别 ttf 文件。MyFontAssembly并且MyStylesAssembly是类型的项目ClassLibrary并且不包含除 in …wpf ×7
c# ×4
xaml ×4
styles ×2
app.xaml ×1
datagrid ×1
datatemplate ×1
fonts ×1
icons ×1
silverlight ×1
winforms ×1
winrt-xaml ×1