2

I'm making an AutoCAD .net program that has a WPF window as the interface. Currently the WPF interface is being referenced into the AutoCAD .net aplication and I'm calling the window from AutoCAD as follows.

public class Class1
{
    public static WPFWindow.MainWindow mainWindow = new WPFWindow.MainWindow();

    [CommandMethod("Launch", CommandFlags.Session)]

    public void Launch()
    {
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalWindow(mainWindow);

    }
}

This works fine until I start adding any form of resource to the WPF window I'm adding in. eg The following works until

<Window x:Class="WPFWindow.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:WPFWindow" 
    mc:Ignorable="d"
    Title="Test" Height="450" Width="800"
    WindowStyle="None"
    AllowsTransparency="True"
    >
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="Themes/Styles.xaml"/>

        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>

</Window.Resources>

<Grid>
    <Button Content="Press Me"/>

</Grid>

....I reference a static resource style for the window

WindowStyle="None"
    AllowsTransparency="True"
   Style="{StaticResource MainWindow}"
    >

With the static resource when I run the "Launch" command in AutoCAD the program fails to find the static resource. I'm unsure how to get the instance of the WPFWindow to find the resource using C# code. As a test I added the WPFWindow as a reference to a WPF application and managed to get it to find the resource using the Pack URI

    <ResourceDictionary Source="pack://application:,,,/WPFWindow;component/Themes/Styles.xaml"/>

Is there a C# equivalent of that I can use for the instance of the WPFWindow.MainWindow?

Tim
  • 173
  • 1
  • 11

2 Answers2

1

I managed to get it to work by adding the resources to the window I was referencing in its code behind files.

namespace WPFWindow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.Resources.Source = new Uri(@"pack://application:,,,/WPFWindow;component/Themes/Styles.xaml"", UriKind.Absolute);
        InitializeComponent();
    }
}

}

I think this allowed the static resources to be loaded in before they were called for the window.

Tim
  • 173
  • 1
  • 11
0

You cant use Staticresource in the root tag for external Resourcedictionaries. At time of Initialization the resource is not present. Linking it in ctor before calling InitializeComponent (as you did) does actually the same...

...
WindowStyle="None"
AllowsTransparency="True"
Style="{DynamicResource MainWindow}"
...

will work.

dba
  • 1,159
  • 1
  • 14
  • 22