我确信这个问题或它的衍生物已被问过无数次,但我找不到任何帮助我解决问题的东西,所以我问.请随时告诉我我确定存在但我找不到的副本.显然我对关键词的关注度不高.
我有一个自定义控件,它有自己的资源字典,仅用于定义控件模板.然后将该字典合并到Generic.xaml中.
问题是当这个控件出现在UI中时,它内部没有任何内容.我用Snoop找到了这个.控件位于UI中,但它完全为空.
您将在下面找到我认为对此问题负责的项目.我们非常感谢您提供的任何帮助或建议.
我的文件夹结构的相关部分是这样的:

BasicTemplate.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFSpecBuilder.Layouts.Templates">
<Style TargetType="{x:Type local:BasicTemplate}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BasicTemplate}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<TextBlock Text="This is a basic template." />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Layouts/Templates/XAML/BasicTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud) 我有一个WPF UserControls库和一个在库中共享的ResourceDictionary.
此库中的所有UserControl仅出现在单个"shell"父控件中,该控件实际上只是一组较小控件的容器.当我添加以下XAML时,我能够按预期从我的shell控件访问ResourceDictionary
<Control.Resources>
<ResourceDictionary Source="MyResources.xaml" />
</Control.Resources>
Run Code Online (Sandbox Code Playgroud)
但是,我无法从位于"shell"控件内的子控件访问ResourceDictionary.
我的印象是WPF应该在本地检查资源,然后向上遍历,直到找到适当的资源?
相反,我得到了
Cannot find resource named '{BoolInverterConverter}'.
Resource names are case sensitive. Error at
object 'System.Windows.Data.Binding' in markup file...
Run Code Online (Sandbox Code Playgroud)
显然,我可以(并且)在我的子控件中引用ResourceDictionary; 但现在每个控件都需要引用这个字典,我相信这不是必需的.
任何想法,我做的事情是奇怪的还是我对行为的期望不正确?
我有一个导入MEF组件,它在导入向导打开时动态加载.一旦用户选择了她想要处理的导入类型,就会将对导入向导对话框的控制传递给所选的导入组件.当然,导入组件需要为向导对话框提供资源(例如DataTemplates).目前,这是通过DataTemplateSelector导入组件提供的s 实现的.他们访问ResourceDictionary导入组件程序集的本地.
但是你可以想象,这很乏味:我必须为每个DataTemplate提供的代码添加代码,WPF不会DataTemplate根据ViewModel显示的类型自动使用右边的代码.
以前有人解决了这个问题吗?你们是如何在插件环境中提供资源的?
感谢您提前提供的任何帮助.
最好的祝福
目前,我有一个包含主题/模板和字体(作为资源)的类库项目.然后我在我的主WPF应用程序项目中引用名为Shared.UI的项目.
我使用以下方法引用它:
<ResourceDictionary Source="pack://application:,,,/Shared.UI;component/Skin.xaml" />
Run Code Online (Sandbox Code Playgroud)
在MergedDictionary标记内.
我遇到的问题是主WPF应用程序无法使用项目中的字体作为资源构建.
在我的Shared.UI/Skins.xaml文件中,我有:
<!-- LABEL -->
<Style TargetType="{x:Type Label}">
<Setter Property="FontFamily" Value="./resources/#Helvetica Neue" />
<Setter Property="Foreground" Value="{DynamicResource PrimaryBlue}" />
<Setter Property="FontSize" Value="12" />
</Style>
Run Code Online (Sandbox Code Playgroud)
我假设这在我的主WPF应用程序中不起作用,因为它自己./resources查找#Helvetica Neue并且找不到它(因为它在Shared.UI中).我试图/Shared.UI;component/resources/#Helvetica Neue在资源文件中明确引用,但这对我不起作用.
我在一个xaml文件中有一个ResourceDictionary,它表示我的硬盘驱动器上某个随机文件夹中的一个皮肤.说D:\ Temp2\BlackSkin.xaml.
我已正确设置权限以提供完全访问权限,因此不是问题.
这个ResourceDictionary BlackSkin.xaml像这样引用了一个BaseSkin.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="{D:\Temp2\BaseSkin.xaml">
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
所以我正在使用XamlReader.Load(...)加载这个BlackSkin.xaml
如果我把它工作正常:
Source="D:\Temp2\BaseSkin.xaml"/>
Run Code Online (Sandbox Code Playgroud)
这当然是硬编码的路径.我想提供一个相对路径,而不是相对于正在运行的当前应用程序,但是当前正在加载的皮肤的相对路径(BlackSkin.xaml)...又名"D:\ Temp2\BaseSkin.xaml".. .or ...如果您只是"BaseSkin.xaml",因为它们位于同一个文件夹中,该文件夹与应用程序的文件夹不同.
我是否需要定义自定义标记扩展才能执行此操作?如果是这样,我会把它放在我的项目中?只需创建一个名为"Extensions"的文件夹并创建一些随机的.cs文件并在那里实现它?调用XamlReader.Load(...)时会调用这样的自定义标记扩展吗?
然后,我需要将此自定义标记扩展绑定到程序中,以从我的应用程序中的某个设置或配置文件获取完全限定的路径,并将路径作为ProvideValue函数的一部分返回?
有没有其他方法可以做到这一点?
比如我在这里调用XamlReader.Load(fileStream):
public void ApplySkin(string fileName, string baseSkinFileName)
{
if (File.Exists(baseSkinFileName))
{
ResourceDictionary baseSkinDictionary = null;
using (FileStream baseSkinStream = new FileStream(baseSkinFileName, FileMode.Open))
{
//Read in the BaseSkin ResourceDictionary File so we can merge them manually!
baseSkinDictionary = (ResourceDictionary)XamlReader.Load(baseSkinStream);
baseSkinStream.Close();
}
using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
{
// Read in ResourceDictionary File
ResourceDictionary skinDictionary = (ResourceDictionary)XamlReader.Load(fileStream);
skinDictionary.MergedDictionaries.Add(baseSkinDictionary); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我有一个子项目,它引用了图书馆项目.
在我的库项目(电话类库)中,我如何创建ResourceDictionary.xaml,我需要添加一些样式并在xaml文件和.cs文件中使用它.
我需要在我的xaml文件和.cs文件中访问ResourceDictionary.xaml中的样式如何操作?
给定一个按钮(或组合框,或任何控件):
<Button x:Name="button" Command="{Binding DoStuff}" Margin="10,0,5,5" Content="Do Stuff!" Style="{StaticResource buttonDefaults}"/>
Run Code Online (Sandbox Code Playgroud)
随风格:
<Style x:Key="buttonDefaults" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
</Style.Resources>
<Setter Property="Background">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Color="#F4083268" Offset="1"/>
<GradientStop Color="#FE5C9247"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Focusable" Value="False"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
它在Windows 8中看起来与在Windows 7中看起来非常不同.
现在,奇怪的是,在DESIGNER中,一切在Windows 8机器上看起来都很完美,就好像它是Windows 7 ......但在运行时,风格并没有"应用".
另一方面,它在设计器和运行时的Windows 7上看起来都很完美.有没有办法来解决这个问题?
额外细节:
随着风格的RD:
<Style x:Key="buttonDefaults" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero,Version=4.0.0.0,Culture=Neutral,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"/>
</Style.Resources>
<Setter Property="Background">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Color="#F4083268" Offset="1"/>
<GradientStop Color="#FE5C9247"/>
</RadialGradientBrush> …Run Code Online (Sandbox Code Playgroud) 我们在项目中使用SharedResourceDictionary.字典是在SO上的几个帖子构建的.但我把它减少到了这个:
public class SharedResourceDictionary : ResourceDictionary
{
public new Uri Source
{
get { return base.Source; }
set { base.Source = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
在新的VS2015中,XAML设计无法加载样式和抱怨
1. The resource "..." could not be resolved.
2. Object does not match target type.
3. The resource "..." could not be resolved.
4. The resource "..." could not be resolved.
5. The resource "..." could not be resolved.
Run Code Online (Sandbox Code Playgroud)
在以下标记中(下划线文本是它抱怨的地方):
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Style="_1_{StaticResource ...}__"
...
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<utils:SharedResourceDictionary _2_Source="pack://application:,,,/.../Styles.xaml"__ />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary> …Run Code Online (Sandbox Code Playgroud) 在我的UWP应用程序的开发过程中,我注意到并且有些奇怪,我很难解释.
我用户MvvmLight,我决定将ViewModelLocator资源实例添加到一个单独的ResourceDictionary Core.xaml中,该文件将MergedDictionaries在App.xaml中引用.以下是App.xaml的内容:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Core.xaml" />
<ResourceDictionary Source="Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
Core.xaml的内容:
<ResourceDictionary ...>
<viewModel:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
现在我认为Core.xaml中的资源是InitializeComponent在App.xaml.cs中的方法调用期间初始化的,但是当我尝试使用ServiceLocator该类(在MvvmLight的ViewModelLocator的构造函数中设置)时 - 就像这样ServiceLocator.Current.GetInstance<INavigationService>().Navigate<MainViewModel>();- 我得到了一个例外说:
An exception of type 'System.InvalidOperationException' occurred in
Microsoft.Practices.ServiceLocation.dll but was not handled in user code
Additional information: ServiceLocationProvider must be set.
Run Code Online (Sandbox Code Playgroud)
实际上,如果我在ViewModelLocator的构造函数中放置一个断点,则在激活Window之前不会调用它.更有趣的是 - 如果我手动引用定位器资源键(例如置于Debug.WriteLine(Resources["Locator"]);调用之上ServiceLocator),一切正常.如果我将ViewModelLocator资源直接移动到App.xaml,那么同样如此- 然后在实例化期间进行实例化 …
我最近一直在探索XAML资源词典.它们非常强大,但是为了减少(甚至进一步)为了适应任何修改而需要进行的更改,我想使用一些基本的算术运算来改变它的HeightRequest属性Entry.
我已经在充分利用OnPlatform和OnIdiom不同方面,比如FontSize.
对于iOS平台,我想做HeightRequest一个条目20+(FontSize).将FontSize使用已经设置OnIdiom(它略有增加粒).
在一个完美的世界里,我正在努力做的核心事情可能看起来像
<Setter Property="HeightRequest" Value="{DynamicResource StandardFontSize}+10">
我有一个工作的解决方案,如果我使用的组合OnIdiom和OnPlatform.
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinDesigner.App"
xmlns:local="clr-namespace:XamarinDesigner"
>
<Application.Resources>
<ResourceDictionary>
<OnIdiom x:Key="StandardFontSize" x:TypeArguments="x:Double" Tablet="22" Phone="18"/>
<Style x:Key="MyEntry" TargetType="Entry">
<Setter Property="FontSize" Value="{DynamicResource StandardFontSize}"/>
<Setter Property="HeightRequest">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="30"/>
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="40"/>
</OnIdiom.Tablet>
</OnIdiom>
</Setter.Value>
</Setter>
<Setter Property="VerticalOptions" Value="Center"/> …Run Code Online (Sandbox Code Playgroud)