1
votes

I am using MahApps.Metro controls in my XAML code for the toolbox in Visual Studio Extension. I installed the package via NuGet, then I tried to add a control into my XAML markup. Below is the code snippet.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:custom="http://metro.mahapps.com/winfx/xaml/controls" 
    x:Class="AutoDebug.MyControl"
    Background="{DynamicResource VsBrush.Window}"
    Foreground="{DynamicResource VsBrush.WindowText}"
    mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="400"
    DataContext="{Binding UserControlModel}"
    x:Name="AutoDebugWindow">

    <Grid Margin="15">
        <custom:Tile Content="Tile" HorizontalAlignment="Left" Margin="75,150,0,0" VerticalAlignment="Top" Background="#FF8B00BF"/>
    </Grid>
</UserControl>

But I receive the following error no matter what.

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Could not load file or assembly 'MahApps.Metro, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I have already tried installing/uninstalling, deleting/adding references but nothing has worked so far.

1
Read the quickstart docs and you should be able to get it working : mahapps.com/guides/quick-start.html - Sybren
I have followed the steps. But it does not help. Also adding the code for reference. - Shahzad
@shahzad It seems that the Visual Studio Extension doesn't load the MahApps dll... - punker76
@punker76 Yes it seems so. But I am unable to figure out why - Shahzad
@shahzad- have resolved your issue. is yes then how pls. - J R B

1 Answers

3
votes

This is caused by the fact that MahApps.Metro is not included as a reference when the Visual Studio extension is compiled.

I'm not entirely sure why, but if you only use MahApps in XAML, then no reference is included in the compiled assembly. You can check this by unpackaging the extension (it's just a zip file), and opening the assembly in ILSpy. Under the references, MahApps will not be listed.

A workaround for this is to use MahApps somewhere in code. The simplest way to do this is to name the MahApps control that you are using. This generates a field for the control, and that seems to be enough to cause a reference to be included in the assembly.

<Grid Margin="15">
    <custom:Tile x:Name="MyTile" />
</Grid>

You can also use an object from the MahApps assembly anywhere else in code (for example, you could create a new object in the constructor of the Package), but giving one of the controls a name is probably the simplest way.