Skip to content

Instantly share code, notes, and snippets.

@johannesegger
Last active August 29, 2015 14:10
Show Gist options
  • Save johannesegger/22a93f385d3bdf61942f to your computer and use it in GitHub Desktop.
Save johannesegger/22a93f385d3bdf61942f to your computer and use it in GitHub Desktop.
MahApps sample code for issue #1690
<Application x:Class="MahAppsTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Window x:Class="MahAppsTest.InfoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="InfoWindow" Height="300" Width="300">
<TextBox>asd</TextBox>
</Window>
<Window x:Class="MahAppsTest.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Button Click="OpenInfoWindow_OnClick">Open window</Button>
</Window>
using System.Threading;
using System.Windows;
namespace MahAppsTest
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OpenInfoWindow_OnClick(object sender, RoutedEventArgs e)
{
var t = new Thread(() =>
{
var window = new InfoWindow();
window.Show();
System.Windows.Threading.Dispatcher.Run();
}) { IsBackground = true };
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
}
@thoemmi
Copy link

thoemmi commented Dec 3, 2014

I guess your thread should look like this:

var t = new Thread(() =>
    {
    var window = new InfoWindow();
    window.Show();
    System.Windows.Threading.Dispatcher.Run();
}) { IsBackground = true };
t.SetApartmentState(ApartmentState.STA);
t.Start();

See Running WPF Application with Multiple UI Threads

@johannesegger
Copy link
Author

Thanks, I tried that, but still, using MahApps.Metro's ResourceDictionarys it fails. Anyway, I will update the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment