我有一个资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfUI2.MainWindowEvents">
<DataTemplate
x:Key="WorkspacesTemplate">
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
...
Run Code Online (Sandbox Code Playgroud)
我想为TabControl添加一个事件处理程序.MainWindowEvents是在没有其他类的文件中定义的类:
Namespace wpfUI2
Public Class MainWindowEvents
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
当我去添加一个事件处理程序时
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
SelectionChanged=""
/>
Run Code Online (Sandbox Code Playgroud)
并尝试在""之间单击以创建事件我收到一条错误,指出x:Class属性指定的类必须是文件中的第一个.好吧!奇怪的是,当我手动创建处理程序时:
Namespace wpfUI2
Public Class MainWindowEvents
Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
End Sub
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
一切都编译好,但我在window.show上得到一个运行时异常
我究竟做错了什么?
我正在尝试ResourceDictionary在WPF UserControl库项目中创建一个内部.当我添加以下样式时:
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ResourceKey=GreyBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource ResourceKey=LightBlueBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ResourceKey=OrangeBrush}"/>
</Trigger>
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="{StaticResource ResourceKey=LightOrange}" Duration="0:0:.1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说:
The type 'x:Type' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Run Code Online (Sandbox Code Playgroud)
我宣布x为:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Run Code Online (Sandbox Code Playgroud)
这在我在WPF应用程序项目中创建资源字典时有效,但不在UserControl库项目中创建.知道为什么吗?
事件设置器不能用于主题资源字典中包含的样式.这是因为运行时的主题资源字典通常是松散的二进制XAML(BAML)文件,并且没有定义任何范围,其中定义处理程序的后续代码隐藏可以存在.
这个SO问题的第一个答案证实了这一点,该问题指出:
资源xaml不能有文件后面的代码,它们通常被称为"松散的xaml".您可以在msdn中了解有关EventSetter的内容.
但是,我还不了解事件设定者的限制.我尝试使用代码隐藏文件作为资源字典.我为在所述资源字典中的样式中定义的模板中包含的控件事件分配了一个事件处理程序 - 并且它起作用.
另一方面,将事件设置器添加到同一资源字典中的样式会导致异常.
我是否遇到了碰巧工作的特殊情况?
或者在模板中设置事件处理程序始终有效,但如果是这样,那么为什么我不能在同一资源字典中的样式中使用事件设置器?
我的问题归结为:
MSDN声明主题资源字典通常是松散的二进制XAML 究竟是什么意思- 在什么情况下究竟是什么时候?
鉴于我有一个shell应用程序和一些使用Microsoft CompoisteWPF(Prism v2)的独立模块项目......
在接收命令时,模块创建新的ViewModel并通过区域管理器将其添加到区域.
var viewModel = _container.Resolve<IMyViewModel>();
_regionManager.Regions[RegionNames.ShellMainRegion].Add(viewModel);
Run Code Online (Sandbox Code Playgroud)
我想我可以在模块中创建一个资源字典并设置一个数据模板来显示已加载的视图模型类型的视图(参见下面的xaml).但是当视图模型添加到视图中时,我得到的只是打印出的视图模型命名空间.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Modules.Module1.ViewModels"
xmlns:vw="clr-namespace:Modules.Module1.Views"
>
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<vw:MyView />
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
编辑:
我可以通过添加到App.xaml来实现它
<Application.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Module1;component/Module1Resources.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Module2;component/Module2Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
这很好,但这意味着在创建新模块时,需要添加App.xaml文件.我正在寻找的是模块的一种方式,因为它们加载动态添加到Application.Resources.这可能吗?
当我创建一个新的WPF项目时,我可以在Add New Item Window中找到ResourceDictionary.但我还有另一个项目,我找不到,我不知道为什么.

更新:该项目最初是针对.net 3.5的,但现在它还有.net 4.0的版本.这意味着同一个项目中有两个.sln文件(一个用于3.5,另一个用于4.0).
我想在我的Windows.Resources中引用MergedDictionary和本地声明的资源.但是,我收到此错误:
"添加到IDictionary的所有对象必须具有Key属性或与其关联的其他类型的键."
是否可以将本地资源与导入的资源混合在同一个Window.Resources中?
XAML是:
<Window.Resources>
<CollectionViewSource x:Key="cvsData" Source="{Binding Path=Data}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Country"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="images" Source="pack://application:,,,/CoreWpfControls;component/Images.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
谢谢杰里米
我有一个在多个视图中引用的类,但我希望它们之间只共享一个类的实例.我已经实现了我的课程:
using System;
public class Singleton
{
private static Singleton instance;
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将Singleton.Instance作为资源添加到我的资源字典中?我想写点类似的东西
<Window.Resources>
<my:Singleton.Instance x:Key="MySingleton"/>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
而不是{x:static my:Singleton.Instance}每次我需要引用它时都要写.
我在App.xaml中有以下代码集:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
该NavigationPanel.xaml包含了风格,看起来像这样:
<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
<Setter Property="Background" Value="{StaticResource DarkBackground}" />
<Lots of XAML>
</Style>
Run Code Online (Sandbox Code Playgroud)
的{StaticResource的DarkBackground}在定义Brushes.xaml文件(即,第一资源字典).它被定义为
<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />
Run Code Online (Sandbox Code Playgroud)
在资源字典中.
在运行时,我收到以下错误:
Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]
Run Code Online (Sandbox Code Playgroud)
行号和位置引用app.xaml中的NavigationPanel.xaml资源字典.
我可以从其他控件引用画笔,而不是包含的资源字典.
为什么我不能引用或为什么它不解析对合并资源字典的层次结构更高的资源的引用?我在这里错过了什么?
如何将样式中的BasedOn标记指定为某些其他文件中定义的样式.
例,
Dictionary1.xaml定义
<Style x:Key="basicStyle" TargetType="TextBlock" >
<Setter Property="FontSize" Value="24"></Setter>
<Setter Property="Foreground" Value="DarkGray"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
在Dictionary2.xaml中我需要类似的东西
<Style x:Key="headerStyle" TargetType="TextBlock" >
<Setter Property="FontSize" Value="46"></Setter>
<Setter Property="Foreground" Value="DarkGray"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
怎么做到这一点?
我有两个库和一个应用程序集程序项目布局,我想知道如何通过其中一个我在另一个库中定义的库中的键来引用样式.
我的解决方案布局
我已经看到一些示例解释如何使用简单的单个控件库和应用程序组件来执行此操作,但在我的情况下,我有两个程序集和一个应用程序.所以换句话说在我的情况下,我的第二个控件库中没有App.xaml,我可以使用MergeDictionaries概念.我很确定我是否可以直接在每个控件XAML文件中放置一个MergedDictionaries引用来合并WpfControls.dll程序集中定义的所有Generic.xaml但我的猜测是这会导致样式被冗余地合并到各个地方在我的SpecializedControls程序集中,这似乎不是最好的解决方案.更糟糕的是,这些词典也可能合并到Application.exe中,这会在运行时开始膨胀我的应用程序吗?
任何人都可以用最少的编程和资源开销来澄清分享这些样式的推荐或最佳方法.
更新:经过一些测试后,即使在同一个程序集中,我也无法通过它的密钥引用Generic.xaml中定义的任何资源.关于我如何理解资源键的工作方式可能存在一些完全错误,所以我需要进行更多的研究以及调查ComponentResourceKey.如果有人有任何提示或建议,请帮助.
wpf ×9
c# ×3
styles ×2
xaml ×2
.net ×1
assemblies ×1
datatemplate ×1
eventsetter ×1
mvvm ×1
prism ×1
silverlight ×1
wpf-controls ×1