zoukankan      html  css  js  c++  java
  • WPF对象级资源的定义与查找

    文章概述:

    本演示介绍了怎样定义WPF对象级的资源,并通过XAML代码和C#訪问和使用对象级资源。

    相关下载(代码、屏幕录像)http://pan.baidu.com/s/1hqvJNY8

    在线播放:http://v.youku.com/v_show/id_XODA1NTU2Mzky.html

    温馨提示:假设屏幕录像和代码不能正常下载,可站内留言。或发邮件到524130780@QQ.COM


    一、完整的定义和使用资源

    <Window x:Class="Demo008.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="Resource" FontSize="16" Loaded="Window_Loaded">
        <!--完整的写法-->
        <Window.Resources>
            <ResourceDictionary>
                <sys:String x:Key="str">沉舟側畔千帆过,病树前头万木春。

    </sys:String> <sys:Double x:Key="dbl">3.1415926</sys:Double> </ResourceDictionary> </Window.Resources> <StackPanel> <TextBlock Margin="5" Text="{StaticResource ResourceKey=str}" /> </StackPanel> </Window>


    二、简写的资源定义和使用
    <Window x:Class="Demo008.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="Resource" FontSize="16" Loaded="Window_Loaded">
        <!--简写-->
        <Window.Resources>
            <sys:String x:Key="str">沉舟側畔千帆过。病树前头万木春。

    </sys:String> <sys:Double x:Key="dbl">3.1415926</sys:Double> </Window.Resources> <StackPanel> <TextBlock x:Name="TextBlock1" Margin="5" Text="{StaticResource str}" /> </StackPanel> </Window>


    三、代码查找资源
    通常的做法例如以下所看到的:
    string text = this.FindResource("str").ToString();
    this.TextBlock1.Text = text;

    假设知道资源位于哪个对象的资源字典中能够使用例如以下的方式直接訪问:
    string text = this.Resources["str"].ToString();
    this.TextBlock1.Text = text;


  • 相关阅读:
    python基础(set)补充
    运算
    初识正则表达式
    xml中俩种解析方式
    Android Studio 调用夜神模拟器
    Android Studio 配置虚拟设备的镜像文件的存放路径
    Allegro PCB Design GXL (legacy) 使用slide推挤走线,走线的宽度就发生改变的原因
    OrCAD Capture CIS 16.6 在原理图页面内放置图片
    OrCAD Capture CIS 16.6 修改原理图的页面大小
    ESD选型指南
  • 原文地址:https://www.cnblogs.com/llguanli/p/8880117.html
Copyright © 2011-2022 走看看