wpf 加载资源文件

方法一:App.xaml页面上写

<Application x:Class="LanguageChange.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="skin/aa.xaml"/>
<ResourceDictionary Source="skin/bb.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

</Application.Resources>
</Application>

 

方法二:后台实现写法

System.Windows.Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary { Source = new Uri(aa, UriKind.RelativeOrAbsolute) };

转载于:https://www.cnblogs.com/ping2015/p/7049438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,我们可以使用独立源文件(.xaml)来定义应用程序中的样式、模板、图像和字符串等源。这些源文件可以在多个页面或窗口中共享,并可以动态地加载和卸载,提高了应用程序的可维护性和可扩展性。 下面是一个简单的独立源文件的示例: ```xml <!-- AppResources.xaml --> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="ButtonBackgroundBrush" Color="#FF006699" /> <SolidColorBrush x:Key="ButtonForegroundBrush" Color="White" /> <Style TargetType="Button" x:Key="MyButtonStyle"> <Setter Property="Background" Value="{StaticResource ButtonBackgroundBrush}" /> <Setter Property="Foreground" Value="{StaticResource ButtonForegroundBrush}" /> <Setter Property="FontSize" Value="16" /> <Setter Property="Padding" Value="10,5" /> <Setter Property="Margin" Value="5" /> </Style> </ResourceDictionary> ``` 在上面的示例中,我们定义了两个 SolidColorBrush 类型的源,分别用于按钮的背景和前景色。同时,我们还定义了一个名为 MyButtonStyle 的按钮样式,它使用了刚才定义的两个源。 要在应用程序中使用这个独立源文件,可以在 App.xaml 文件中引用它,如下所示: ```xml <!-- App.xaml --> <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="AppResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> ``` 在上面的示例中,我们将 AppResources.xaml 文件引用到了 Application.Resources 中,并使用了 MergedDictionaries 属性来合并多个源文件。这样,在应用程序的任何页面或窗口中,都可以使用 AppResources.xaml 中定义的源了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值