C# 加载引用 XAML cs中动态加载xaml xaml中引用xaml

C# cs代码中加载 XAML

1、全局加载

csharp 复制代码
ResourceDictionary languageResDic = new ResourceDictionary();
languageResDic.Source = new Uri("View/Theme.xaml", UriKind.RelativeOrAbsolute);
this.Resources.MergedDictionaries.Add(languageResDic);

2、动态加载外部xaml文件

csharp 复制代码
using (FileStream s = new FileStream(@"C:\Users\Lin.net\Desktop\WpfApplication1\Xaml\Test.xaml", FileMode.Open))
{
    Grid grid = XamlReader.Load(s) as Grid;
    Window win = new Window1();
    win.Content = grid;
    win.ShowDialog();
}

3、动态加载内部xaml

1、动态加载

csharp 复制代码
Button btn =new  Button();
btn.SetValue(Button.StyleProperty, Application.Current.Resources["资源名称"]);



Style appButtonStyle = (Style)Application.Current.Resources["appButtonStyle"];

2、动态加载

csharp 复制代码

4、Xaml引用内部xaml资源文件

1、资源文件

csharp 复制代码
<Window.Resources>
    <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
        <Setter Property="Height" Value="72" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Background" Value="Black" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
</Window.Resources>

2、加载

csharp 复制代码
<Button Style="{StaticResource myBtnStyle}"></Button>

5、xaml资源文件引用xaml外部资源文件

1、资源文件

csharp 复制代码
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib">
    <Style x:Key="myBtnStyle" TargetType="Button">
    <Setter Property="Height" Value="72" />
    <Setter Property="Width" Value="150" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="Blue" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
</ResourceDictionary>

2、加载

csharp 复制代码
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
相关推荐
小皮侠2 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
一名用户3 小时前
unity实现梦日记式传送组件
后端·c#·unity3d
ddfa12343 小时前
XML 笔记
xml·服务器
海外空间恒创科技4 小时前
一台香港原生ip站群服务器多少钱?
服务器·网络协议·tcp/ip
Charlene Fung4 小时前
vs code远程自动登录服务器,无需手动输入密码的终极方案(windows版)
运维·服务器·vscode·ssh
碣石潇湘无限路4 小时前
【部署与总结】从本地运行到公网服务器的全过程
运维·服务器
阿蒙Amon4 小时前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
linux修理工4 小时前
ipmitool 使用简介(ipmitool sel list & ipmitool sensor list)
运维·服务器
zyx没烦恼4 小时前
TCP相关实验
服务器·网络·tcp/ip
朗晴5 小时前
文本编辑器VIM的使用方法!
linux·运维·服务器