日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF整理-使用逻辑资源

發布時間:2025/3/20 asp.net 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF整理-使用逻辑资源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF整理-使用邏輯資源 原文:WPF整理-使用邏輯資源

"Traditional application resources consist of binary chunks of data, typically representing things such as icons, bitmaps, strings, and XML. In fact, the .NET framework provides
generic support for these through the ResourceManager class.
WPF is no different—binary resources play an important role in a typical application.
However, WPF goes a lot further with another kind of resource: logical resources.
These are objects, any objects, which can be shared and used in a multitude of locations
throughout the application; they can even be accessed across assemblies. Some
of WPF's features, such as implicit (automatic) styles and type-based data templates,
rely on the logical resources system."

傳統的資源一般是指大塊的二進制資源,典型的如圖片、xml文件等,.NET提供了ResourceManager類來管理這些資源。而WPF引進了另一種形式的資源-logical resources(邏輯資源),它可以是任何需要共享的對象。WPF的許多特性也都依賴邏輯資源系統。

These objects are typically placed inside a
ResourceDictionary and located at runtime using a hierarchical search.”

下面通過一個例子,說明為什么需要logical resource、如何在XAML中及Code behind file中訪問logical resource。

1.為什么需要Logical Resource?

假如我們需要為一個rectangle的Fill屬性賦一個LinearGradientBrush值。通常實現如下:

<Rectangle Height="100" Stroke="Red"><Rectangle.Fill><LinearGradientBrush ><GradientStop Offset="0.3" Color="Green"/><GradientStop Offset="0.8" Color="Brown"/></LinearGradientBrush></Rectangle.Fill></Rectangle>

假如,現在我們另外還有1個Rectangle需要同樣的Brush,當然我們可以Copy上面的<Rectangle.Fill>...</Rectangle.Fill>,這樣可以滿足需求,但是這樣的Copy很讓人頭疼。
Logical resource能夠解決這個問題。

2.定義logical resource

我們可以把上面的Brush放到Window.Resources中,如下:

<Window.Resources><LinearGradientBrush x:Key="brush1"><GradientStop Offset="0.3" Color="Green"/><GradientStop Offset="0.8" Color="Yellow"/></LinearGradientBrush> </Window.Resources>

注意以下2點:
a.“Every element (deriving from FrameworkElement) has a Resources property of type
ResourceDictionary. This means that every element can have resources associated with
it.”

每個element都有Resources屬性。

b.“The Resources property is a dictionary.In XAML, the x:Key attribute must be specified (most of the time; exceptions to this rule
will be discussed in relation to styles and data templates).”

在XAML中x:key必須聲明,style和data templates有些例外(x:Type)。

3.在XAML中使用

添加完成后,我們可以在XAML中通過StaticResource(后面介紹DynamicResource)這個markup extension,很方便的使用這個logical resource

<Rectangle Height="100" Fill="{StaticResource brush1}"/>

4.在Code Behind File中使用

如為下面的Ellipse賦Fill值

<Ellipse x:Name="ellipse2" Stroke="Red" StrokeThickness="20" Height="100" />

我們有兩種方法獲取這個Resource。

?方法a:通過FrameworkElement.FindResource?,此方法在找不到的時候放回null,因此最好加個null判斷。

Brush brush1 = (Brush)ellipse2.FindResource("brush1"); if (brush1 != null) {ellipse2.Fill = brush1; }

?方法b.是資源可以通過它的索引直接獲得。由于我們知道定義資源的是哪個element,我們可以直接使用element.Resources["name"]獲得。本例定義的是Window的資源,因此是:

Brush brush = (Brush)this.Resources["brush1"];

?5.Logical Resource是如何匹配的?

?如上面的

<Rectangle Height="100" Fill="{StaticResource brush1}"/>

?“This causes a search from the current element (the Rectangle) up the element tree, looking
for a resource with the key brush1; if found, its associated object is used as the property's
value. If not found on any element up to the root Window, the search continues within the
resources of Application (typically located in the App.xaml file). If not found even there,
a runtime exception is thrown. This is depicted in the following diagram:”

附:完整的XAML和Code Behind File如下:

<Window x:Class="UsingLogicalResources.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><LinearGradientBrush x:Key="brush1"><GradientStop Offset="0.3" Color="Green"/><GradientStop Offset="0.8" Color="Yellow"/></LinearGradientBrush> </Window.Resources><StackPanel><Rectangle Height="100" Stroke="Red"><Rectangle.Fill><LinearGradientBrush ><GradientStop Offset="0.3" Color="Green"/><GradientStop Offset="0.8" Color="Brown"/></LinearGradientBrush></Rectangle.Fill></Rectangle><Ellipse x:Name="ellipse2" Stroke="Red" StrokeThickness="20" Height="100" /><Rectangle Height="100" Fill="{StaticResource brush1}"/></StackPanel> </Window> View Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace UsingLogicalResources {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();//Brush brush1=(Brush)this.Resources["brush1"];Brush brush1 = (Brush)ellipse2.FindResource("brush1");if (brush1 != null){ellipse2.Fill = brush1;}}} } View Code

運行,如下:

?

?

posted on 2018-05-29 00:48 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/9103102.html

總結

以上是生活随笔為你收集整理的WPF整理-使用逻辑资源的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。