Component user Control reference style resource (ii)---code reference

Source: Internet
Author: User

There are some drawbacks to the way the style file is referenced (excerpt from the official)

When you reference a ResourceDictionary in XAML, a ResourceDictionary object was created each time you reference it. So if you have a custom controls in your library and merge the shared resourcedictionaries for each control by using XAML  , you create ten identical ResourceDictionary objects. You can avoid this by creating a static class that returns the ResourceDictionary and merging the resources in code.

Therefore, we can circumvent this problem by the following way

1. Create a resource folder in the base Resource class library resources, create a dictionary resource style in resources Styles.xaml

<lineargradientbrush x:key= "NormalBrush" startpoint= "0,0" endpoint= "0,1" >
<GradientBrush.GradientStops>
<GradientStopCollection>
<gradientstop color= "#Red" offset= "0.0"/>
<gradientstop color= "#Blue" offset= "1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>

2. Creating a class in resources

public static Class Shareddictionarystylemanager
{
Static ResourceDictionary _sharedstyledictionary;
public static ResourceDictionary Sharedstyledictionary
{
Get
{
if (_sharedstyledictionary = = null)
{
System.Uri resourcelocater = new System.Uri ("/myglobal.infrustructure;component/resources/styles.xaml", System.UriKind.Relative);
_sharedstyledictionary = (ResourceDictionary) application.loadcomponent (resourcelocater);
}
return _sharedstyledictionary;
}
}
}

3. Create a user control Upjappmenuview.xaml, access the load style before the constructor

Public Upjappmenuview ()
{
Referencing global singleton style code ******************************
This. RESOURCES.MERGEDDICTIONARIES.ADD (shareddictionarystylemanager.sharedstyledictionary);
InitializeComponent ();
}

4. The user control XAML can reference the

<border width= "Auto" height= "Auto" borderbrush= "Blue" borderthickness= "ten" cornerradius= "ten" >
<rectangle stroke= "Black" strokethickness= "2" fill="{StaticResource NormalBrush}"></rectangle >

</Border>

Component user Control reference style resource (ii)---code reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.