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

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 = ""

Related

Report Viewer in WPF ReportDocument type doesnt work

Im trying to show a Report File in my WPF app in VS2017. CS1 code works perfect and ask for connection data. In CS2 I want to do it through ReportDocument, but I have only waiting Wheel with gray background and Viewer doesnt ask for connection data. Any idea how to do it? I need to do it with CS2 method.
CS1:
cryrep.ViewerCore.ReportSource = "C:\\Reports\\GeneralBusiness\\testDB.rpt";
cryrep.ViewerCore.RefreshReport();
CS2:
CrystalDecisions.CrystalReports.Engine.ReportDocument crReportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
crReportDocument.Load("C:\\Reports\\GeneralBusiness\\testDB.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);
cryrep.ViewerCore.ReportSource = crReportDocument;
cryrep.ViewerCore.RefreshReport();
My XAML:
<UserControl
x:Class="ReportingPortal.Views.ReportingPortalFilterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:UCM.Infrastructure.Behaviors;assembly=UCM.Infrastructure"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cr="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
lex:LocalizeDictionary.DesignCulture="de"
lex:ResxLocalizationProvider.DefaultAssembly="ReportingPortal"
lex:ResxLocalizationProvider.DefaultDictionary="Resources"
prism:ViewModelLocator.AutoWireViewModel="True"
KeyboardNavigation.TabNavigation="Cycle"
mc:Ignorable="d">
<Grid Background="{DynamicResource LightLightBaseColorBrush}">
<cr:CrystalReportsViewer
x:Name="cryrep"
Width="1000"
Height="1600"
Margin="-14,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
I had to install CrforVS Runtime and now it works perfect

Navigation in Windows Universal Apps 10 Not Working (Taking me to a blank page)

and thank you. I'm using Visual Studio 2015 and trying to navigate between 2 pages, MainPage.xaml and Meds.xaml On MainPage.xaml I put this button to navigate to Meds.xaml with.
<Button x:Name="button_meds" Content="Meds" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="268,405,0,0" FontSize="18.667" Click="button_meds_Click"/>
In the MainPage.xaml.cs file, I simply did this
private void button_meds_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Meds));
}
Meds.xaml has an image and background, but I omitted the logo and grid from the code below
<Page
x:Class="SQLMobileMoodSwing.Meds"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SQLMobileMoodSwing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</Page>
I'm not getting any errors, but when I click the button, it simply takes me to a white page. Not sure what I'm doing wrong, have looked for solutions but nothing seems to work.
Thank you!
I have created Meds.xaml as XAML View, not as Blank Page.
Then I deleted it and created again as a Blank Page and BOOM! It works

UserControl content can't be set more than once

having some troubles with UserControl. I cannot get rid of "Content is set more than once error"
From what I've read, the common cause is that .. well, content is set more than once. For example having more than one child or setting content via Content=".." and then specifying another content between the tags.
However, this is UserControl generated by VisualStudio and I made no changes to the xaml.
<UserControl x:Class="TMEGadget.View.Toolbox"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
Got any tips?
P.S: Can anyone tell me why, when I try to type "Hey,\n\nSome text...", the "Hey,\n\n" is deleted?
Edit: Restarting VS fixed the problem , thanks #Bolu

Can't access Windows 8 Store user control from code

I'm writing a Windows 8 Store application and within that I've designed my own user control.
Here is the code for my usercontrol (This is a dummy control but the problem exists with this):
<UserControl
x:Class="Windows8StoreTest.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="70"
Height="40">
<StackPanel>
<Button Content="Hello" Foreground="Pink" BorderBrush="Pink"/>
</StackPanel>
</UserControl>
I've dropped the user control onto my page and give it a name:
<Page
x:Class="Windows8StoreTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:TestUserControl Name="testControl"/>
</Grid>
</Page>
However, when I go to the code behind I can't access the control by that name. It doesn't seem to exist! What is weird is that the control doesn't exists within InitializeComponent() method for the MainPage class which will be why it does exist.
What am I missing from my user control?
I'm using Windows 8 Store, XAML, c#.
Thanks in advance
Try to use this:
<local:TestUserControl x:Name="testControl"/>
Should work...
hello i don't know what is wrong but it should work.i have just made a sample example of it..i am putting it here hope you have done the same way.
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 x:Name="hellousercontrol" />
</Grid>
in my mainpage.cs.. i have just use it like this..
public MainPage()
{
this.InitializeComponent();
hellousercontrol.Height = 100;
}
one more this..have build your solution ?
I had the same issue in c++ environment. I observed, I didn't had default constructor in my class, as soon as I added the default constructor, I could use the defined UserControl in my project through XAML file. However without default constructor I was able to use it from within c++ code.

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

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.

Categories

Resources