最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解WPF中的對(duì)象資源

 更新時(shí)間:2021年04月09日 09:56:16   作者:杜文龍  
這篇文章主要介紹了WPF中對(duì)象資源的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用WPF,感興趣的朋友可以了解下

  在WPF中,所有繼承自FrameworkElement的元素都包含一個(gè)Resources屬性,這個(gè)屬性就是我們這篇要講的資源。

  這一篇講解的資源是不是上一篇的程序集資源(那個(gè)是在編譯過(guò)程中打包到程序集中),這個(gè)是資源是我們想在公共的地方寫(xiě)一個(gè)對(duì)象讓其他元素重復(fù)使用。

  先貼個(gè)例子:

<Window x:Class="NETResource.MainWindow"
        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"
        xmlns:local="clr-namespace:NETResource"0
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="RedBrushButtonBackground" Color="Red" />
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <SolidColorBrush x:Key="BlueBrushButtonBackground" Color="Blue"/>
        </Grid.Resources>
        <StackPanel>
            <Button Width="120" Background="{StaticResource RedBrushButtonBackground}" Content="我是按鈕A"/>
            <Button Width="120" Background="{StaticResource BlueBrushButtonBackground}" Content="我是按鈕B"/>
        </StackPanel>
    </Grid>
</Window>

顯示效果:

從例子中我們看幾個(gè)資源的關(guān)鍵點(diǎn),我們?cè)賅indow元素和Grid元素下添加兩個(gè)顏色畫(huà)刷資源,使用x:key標(biāo)識(shí)。上面2個(gè)Button都使用了2個(gè)不同層級(jí)的父元素資源。WPF中有個(gè)設(shè)計(jì)比較好的地方就是元素可以使用父元素的資源。這樣我們都把資源放在Window節(jié)點(diǎn)下,公用這些資源。

資源定義好之后,再使用時(shí),可以指定以靜態(tài)的方式使用資源,還是以動(dòng)態(tài)的方式使用資源。

差別就是靜態(tài)資源只從資源集合獲取對(duì)象一次,對(duì)象的任何變化都會(huì)得到消息,而動(dòng)態(tài)資源每次需要用到對(duì)象時(shí)都會(huì)重新從資源集合中查找對(duì)象。注意動(dòng)態(tài)資源是每次需要用到對(duì)象時(shí)才會(huì)去資源集合查找,所以在資源非常大,且非常復(fù)雜的時(shí)候,可以用動(dòng)態(tài)資源的方式可以提高第一次加載窗口的速度(因?yàn)闆](méi)有解析資源標(biāo)記的過(guò)程),其他任何使用都不建議使用動(dòng)態(tài)資源。使用數(shù)據(jù)綁定去實(shí)現(xiàn)。

<Window x:Class="NETResource.Window1"
        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"
        xmlns:local="clr-namespace:NETResource"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="ButtonBrushBackground" Color="DarkBlue"/>
    </Window.Resources>
    <Grid> 
        <StackPanel Width="120">
            <Button Content="按鈕A靜態(tài)資源" Background="{StaticResource ButtonBrushBackground}"/>
            <Button Content="按鈕B動(dòng)態(tài)資源" Background="{DynamicResource ButtonBrushBackground}"/>
            <Button Content="點(diǎn)擊切換資源對(duì)象" Click="SetButtonBackgroudButton_OnClicked"/>
            <Button Content="點(diǎn)擊修改資源的值" Click="UpdataButtonBackgroundButton_OnClicked"/>
        </StackPanel>
    </Grid>
</Window>
using System.Windows;
using System.Windows.Media;

namespace NETResource
{
    /// <summary>
    /// Window1.xaml 的交互邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void SetButtonBackgroudButton_OnClicked(object sender, RoutedEventArgs e)
        {
            //修改資源指向的對(duì)象。只有動(dòng)態(tài)資源會(huì)受到影響,因?yàn)閯?dòng)態(tài)資源每次使用值的時(shí)候,都會(huì)重新讀取。靜態(tài)資源不會(huì),所以靜態(tài)資源不受影響。
            //修改樣式1
            SolidColorBrush brush = new SolidColorBrush(Colors.Red);
            brush.Opacity = 0.3;
            this.Resources["ButtonBrushBackground"] = brush;
            //修改樣式2
            // LinearGradientBrush linear = new LinearGradientBrush(Colors.Red, Colors.Blue, 90.0);
            // this.Resources["ButtonBrushBackground"] = linear;
        }

        private void UpdataButtonBackgroundButton_OnClicked(object sender, RoutedEventArgs e)
        {
            //修改資源對(duì)象的值,資源沒(méi)有改變,只是改變了資源的值,所以2個(gè)按鈕都受到影響。
            var brush = this.Resources["ButtonBrushBackground"];
            if (brush is SolidColorBrush solidColorBrush)
            {
                solidColorBrush.Color = Colors.LightBlue;
            }
        }
    }
}

如果有些資源是所有窗體都共享的,建議寫(xiě)在Application的.Resources下。

<Application x:Class="NETResource.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NETResource"
             StartupUri="Window1.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="TitleBrush" Color="Red"/>
    </Application.Resources>
</Application>
<Window x:Class="NETResource.Window1"
        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"
        xmlns:local="clr-namespace:NETResource"
        mc:Ignorable="d" 
        Title="WPF教程九:理解WPF中的資源"  
        Height="450" Width="800">
    
    <Window.Resources>
        <SolidColorBrush x:Key="ButtonBrushBackground" Color="DarkBlue"/>
    </Window.Resources>
    <Grid>
        <TextBlock Text="WPF教程九:理解WPF中的資源"  Foreground="{StaticResource TitleBrush}"/>    
    </Grid>
</Window>

資源我們都會(huì)使用了,接下來(lái)需要?dú)w類(lèi)整理我們的資源,使用資源字典:

我們?cè)诠こ躺嫌益I=》添加=》資源字典=》設(shè)置名字為AppBrushes.xaml=》保存

添加代碼如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:NETResource">
    <SolidColorBrush x:Key="DictionaryTitleBrush" Color="Beige"/>
</ResourceDictionary>

在App.xaml中添加對(duì)資源字典的使用:

<Application x:Class="NETResource.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NETResource"
             StartupUri="Window1.xaml">
    <Application.Resources> 
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="AppBrushes.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <SolidColorBrush x:Key="TitleBrush" Color="Red"/>
        </ResourceDictionary>  
    </Application.Resources>
</Application>

這樣資源字典就可以被訪問(wèn)了。

我們創(chuàng)建一個(gè)button按鈕使用資源字典內(nèi)的樣式

  <Button Content="使用資源字典下的畫(huà)刷" Background="{StaticResource DictionaryTitleBrush}"/>

跨程序集使用資源:這個(gè)對(duì)象資源跨程序集使用。

一定要分清楚,什么是二進(jìn)制資源(程序集資源持久化),什么是對(duì)象資源(公共部分重復(fù)使用的對(duì)象)。我們手動(dòng)創(chuàng)建引用其他庫(kù)的資源字典。

 在新建資源DLL的時(shí)候,我沒(méi)有找到直接新建添加引用之后的類(lèi)庫(kù),所以我用以下2種方法種的一種來(lái)創(chuàng)建程序集,我使用的是第一種:

1)創(chuàng)建WPF程序,然后刪除他下面的App.config、App.xaml、MainWindow.xaml 和Properties下的Rsources.resx、Settings.settings,工程右鍵=》屬性=》應(yīng)用程序=》輸出類(lèi)型=》類(lèi)庫(kù)。用于保留自動(dòng)對(duì)PresentationCore、PresentationFramlework、WindowsBase的引用。

2)添加類(lèi)庫(kù)程序,然后添加=》引用=》PresentationCore、PresentationFramlework、WindowsBase。這三個(gè)的引用。

添加DLL完畢后,在窗體程序中添加對(duì)DLL庫(kù)的引用。整個(gè)目錄引用關(guān)系結(jié)構(gòu)如下: 

現(xiàn)在開(kāi)始寫(xiě)代碼,

首先是ResourceLibrary庫(kù)。這個(gè)是我們的資源庫(kù),里面存放我們的資源文件。目前是一個(gè)xaml的資源字典。需要我們新建出來(lái)。

代碼如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ResourceLibrary">
    <SolidColorBrush x:Key="ReusableTitle" Color="Yellow"/>
</ResourceDictionary>

而后是引用的App,在App.xaml下添加跨程序集的資源字典(使用pack: URI): 

<Application x:Class="WPFUsingResourceLib.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFUsingResourceLib"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ResourceLibrary;component/ReusableDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在Window窗體中使用以添加引用的資源:

<Window x:Class="WPFUsingResourceLib.MainWindow"
        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"
        xmlns:local="clr-namespace:WPFUsingResourceLib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid> 
        <Button  VerticalAlignment="Top" HorizontalAlignment="Left" Width="180" Height="30" Content="我是跨程序集使用資源" Background="{StaticResource  ReusableTitle}"/>         
    </Grid>
</Window>

效果圖如下:

好啦,這一篇就寫(xiě)這么多把。主要是就是對(duì)象資源的使用,靜態(tài)和動(dòng)態(tài)資源的差別,跨程序集資源,元素可以使用父類(lèi)資源。這篇主要是理解就行,后面會(huì)寫(xiě)軟件,用于演示如何更好的使用這些內(nèi)容。

以上就是詳解WPF中的對(duì)象資源的詳細(xì)內(nèi)容,更多關(guān)于WPF 對(duì)象資源的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#使用linq語(yǔ)句查詢(xún)數(shù)組中以特定字符開(kāi)頭元素的方法

    C#使用linq語(yǔ)句查詢(xún)數(shù)組中以特定字符開(kāi)頭元素的方法

    這篇文章主要介紹了C#使用linq語(yǔ)句查詢(xún)數(shù)組中以特定字符開(kāi)頭元素的方法,涉及C#使用linq進(jìn)行查詢(xún)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C# Mqtt 斷線重連的實(shí)現(xiàn)代碼

    C# Mqtt 斷線重連的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C# Mqtt 斷線重連,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • C#實(shí)現(xiàn)文件篩選讀取并翻譯的自動(dòng)化工具

    C#實(shí)現(xiàn)文件篩選讀取并翻譯的自動(dòng)化工具

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)文件篩選及讀取內(nèi)容,并翻譯的自動(dòng)化工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • C#操作RabbitMQ的完整實(shí)例

    C#操作RabbitMQ的完整實(shí)例

    這篇文章主要為大家詳細(xì)介紹了C#操作RabbitMQ的完整實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#裝箱和拆箱原理詳解

    C#裝箱和拆箱原理詳解

    這篇文章通過(guò)圖例主要介紹了C#裝箱和拆箱原理,內(nèi)容很簡(jiǎn)單,感興趣的小伙伴們可以參考一下
    2015-10-10
  • C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并)

    C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并)

    C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并),需要的朋友可以參考一下
    2013-04-04
  • C#處理猜拳問(wèn)題的簡(jiǎn)單實(shí)例(非窗體)

    C#處理猜拳問(wèn)題的簡(jiǎn)單實(shí)例(非窗體)

    下面小編就為大家?guī)?lái)一篇C#處理猜拳問(wèn)題的簡(jiǎn)單實(shí)例(非窗體)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • 深入淺析C#泛型類(lèi)型

    深入淺析C#泛型類(lèi)型

    這篇文章主要介紹C#泛型類(lèi)型,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • 基于WPF實(shí)現(xiàn)繪制地鐵路線圖

    基于WPF實(shí)現(xiàn)繪制地鐵路線圖

    經(jīng)常坐地鐵,卻不知道地鐵多少條線路?哪個(gè)站下車(chē)?本文就來(lái)帶大家利用WPF繪制深圳地鐵路線圖,從而帶大家掌握WPF在圖形繪制方面的一些知識(shí),希望對(duì)大家有所幫助
    2023-06-06
  • c# 實(shí)現(xiàn)RSA非對(duì)稱(chēng)加密算法

    c# 實(shí)現(xiàn)RSA非對(duì)稱(chēng)加密算法

    RSA解決了對(duì)稱(chēng)加密的一個(gè)不足,比如AES算法加密和解密時(shí)使用的是同一個(gè)秘鑰,因此這個(gè)秘鑰不能公開(kāi),因此對(duì)于需要公開(kāi)秘鑰的場(chǎng)合,我們需要在加密和解密過(guò)程中使用不同的秘鑰,加密使用的公鑰可以公開(kāi),解密使用的私鑰要保密,這就是非對(duì)稱(chēng)加密的好處?!?/div> 2021-06-06

最新評(píng)論

屯留县| 玉门市| 麦盖提县| 连城县| 寿光市| 噶尔县| 长沙市| 滨海县| 洛阳市| 白沙| 双江| 石狮市| 保亭| 连山| 龙江县| 宜川县| 石楼县| 西宁市| 大理市| 临安市| 普陀区| 高台县| 东平县| 宜丰县| 咸宁市| 原阳县| 方山县| 莎车县| 瓦房店市| 清镇市| 郑州市| 贡山| 从化市| 泊头市| 株洲县| 苏州市| 曲麻莱县| 诏安县| 湟中县| 泰顺县| 桐柏县|