More Issues Opening Window WPF - wpf

I'm using VB.net and WPF 4.
I cannot seem to open a new window in WPF consistantly. It will work one time, and then the next time I execute the same script, it throws the following exception:
XAMLParseException occured 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '4' and line position '208'. {Cannot evaluate expression because the code of the current method is optimized.}
Here is the script:
Dim Window As Window = New GAME_WINDOW
Window.Show()
Yes, "GAME_WINDOW" is a legitimate WPF window. Here are the first four lines of XAML code for the window I'm trying to open:
<Window x:Class="GAME_WINDOW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Operation SpyRat: Word4Word" Icon="/VBP-WORD4WORD;component/alicia.ico" WindowState="Maximized" Background="Black" xmlns:my="clr-namespace:VBP_WORD4WORD">

I suspect the problem is the window Icon specification in the XAML. Try removing the Icon="..." from your XAML, and see if it corrects the issue.
If so, you'll need to make sure this is specified in proper Pack URI format. Most likely, this would be:
Icon="pack://application:,,,/component/alicia.ico"

Related

WPF RichTextBox shows text in FlowDocument only 1 character per line

I copied the code example from the RichTextBox help into an empty WPF window like this:
<Window x:Class="RichTextBoxWidthProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<RichTextBox>
<FlowDocument>
<Paragraph>
This is flow content and you can
<Bold>edit me!</Bold>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
The result looks rather strange:
Why is there only 1 character written per line and how can I fix this ?
After spending a day trying to find the reason for this problem, I just run the application and the result looks like this:
The text uses all available space and gets reformatted if the Window size changes. This basically means there is no real problem with my code, but with the WPF designer in Visual Studio. After some further investigation I found out that in the designer, the Paragraph is only zero pixel wide.
If you want to see also in the designer a reasonable display, give a Width to Window, Grid or RichTextBox. But, of course, the text will then no longer use all the available space, but only the width you defined, which might not be what you want. If you want the width only to be used by the designer, but not during runtime, add the 'd:' before Width:
<Window x:Class="RichTextBoxWidthProblem.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" d:Width="333">
Please read this first before marking this as duplicate
I made the unfortunate experience that often a question gets marked a duplicate, when it is actually not. I am aware that there are several questions regarding FlowDocument not using the available width, but please note that most recommend to set ColumnWidth or some other width, which would actually then be correctly displayed in the Designer, but the running application would be limited to that width, which can be rather annoying (no resizing of the FlowDocument when the Window is made bigger).
Therefore it is important that an answer is on Stackoverflow explaining that it is in this case just a designer problem and not really a width problem.

Command line equivalent of Visual Studio Add New Page (WPF)

In the WPF first desktop app walkthrough, it uses Visual Studio to explain how to create a new page in a WPF project. I don't use Visual Studio, but I want to make an XAML Page file using the terminal. I can't just create a new file because it has prewritten code for a new Page similar to below:
<Page x:Class="ExpenseIt.ExpenseItHome"
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="350" d:DesignWidth="500"
Title="ExpenseIt - Home">
<Grid>
</Grid>
</Page>
I checked the dotnet new documentation and it says nothing about creating a new file. I know I can just have the default contents in a separate file which I can copy and paste but this seems like an inelegant workaround. Is there a command line argument or another way to get the Page WPF Template without Visual Studio?
Edit
To be more specific on what I am looking for, I would like an XAML file and its corresponding .xaml.cs code-behind file with all the using statements. As suggested, I can use XamlReader, which I have tried, but I would preferably like a tool. XamlPadX is outdated and does not have the code-behind that I am looking for.
Edit 2
This is my current code in the console app I am making:
if (args[0].StartsWith("loadpage-"))
{
string className = args[0].Substring("loadpage-".Length);
string xamlOutput = String.Join(
Environment.NewLine,
$"<Page x:Class=\"{className}\"",
"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=\"350\" d:DesignWidth=\"500\"",
$"Title=\"\">",
"<Grid>", "", "</Grid>",
"</Page>");
Console.WriteLine(xamlOutput);
}
else
{
Console.WriteLine("You must enter the argument in the following format: ");
Console.Write("loadpage-CLASSNAME");
}
You could write a console application which uses command line arguments to receive the xaml content via pure text, or text file containing the content of the xaml page.
Load it using XamlReader in Main method of your program.cs:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlreader?view=netcore-3.1

Telerik RadRibbonView pops up RadRibbonTab nested objects to the left in Surface Pro

Got an interesting issue that only affects the code on a Surface Pro 3. When running a telerik control for a RadRibbonView and then having it minimized the popup will open to the very left of the screen. This behavior does not happen on a desktop and works just clipping below the RadRibbonTab. On a Surface Pro 3 it works fine when the View is not minimized but when minimized it behaves badly. I was thinking of investigating more on what could be causing this and was following some of the tutorials on Telerik: https://docs.telerik.com/devtools/wpf/controls/radribbonview/features/ribbon-controls/ribbon-window-wpf. In my example XAML shown it is using their RadRibbonWindow but a Window top level will create this problem as well. I will most likely contact Telerik as well but was curious if anyone on Stack Overflow has seen this and known how to deal with it.
<telerik:RadRibbonWindow x:Class="TelerikTesting.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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TelerikTesting"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<telerik:RadRibbonView x:Name="RibbonView" IsMinimizable="True" IsMinimized="True" MinimizeButtonVisibility="Visible"
PopupOpened="RadRibbonView_PopupOpened">
<telerik:RadRibbonView.Items>
<telerik:RadRibbonTab Header="Home">
<telerik:RadRibbonGroup Header="HomeContent" >
</telerik:RadRibbonGroup>
</telerik:RadRibbonTab>
<telerik:RadRibbonTab Header="View" />
</telerik:RadRibbonView.Items>
</telerik:RadRibbonView>
</telerik:RadRibbonWindow>
Well this should not be happening.
Works fine when docked and not minimized
Telerik posted a response and it is essentially the default behavior of Tablet Devices that is causing the problem. You can fix it by changing the OS behavior directly or in code that you hook up to fire on a constructor. Either way it seems to easily solve the issue on Surface Pro 3 and I am guessing other tablet devices as well.
From Telerik:
'The described behavior is caused by the settings of the window's popups. The popups in the Windows OS are rendered in different directions based on the Handedness setting. You can verify it by going to your Windows settings: ControlPanel->Tablet PC Settings. If the handedness is set to "Left-handed", which is the default value in the most version of the OS, the popup menus will be rendered from right to left of their target element. In other words, the popup with the RadRibbonView's content will be position correctly. On the other hand, the "Right-handed" setting will draw the popups from right to left.
You can try changing the Handedness to "Left-handed" and see if this resolves the issue. Another workaround which can try is using code. Basically, you can change the value of the SystemParameters.MenuDropAlignment property. You can use the following method to achieve this:
public static void SetAlignment()
{
var ifLeft = SystemParameters.MenuDropAlignment;
if (ifLeft)
{
// change to false
var t = typeof(SystemParameters);
var field = t.GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static);
field.SetValue(null, false);
ifLeft = SystemParameters.MenuDropAlignment;
}
}
'
I just hooked up the code method given to my constructor of the view in the code behind and it worked just fine.

Value cannot be null. Parameter name: hostNameOrAddress

I have a WPF application and am suddenly getting the error
Value cannot be null. Parameter name: hostNameOrAddress
With this error the program will still run. When I remove the datacontext from the window this error goes away. In this window my datacontext is my MainViewModel. I tried removing the MainViewModel from the assembly and adding it back in thinking that might solve the problem but to no avail. Does anyone have any thoughts on why this could be happening. Note: I am using MahApps.Metro.
<controls:MetroWindow x:Class="DatalogConversion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DatalogConversion"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:helper="clr-namespace:DatalogConversion.Helper"
Height="auto"
Width="325"
SizeToContent="Height">
<controls:MetroWindow.DataContext>
<local:MainViewModel/>
</controls:MetroWindow.DataContext>
I was using an attached property to call a method in my view model when my window was loaded
helper:MvvmBehaviors.LoadedMethodName="OnWindowLoaded"
I didn't realize this at the time but the problem was I was calling a service from the method and it was breaking the designer. To fix the problem I inserted a guard clause in the OnWindowLoaded method to fix the problem.
if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject())) return;

ImageBrush throws exception when trying to set Window background

I'm trying to set the background of my WPF window to an image but I'm getting this exception when I try to run it:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.
I don't want to add the image to the project, as I would like to be able to change the image at runtime. My intention is to use databinding to set the background picture during start-up once I have this bit working.
Source Code:
<Window x:Class="ColinsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Colin's Test Application"
WindowStyle="None"
WindowState="Maximized">
<Window.Background>
<ImageBrush
ImageSource="DeLaRue-Background.jpg"/>
</Window.Background>
<Grid></Grid>
</Window>
Any Ideas? Thanks
Wrong. It should be set to CONTENT not Resource.
Include in your project (use the file explorer)
Right click on the image > Properties > Advanced.
Build Action: Content
Copy to Output Directory: Copy always.
Your image's build action is probably set to Content, change it to Resource
Set the item (your image file) to be a Resource. (In Visual Studio) Right click,Properties,Build Action,Resource
I had the same problem with
<Button>
<Button.Background>
<ImageBrush ImageSource="/Resources/icon_done.png">
</Button.Background>
</Button>
I used / before Resources and it work correctly, but i used solution with properties image ->Properties->Build to Resources.
These two actions solved my issue.
Add the forward "/" to the location
Then the whole ImageSource string should look like
<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" />

Resources