WPF published application crashing when changing theme - wpf

The application is working fine when i run it from Visual Studio but if i run my application when its published it crashes. This is the first application i have published so i dont know where to start.
I have 2 combo boxes that let you choose the theme color of the application as soon as i choose one it crashes.
This is using Mahapps Metro
The combobox's item source is a metro thememanager
ItemsSource="{x:Static metro:ThemeManager.Accents}"
This is the Selection Changed code
private void ColorsSelectorOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedColor = this.ColorsSelector.SelectedItem as KeyValuePair<string, Color>?;
if (selectedColor.HasValue)
{
var theme = ThemeManager.DetectAppStyle(Application.Current);
ThemeManagerHelper.CreateAppStyleBy(selectedColor.Value.Value, true);
Application.Current.MainWindow.Activate();
}
When creating a runtime accent resource dictionary:

Related

XAML Islands ProgressRing White Background

I'm porting a WPF app to WPF .NET Core 3.0. I'm trying to use UWP controls using XAML Islands via WindowsXamlHost from the Community Toolkit. The WindowsXamlHost control itself has a white background and I don't know how to get rid of it. Here's an example of a ProgressRing:
<xamlHost:WindowsXamlHost x:Name="MyRing" InitialTypeName="Windows.UI.Xaml.Controls.ProgressRing"/>
private void MyRing_ChildChanged(object sender, EventArgs e)
{
if (MyRing.Child is ProgressRing progressRing)
{
progressRing.IsActive = true;
var brush = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Transparent);
progressRing.Background = brush;
}
}
Giving the ProgressRing object a new background color works but doesn't help if I'm after transparency. I'm using .NET Core 3 preview 7.
I'm not sure whether it is possible. I have a similar issue which I reported here: https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/issues/160#issuecomment-522288462. Hopefully, Microsoft employees will answer there.

Open/Load view from another project of same solution in WPF

In my application, in a single solution, there are two different project.
Lets say, Project A and Project B.
From the view of first project A, on button click event, I want to load the view from the second project B.
Could you please guide me.
Thanks.
Ruhul
Add a reference to Project B from Project A in Visual Studio: Project->Add Reference->Projects->Solution.
Then you can create an instance of the view class that is defined in Project B and use it as appropriate:
private void btnClick_In_Project_A(object sender, RoutedEventArgs e)
{
//if viewB is a window:
ProjectB.ViewB viewB = new ProjectB.ViewB();
viewB.Show();
//or if viewB is a UserControl:
Window win = new Window();
win.Content = new ProjectB.ViewB();
win.Show();
}
Project B should be a WPF User Control Library or a WPF User Control Library.

WinForm progressbar not working when I open that form in Wpf application?

I created a Windows form project in Visual studio and used a progress bar with style as marquee and I created another wpf project and added windows project reference to the newly created wpf project and call the form in a button click event as
private void Button_Click(object sender, RoutedEventArgs e)
{
Form1 form = new Form1();
form.Show();
}
But the progress bar inside the form is not working.
I fixed the above issue by adding the following lines
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

Strange behavior when showing WPF window in Outlook

I use the following code to show my WPF window from the new message window in Outlook:
private void DisplayWindow(Window window) {
var wih = new System.Windows.Interop.WindowInteropHelper(window);
wih.Owner = GetForegroundWindow();
window.ShowInTaskbar = false;
window.ShowDialog();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
My problem is that when ToolTips and ComboBox drop downs become visible, the WPF window disappears behind the new message window, leaving just the "popup" content in front. Could anyone explain why this might be happening, and what the correct way to host the window might be?
EDIT:
This only happens once a recipient has been added to the Send box and seems to only be a problem when the foreground window is the new mail message window.
To replicate:
Add an Outlook Add-in project and a WPF project (targeting .NET 4.0) to a new solution.
Put a ComboBox with a few items in it on the MainWindow.xaml.
Remove the StartupUri from App.xaml and add the following to App.cs.
public void ShowWindow() {
MainWindow window = new MainWindow();
var wih = new System.Windows.Interop.WindowInteropHelper(window);
wih.Owner = GetForegroundWindow();
window.ShowInTaskbar = false;
window.ShowDialog();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
Add references to WindowsBase, System.Xaml and PresentationFramework to the Outlook project.
Add a Ribbon (XML) to the Outlook project with the following in the .xml.
<customUI ...>
<ribbon>
<tabs>
<tab idMso="TabNewMailMessage">
<group id="MyGroup"
insertAfterMso="GroupMailNew">
<button id="myButton"
size="large"
onAction="myButton_Action"
imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Add the following to the ribbon code.
MyWpfApplication.App app;
public void Ribbon_Load(Office.IRibbonUI ribbonUI) {
this.ribbon = ribbonUI;
var appThread = new Thread(new ThreadStart(() => {
this.app = new MyWpfApplication.App();
app.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
app.Run();
}));
appThread.SetApartmentState(ApartmentState.STA);
appThread.Start();
}
public void myButton_Action(Office.IRibbonControl control) {
// Dispatcher used as cross thread operation.
app.Dispatcher.BeginInvoke((Action)(() => {
app.ShowWindow();
}));
}
Add the following to ThisAddIn
protected override Microsoft.Office.Core
.IRibbonExtensibility CreateRibbonExtensibilityObject() {
return new Ribbon();
}
Run the Outlook add-in, create a new message, add a recipient and click on the smiley face button. You will see the bug when you click on the ComboBox.
Perhaps you are encountering the infamous "airspace" issue. See here, here and here. There were high hopes that it would be fixed in .NET 4.5, but sadly these were dashed when MS announced that the fix itself was too buggy to release.
It may be the same issue described here:
The WPF dialog box disappears when it displays a tooltip or drop-down combo box in Windows 8 or Windows Server 2012
Source codes of .NET Framework 4.7.1 have comment in System.Windows.FrameworkCompatibilityPreferences class, saying there is a bug in the Windows desktop window manager which can cause incorrect z-order for windows on some conditions.
The suggested workaround is to add the following code to the WPF application’s app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="UseSetWindowPosForTopmostWindows" value="True" />
</appSettings>
</configuration>

WPF ComboBox doesn't stay open when used in a Task Pane

I have a strange bug with WPF Interop and an Excel Addin. I'm using .Net 3.5 SP1.
I'm using Add-in Express to create a Custom Task Pane for Excel 2003. Within that taskpane I'm using ElementHost to host a WPF UserControl. The UserControl simply contains a Grid with a TextBox and ComboBox. My problem is that whilst everything displays properly, the ComboBox won't stay dropped-down unless I hold the mouse down over the down-arrow.
I don't believe this is necessarily related to Add-in Express because I've had a similar problem when I tried displaying a WPF window modelessly in Excel.
A second problem is that the ComboBox seems reluctant to give up focus. If I click it, the text area goes grey to indicate that it has focus, but I can't move focus anywhere else in the window. The only way to wrest focus away is to move the mousewheel.
Anybody else had a similar problem, and managed to fix it?
Add-in Express looked into this for me, and it turns out to have something to do with the Window style of the Task Pane that gets added to Excel. If you turn off the WS_CHILD flag in the Windows CreateParams then Combo Boxes and other popups work as expected.
They gave me this snippet of code to add to my ADXExcelTaskPane:
private const uint WS_CHILD = 0x40000000;
private const uint WS_CLIPCHILDREN = 0x02000000;
private const uint WS_CLIPSIBLINGS = 0x04000000;
private CreateParams _CreateParams = new CreateParams();
protected override CreateParams CreateParams
{
get
{
_CreateParams = base.CreateParams;
if (!DesignMode)
_CreateParams.Style = (int)(WS_CLIPCHILDREN | WS_CLIPSIBLINGS); //| WS_CHILD
return _CreateParams;
}
}
I had the same problem. I have a WPF user control hosted in a WinForm user control and the whole is an Excel AddIn. I work with Visual Studio 2010 and Excel 2007 and Excel 2010.
My problem was that when I clicked once in the Excel sheet, the AddIn never gains focus again.
I found a workaround.
In the constructor of my WinForm user control, I register on the event MouseEnter of my WPF user control.
In the MouseEnter event handler, I give the focus to myself (this.Focus())
public WpfContainerUserControl()
{
InitializeComponent();
GpecsBrowserTabUserControl gpecBrowser = elementHost1.Child as GpecsBrowserTabUserControl;
gpecBrowser.MouseEnter += new System.Windows.Input.MouseEventHandler(gpecBrowser_MouseEnter);
}
void gpecBrowser_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
this.Focus();
}

Resources