How to enable window chrome by using C# code and not XAML? - c#

So I am using the WPFShell to apply chrome to a custom window. I have learned from this article that in order to use it, I have to reference the Microsoft.Windows.Shell library and use this XAML code:
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
ResizeBorderThickness="6"
CaptionHeight="43"
CornerRadius="25,25,10,10"
GlassFrameThickness="0">
</shell:WindowChrome>
</shell:WindowChrome.WindowChrome>
My question is, how do I enable chrome by using C# code and not XAML? (i.e. How do I apply chrome in code-behind?)

Ah, stupid me. It was easy:
WindowChrome.SetWindowChrome(this, new WindowChrome());

I know that this is a older question, But I noticed that I couldn't get WindowChrome.GetWindowChrome() to work in .NET 4.5. I'm not sure if this has to do with System.Windows.Shell being included within the PresentationFramework assembly. But since it kept returning null there would be no way to update the chrome.
So my solution was to add a 'Name' to the WindowChrome which made it accessible in Code Behind.
XAML:
<Window x:Class="SomeProject.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="Some Window" WindowStyle="None" ResizeMode="CanResize"
AllowsTransparency="True">
<WindowChrome.WindowChrome>
<WindowChrome x:Name="chrome" ResizeBorderThickness="6" CaptionHeight="0"
GlassFrameThickness="0" CornerRadius="0" UseAeroCaptionButtons="False"/>
</WindowChrome.WindowChrome>
</window>
Code Behind:
using System;
using System.Window;
namespace SomeProject
{
public partial class MainWindow: Window
{
public MainWindow()
{
//Get Existing 'WindowChrome' Properties.
var captionHeight = chrome.CaptionHeight;
//Set Existing 'WindowChrome' Properties.
chrome.GlassFrameThickness = new Thickness(2d);
//Assign a New 'WindowChrome'.
chrome = new System.Windows.Shell.WindowChrome();
}
}
}
I hope this helps someone who needs it.

Related

Webview2 WPF Windows 10 - Custom Source URL with parameters, possible?

I spent the night trying to solve this problem but couldn't unfortunately. Please show mercy, since I am not proficient in C#.
My MainWindow.xaml is pretty much from the MS support pages:
<Window x:Class="TrayApp.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:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:r="clr-namespace:System;assembly=mscorlib"
xmlns:local="using:clr-namespace:TrayApp"
Title="MainWindow" Height="825" Width="500"
Deactivated="Window_Deactivated"
ShowInTaskbar="False"
WindowStyle="None">
<DockPanel>
<wv2:WebView2 Name="webView"
Source = "https://website.com" />
</DockPanel>
</Window>
My goal is, whenever the webview2 control is launched the URL has to be dynamically changed to https://website.com/?param=hostname
I know that its possible to get hostname of a PC via Environment.GetEnvironmentVariable("COMPUTERNAME") however, I tried very many variations data bindings and other tricks, but nothing seems to work. Hopefully someone someone can help.
UPDATE:
Thanks to Klaus Gütters suggestion, it made click in my head and it works as intended now!
My MainWindow.xaml.cs looks like this:
public MainWindow()
{
InitializeComponent();
this.Hide_Window();
string host = Environment.GetEnvironmentVariable("COMPUTERNAME");
webView.Source = new Uri("https://website.com/?param=" + host);
And my MainWindow.xaml like this:
<wv2:WebView2 x:Name="webView"
Source = ""

XAML designer doesn't show Custom Window

I have a WPF window that is inherited from custom window. It runs correctly but VS designer shows it like default empty window instead.
Custom window code:
using System.Windows;
using System.Windows.Media;
namespace WpfApplication2
{
public class CustomWindow : Window
{
public CustomWindow()
{
Background = new SolidColorBrush(Colors.Red);
}
}
}
MainWindow.xaml:
<local:CustomWindow x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
</local:CustomWindow>
Designer:
Expected result:
The XAML designer in both Visual Studio and Blend doesn't necessarily respect any dependency properties that are set directly in code (especially in the constructor), but rather expects those to be set in XAML only, which is the canonical way of working with the dependency system at design time. Of course all things can be set in code if you want, but when using the XAML designer you cannot guarantee the same behaviour as you will see in a running application.
Also, if you are setting dependency properties in code, then you need to be very careful about how you set them, as you may destroy any bindings that may end up being set on those properties.
Anyway, a better way of getting the result you want is to set the color in the XAML like this
<local:CustomWindow x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525" Background="Red">
</local:CustomWindow>

Embed Firefox/Gecko in WPF/C#

I want to embed the current Gecko in my WPF-Project.
I know there is the possibility with the Winforms-Host and the Skybound-Gecko-Library.
But I do not use the standard wpf-theme for my application. It is another and the scrollbar of the control will not be styled. Furthermore, this is an old library which is designed for Firefox 3.
Which is the best library/strategy to use the current Gecko in WPF?
You should have a look at these options, they all use Chromium:
paid: (Awesomium-based)
http://awesomium.com/ (is free for startups)
http://wpfchromium4.codeplex.com/ (uses awesomium)
free: (Chrome Embedded Framework-based)
https://github.com/chillitom/CefSharp (provides WinForms and WPF, but uses CEF1)
https://bitbucket.org/xilium/xilium.cefglue/wiki/Home (uses CEF3, and therefore supports Chrome's multi-process model, flash plugin, and WebGL)
You can probably use WindowsFormsHost, tutorial here
https://nhabuiduc.wordpress.com/2014/09/18/geckofx-net-webbrowser-setup-and-features/
the interesting part is
WindowsFormsHost host = new WindowsFormsHost();
GeckoWebBrowser browser = new GeckoWebBrowser();
host.Child = browser;
gridWeb.Children.Add(host);
WebKit.Net is free: http://sourceforge.net/projects/webkitdotnet/
Their GitHub page seems to have been more recently updated: https://github.com/webkitdotnet
Here is my answer. As stated by Roman, Gecko is Winforms-based, not WPF-based and so has to be incorporated via the WindowsFormsHost.
After creating the Visual Studio project, install the Gecko package via NuGet, using the command: Install-Package Geckofx45
Make sure the WindowsFormsIntegration and System.Windows.Forms references have been added to your project.
In your Configuration Manager, set your configuration to 32-bit, to get rid of the compiler warnings.
Update MainWindow.xaml 'Grid' element to give it a name and the handler for the 'Loaded' event
<Grid
Name="GridWeb"
Loaded="Window_Loaded">
</Grid>
Modify MainWindow.xaml.cs to incorporate the Gecko as well as make it navigate to a page on loading:
public MainWindow()
{
InitializeComponent();
Gecko.Xpcom.Initialize("Firefox");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowsFormsHost host = new WindowsFormsHost();
GeckoWebBrowser browser = new GeckoWebBrowser();
host.Child = browser;
GridWeb.Children.Add(host);
browser.Navigate("http://www.google.com");
}
I struggle using the SO code editor, so for more detailed explanations and screenshots, see this blog page.
This is an old question, but I came up with a pseudo-solution to add GeckoFX as a XAML tag such as:
<local:GeckoBrowser Width="400" Height="250" />
This can be accomplished by simply wrapping the whole thing in a UserControl such as:
XAML:
<UserControl x:Class="WpfApp1.Browser"
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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border x:Name="border" Background="Black" Margin="0"></Border>
</UserControl>
C#:
public partial class Browser : UserControl
{
WindowsFormsHost host = new WindowsFormsHost();
GeckoWebBrowser browser = new GeckoWebBrowser();
public Browser()
{
InitializeComponent();
Xpcom.Initialize("Firefox");
browser.Navigate("http://www.google.com");
host.Child = browser;
border.Child = host;
}
}
Now, you can use the tag in WPF, in the same project where the UserControl exists.
I have been trying to get this to work as a Control in a library, so I can easily port it to any other project/solution, but it keeps giving me an error about mozglue.dll missing. I suspect this is due to the Xpcom.Initialize("Firefox") but I need to investigate further.

Adding x:Name to a user control gives error in generated code of page its used in

I am basically using a user control for the first time, so hopefully it's just a dumb mistake.
I have a simple user control
<UserControl x:Class="TestProject.WebApp.myUserControl"
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"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid>
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding Data}" FilterMode="StartsWith" MinimumPrefixLength="2" >
</sdk:AutoCompleteBox>
</Grid>
</UserControl>
I am using the control in another page (in a stack panel):
<navigation:Page x:Class="TestProject.WebApp.myPageView"
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"
xmlns:local="clr-namespace:TestProject.WebApp"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid>
<StackPanel Orientation="Horizontal">
<local:myUserControl x:Name="myUC" />
</StackPanel>
</Grid>
</navigation:Page>
The control is in the same namespace as page. Removing the x:Name allows the project to compile and the control functionally works. When x:Name is specified I get an error in the generated code of the page:
Type 'TestProject.WebApp.myUserControl' is not defined.
It happens on these lines of code:
internal TestProject.WebApp.myUserControl myUC;
this.myUC == (TestProject.WebApp.myUserControl)this.FindName("myUC")
I tried with two different controls. Same thing. The generated code has "using TestProject.WebApp;" so I don't know why there is an error only when I have a named instance of the control.
I also cannot seem to use just the Name property. I even tried overloading the NameProperty in the control.
I was able to find a post about this here:
.g.vb file claims that Type X is not defined, even though it is!
Apparently, there is an issue when a project has a service reference (in my case I am using RIA services) and the user control resides in an assembly with the same root namespace as the one you are trying to use it in.
So for me it was,
TestProject.WebApp
Then I tried creating a new project for just the control
TestProject.Controls
Same problem.
Then, when I renamed the project and changed the namespace to
CustomControls
it worked.
Hopefully this saves someone else from some frustration and I really hope this problem can be fixed by MS soon.
What you are showing here should work. What is the default namespace in your project property's? Also did you rename your user control after you created it. Both of those issues could be causing you problems.

Using ICSharpCode.AvalonEdit on .Net 3.5?

I'm trying to use the ICSharpCode.AvalonEdit.TextEditor control from the SharpDevelop 4.0 project in a WPF app that I'm building, but I can't seem to get it to work.
I checked out a copy of the source code from svn://svnmirror.sharpdevelop.net/sharpdevelop/trunk/SharpDevelop/src/Libraries/AvalonEdit at revision 4304. Then, I built the project using Visual Studio 2008 SP1, which succeeded without errors.
I then created a blank new WPF project, added the build DLL to the toolbox and dropped the TextEditor control onto the default empty window, like so:
<Window x:Class="AvalonEditTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
Title="Window1" Height="300" Width="300" >
<Grid x:Name="LayoutRoot">
<avalonedit:TextEditor Name="textEditor" />
</Grid>
</Window>
However, when I run the project, the form comes up completely blank. No caret, the mouse cursor stays the default pointer, and the window does not respond to keypresses.
Am I missing something, or is AvalonEdit just a little broken?
[EDIT: I'm starting to think it might be related to my specific setup. I'm running the 64-bit Windows 7 RC. Might that have something to do with it? I've tried building it for x86 only, made no difference.]
Are you sure your namespace declaration is correct?
You can try something like this:
<Window x:Class="Editor.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:e="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit">
<Grid>
<e:TextEditor x:Name="Editor" WordWrap="True" Height="200">
</e:TextEditor>
</Grid>
</Window>
I was able to get it to work without any issues.
The AvalonEdit TextEditor is just a view for a TextDocument model.
The problem was that a new AvalonEdit instance didn't start connected to any model instance, so there wasn't anything to edit.
The reason the code from statictype worked was that he didn't use <avalonedit:TextEditor/>, but <avalonedit:TextEditor></avalonedit:TextEditor>. This will assign an empty string to the Text property, which caused the editor to implicitly create a new document.
But this isn't relevant with recent AvalonEdit versions anymore, the editor will now always create a new TextDocument.
This works for me with the latest build
<DockPanel LastChildFill="True">
<avalonedit:TextEditor
HorizontalAlignment="Stretch"
Name="textEditor1"
VerticalAlignment="Stretch" />
</DockPanel>

Categories

Resources