Click here to Skip to main content
15,349,767 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: hosting problem from local host? Pin
Piyush Vardhan Singh19-Apr-09 21:09
MemberPiyush Vardhan Singh19-Apr-09 21:09 
GeneralRe: hosting problem from local host? Pin
Mark Salsbery20-Apr-09 5:46
MemberMark Salsbery20-Apr-09 5:46 
QuestionTreeview in expander Pin
Ravi Mori16-Apr-09 20:01
MemberRavi Mori16-Apr-09 20:01 
AnswerRe: Treeview in expander Pin
pc.rajesh.singh17-Apr-09 5:03
Memberpc.rajesh.singh17-Apr-09 5:03 
QuestionResource dls Pin
Christian Graus16-Apr-09 15:56
mveChristian Graus16-Apr-09 15:56 
AnswerRe: Resource dls Pin
Eslam Afifi16-Apr-09 17:06
MemberEslam Afifi16-Apr-09 17:06 
GeneralRe: Resource dls Pin
Christian Graus16-Apr-09 19:55
mveChristian Graus16-Apr-09 19:55 
AnswerRe: Resource dls [modified] Pin
Eslam Afifi17-Apr-09 2:20
MemberEslam Afifi17-Apr-09 2:20 
I have managed to do it in XAML.
In the class library, add a XAML file (or drag and drop from an existing WPF project). Don't forget to set its Build Action to Page.
<ResourceDictionary x:Class="Resource1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="img" UriSource="Resources\Image1.png" />
    <SolidColorBrush x:Key="greenBrush" Color="Green" />
</ResourceDictionary>

And add a class in the library, the code is similar to the one found in App.g.cs
public partial class Resource1 : ResourceDictionary
{
    private bool _contentLoaded;

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public void InitializeComponent()
    {
        if (_contentLoaded)
            return;

        _contentLoaded = true;

        System.Uri resourceLocater = new System.Uri("ClassLibrary1;component/Dictionary1.xaml", System.UriKind.Relative);
        System.Windows.Application.LoadComponent(this, resourceLocater);
    }

    public Resource1()
    {
        InitializeComponent();
    }
}

Add a reference to the library in the WPF project, and add the resource dictionary anywhere (for example Application.Resources. Don't forget to add the XML namespace.
<Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:lib="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"

    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <lib:Resource1 />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Now you can use it in XAML just like any resource dictionary.
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Background="{StaticResource greenBrush}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Image Source="{StaticResource img}" />
    </Grid>
</Window>

And also access them from code,
var brush = this.TryFindResource("greenBrush");
var imgWPFStyle = Application.Current.TryFindResource("img");

And I agree with you that the InitializeComponent code should be automatically generated by visual studio. But until they add that feature, it's not hard to copy and paste that code snippet.
And you're welcome.

Eslam Afifi

modified on Friday, April 17, 2009 3:53 PM

AnswerRe: Resource dls Pin
Oleg V. Polikarpotchkin16-Apr-09 17:41
MemberOleg V. Polikarpotchkin16-Apr-09 17:41 
GeneralRe: Resource dls Pin
Christian Graus16-Apr-09 19:54
mveChristian Graus16-Apr-09 19:54 
GeneralRe: Resource dls Pin
Oleg V. Polikarpotchkin16-Apr-09 23:13
MemberOleg V. Polikarpotchkin16-Apr-09 23:13 
AnswerRe: Resource dls Pin
Mark Salsbery17-Apr-09 9:02
MemberMark Salsbery17-Apr-09 9:02 
QuestionHow bind 3 level master-detail scenario into datagrid? Pin
enochenoch2k16-Apr-09 14:51
Memberenochenoch2k16-Apr-09 14:51 
QuestionWCF Issue Pin
Tauseef A16-Apr-09 8:31
MemberTauseef A16-Apr-09 8:31 
AnswerRe: WCF Issue Pin
Wes Aday16-Apr-09 9:23
professionalWes Aday16-Apr-09 9:23 
QuestionRe: WCF Issue Pin
Tauseef A16-Apr-09 11:06
MemberTauseef A16-Apr-09 11:06 
AnswerRe: WCF Issue Pin
Wes Aday16-Apr-09 12:08
professionalWes Aday16-Apr-09 12:08 
QuestionRe: WCF Issue Pin
Tauseef A16-Apr-09 14:30
MemberTauseef A16-Apr-09 14:30 
AnswerRe: WCF Issue Pin
Wes Aday17-Apr-09 3:18
professionalWes Aday17-Apr-09 3:18 
AnswerRe: WCF Issue Pin
Pete O'Hanlon16-Apr-09 12:18
mvaPete O'Hanlon16-Apr-09 12:18 
QuestionRe: WCF Issue Pin
Tauseef A16-Apr-09 14:32
MemberTauseef A16-Apr-09 14:32 
Questiondata contract issu Pin
Tauseef A16-Apr-09 4:26
MemberTauseef A16-Apr-09 4:26 
Questionsilverlight made website slow very much Pin
Gaurav Mahajan16-Apr-09 1:32
MemberGaurav Mahajan16-Apr-09 1:32 
AnswerRe: silverlight made website slow very much Pin
Mark Salsbery16-Apr-09 9:46
MemberMark Salsbery16-Apr-09 9:46 
GeneralRe: silverlight made website slow very much Pin
Gaurav Mahajan16-Apr-09 16:44
MemberGaurav Mahajan16-Apr-09 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.