App.xaml for Styles

App.xaml for Styles

dnzmaral
Participant Participant
382 Views
4 Replies
Message 1 of 5

App.xaml for Styles

dnzmaral
Participant
Participant

Hello everyone,

 

when building a WPF-Application you can add all styles to the App.xaml and they are shown in entire children windows. How can I do something similar, when I create an add-in for Revit? For now I have to import all styles in all windows and it is not that efficient.

 

Thank you very much!

Accepted solutions (2)
383 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

This came up for me recently, the issue is that by creating a class library you shouldn't have an application. You can still have an Application.xaml with merged dictionaries but that only half worked for me: certain resources from one dictionary were found but a control template for the window wasn't even though the designer found it.

 

In the end I did it programmatically by created a static method that merged the resource dictionary  in the ctor of the window. The downside of this is at design time I have to imagine what it looks like. Although I can template a window to take on the appearance for that purpose.

 

There is this property called Application.ResourceAssembly that can only be set once. 

 

Public Shared Sub Initialise(W As Window)

        Dim TW As Boolean
        If W.WindowStyle = WindowStyle.ToolWindow Then
            TW = True
        End If

        W.AllowsTransparency = True
        W.WindowStyle = WindowStyle.None
        If Application.ResourceAssembly Is Nothing Then
            Application.ResourceAssembly = System.Reflection.Assembly.GetExecutingAssembly
        End If
        Dim RD As New ResourceDictionary
        RD.Source = New Uri("pack://application:,,,/Resources/CustomWindow.xaml", UriKind.RelativeOrAbsolute)
        W.Resources.MergedDictionaries.Add(RD)


        Dim Sty As Style = Nothing

        If TW Then
            Sty = RD.Item("RPT_CustomToolWin")
        Else
            Sty = RD.Item("RPT_CustomWin")
        End If

        W.Style = Sty
    End Sub

 

I think generally we shouldn't have that many windows for an add-in (how many corridors do you want to walk down to get to a room).

Message 3 of 5

dnzmaral
Participant
Participant

Thank you very much for idea and your answer!

 

I think generally we shouldn't have that many windows for an add-in (how many corridors do you want to walk down to get to a room).


We have for each tool another window. You mean, it would be better to create a UserControl for each tool and then use in the window?

Message 4 of 5

Kennan.Chen
Advocate
Advocate
Accepted solution

I created a BaseWindow class to initialize all the necessary resources. All of my windows inherit BaseWindow class to gain the same appearance and behavior.

Message 5 of 5

dnzmaral
Participant
Participant

Thank you very much! It sounds nice! I think, I will have to do something like that.

0 Likes