• 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;


  • 相关阅读:
    选择器的区别
    固定DIV样式
    图片自适应不变形
    CSS实现图片在div a标签中水平垂直居中
    2017年校招全国统一模拟笔试 页码统计
    LeetCode 23. Merge k Sorted Lists
    LeetCode 15. 3Sum
    LeetCode 12. Integer to Roman
    LeetCode 11. Container With Most Water
    LeetCode 8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/llguanli/p/8880117.html
Copyright © 2020-2023  润新知