WPF 資源(StaticResource 靜態資源、DynamicResource 動態資源、添加二進制資源、綁定資源樹)

原文: WPF 資源(StaticResource 靜態資源、DynamicResource 動態資源、添加二進制資源、綁定資源樹)

1、WPF對象級(Window對象)資源的定義與查找

實例一: StaticResource 靜態資源(如:皮膚配置方案,運行後不改變)

<Window x:Class="WpfApplication.Window12"
        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="Window12" Height="300" Width="300">
    <!--將System命名空間引入到xaml代碼並映射爲sys命名空間-->
    <!--在Window.Resources屬性添加兩個資源條目-->
    <Window.Resources>
       <!--鍵值對資源-->
        <ResourceDictionary>
            <sys:String x:Key="str">
                字符
            </sys:String>
            <sys:Double x:Key="dbl">
                3.1415926
            </sys:Double>
        </ResourceDictionary>       
    </Window.Resources>
    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource ResourceKey=str}" Margin="5"/>
    </StackPanel>
</Window>

簡化:css

<Window x:Class="WpfApplication.Window12"
        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="Window12" Height="300" Width="300">
    <!--將System命名空間引入到xaml代碼並映射爲sys命名空間-->
    <!--在Window.Resources屬性添加兩個資源條目-->
    <Window.Resources>

    </Window.Resources>

    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource str}" Margin="5"/>
    </StackPanel>
</Window>
html


在C#代碼中,使用定義的XAML代碼中定義的資源。數據庫

            //string text = (string)FindResource("str");
            string text = (string)this.Resources["str"];//鍵值獲取
            this.textblock1.Text = text;

實例二:把資源像CSS或JavaScript放到獨立文件中。


新建 「資源字典」express


Themes 文件夾中的 ShinyRed.xamlapp

<ResourceDictionary 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">
    <sys:String x:Key="str">字符</sys:String>
    <sys:Double x:Key="dbl">3.1415926</sys:Double>
</ResourceDictionary>

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window12" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary Source="Themes/ShinyRed.xaml"/>
    </Window.Resources>
    <StackPanel>
        <TextBlock Name="textblock1"  Margin="5" Text="{StaticResource str}"/>
    </StackPanel>
</Window>

合併多個外部資源字典成爲本地字典ide

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Xml/TreeFile.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
工具

實例3、DynamicResource 動態資源(如:運行時,容許用戶更改皮膚)

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window12" Height="300" Width="300">
    <Window.Resources>
        <TextBlock x:Key="res1" Text="動態資源"/>
    </Window.Resources>
    <StackPanel>
        <Button Content="{DynamicResource res1}"/>
        <Button Content="Update" Click="Button_Click"/>
    </StackPanel>
</Window>

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["res1"] = new TextBlock { Text="換皮膚"};
        }

3、向程序添加二進制資源

一、Resources.resx 文件添加 字符串二進制資源

雙擊 Resources.resx 文件ui


代碼:this

<Window x:Class="WpfApplication.Window13"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prop="clr-namespace:WpfApplication.Properties"
        Title="Window13" Height="300" Width="300">
    <Grid>
        <TextBox Text="{x:Static prop:Resources.UserName}"/>
    </Grid>
</Window>spa


二、Resources.resx 文件添加 圖片二進制資源

新建文件夾


設置圖片屬性


代碼:

<Image Name="img" Source="Resources/Images/4.jpg" Stretch="Fill"/>

或者

img.Source = new BitmapImage(new Uri(@"Resources/Images/4.jpg", UriKind.Relative));


4、資源綁定樹

效果:


TreeFile.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib"
                    >
    <XmlDataProvider x:Key="xdp" XPath="FileSystem/Folder">
        <x:XData>
            <FileSystem xmlns="">
                <Folder Name="基礎設置">
                    <Folder Name="數據庫地址" Value="Pages/Page_SqlAddress.xaml"/>
                    <Folder Name="配置一次圖" Value="Pages/Page_ConfigControls.xaml"/>
                </Folder>
                <Folder Name="終端模板管理">
                    <Folder Name="終端管理" Value="Pages/Page_Products.xaml"/>
                    <Folder Name="終端變量管理" Value="Pages/Page_Varibles.xaml"/>
                </Folder>
                <Folder Name="配置生成">
                    <Folder Name="生成所有" Value="Pages/Page_BuildAll.xaml"/>
                    <Folder Name="單步生成-終端" Value="Pages/Page_BuildProducts.xaml"/>
                    <Folder Name="單步生成-終端變量" Value="Pages/Page_BuildVaribles.xaml"/>
                    <Folder Name="單步生成-設備" Value="Pages/Page_BuildEquipment.xaml"/>
                    <Folder Name="單步生成-一次圖" Value="Pages/Page_BuildGraphical.xaml"/>
                </Folder>
            </FileSystem>
        </x:XData>
    </XmlDataProvider>
<ResourceDictionary>


App.xaml

<Application x:Class="AutomaticConfigurationAPP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             >
    <!--啓始頁等一堆參數設置,至關於控制檯程序Main,是整個程序入口-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Xml/TreeFile.xaml" />
                <ResourceDictionary Source="pack://application:,,,/AutomaticConfigurationAPP;component/Styles/Bootstrap.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/AutomaticConfigurationAPP;component/PathGeometries/Glyphicons.xaml"/>
                <!--<ResourceDictionary Source="/Themes/Theme.xaml" />-->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>


ViewModel層

<Window x:Class="AutomaticConfigurationAPP.MWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="配置文件自動生成工具" Height="500" Width="800" Icon="/AutomaticConfigurationAPP;component/bitbug_favicon%20%282%29.ico">
    <Window.Background>
        <ImageBrush ImageSource="/AutomaticConfigurationAPP;component/Images/background_blue.jpg" Stretch="Fill" TileMode="Tile" Viewport="0,0,1174,600" ViewportUnits="Absolute" />
    </Window.Background>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="2"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="8"/>
        </Grid.RowDefinitions>
        <TreeView  ItemsSource="{Binding Source={StaticResource xdp}}" Grid.Row="0" Grid.Column="0" x:Name="TreeRoot" Background="#E5EBF3">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">
                        <TextBlock x:Name="a" Text="{Binding XPath=@Name}" FontSize="12"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <Frame x:Name="PageContext" Grid.Row="0" Grid.Column="2"  Background="#F3F3F3" BorderBrush="#888" BorderThickness="1" NavigationUIVisibility="Visible"
               Source="{Binding ElementName=TreeRoot, Path=SelectedItem.Attributes[Value].Value}"/>
    </Grid>
</Window>
相關文章
相關標籤/搜索