Wpf Enabling Transparency Throws Windows.Markup.XamlParseException - c#

My WPF App was working well. I then changed background colors for a button, which lies inside a transparent window, saved and compiled/run. Ever since, running my program throws
Windows.Markup.XamlParseException
with additional info
System.Windows.UI.Visibility threw an exception
I was unable to undo changes, and now I cannot solve this,unless I remove the "AllowTransparency = True" line. I need the window to be transparent, how can I fix this?
Here's the code for the troublesome block.
<Window
x:Class="WPF.OverlayButtonField"
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:local="clr-namespace:WPF"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="OverlayButtonField"
Width="175"
Height="139"
AllowsTransparency="True"
Loaded="Window_Loaded"
ShowActivated="False"
ShowInTaskbar="False"
Topmost="True"
Visibility="Visible"
WindowStyle="None">

You neglected to mention the InnerException, which clarifies the problem:
InvalidOperationException: WindowStyle.None is the only valid value for WindowStyle when AllowsTransparency is true.
The problem is that you're touching Visibility before setting WindowStyle to None. In principle, the order of attributes in XML doesn't matter. In XAML, in practice, it may sometimes matter. This is a case where it does.
Put Visibility="Visible" after WindowStyle="None", or put AllowsTransparency="True" after both of them -- or better yet, omit Visibility="Visible" entirely. It's redundant. Visible is the default.

Related

AvaloniaUI - ExtendClientAreaToDecorationsHint and Panel

I will improve my skill to developp in C#. I used to developp in C++ and Python.
I already train my self with consol application and I found the implementation of the POO paradigme very strong.
So I will try to produce UI like I can do in C++ and Python.
I follow this tutoriel : http://avaloniaui.net/docs/advanced-tutorial/
But I haven't the expected result like in the chapter : create-a-modern-window.
My windows buttons close, minimize and maximize are behind the panel. So It's hard to see this buttons. voir image
Of course, if I had Margin on the panel or if I remove panel, buttons is correctly visible.
It seems to me logic, that if I put a panel in front of something, it gives this result.
But the tutorial give nothing more and I check on the github : https://github.com/AvaloniaUI/Avalonia.MusicStore/tree/master/Avalonia.MusicStore and I found no difference with my code.
So do you have an idear of the problem or how to solves them ?
I work on Windows 10 with Avalovia 0.10.0.
My main window :
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Avalonia.MusicStore.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Avalonia.MusicStore.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="Avalonia.MusicStore"
TransparencyLevelHint="AcrylicBlur"
WindowStartupLocation="CenterScreen"
Background="Transparent"
ExtendClientAreaToDecorationsHint="True">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<Panel>
<ExperimentalAcrylicBorder IsHitTestVisible="False">
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial
BackgroundSource="Digger"
TintColor="Black"
TintOpacity="1"
MaterialOpacity="0.65" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<Panel Margin="40">
<Button Content="Buy Music" Command="{Binding BuyMusicCommand}" HorizontalAlignment="Right" VerticalAlignment="Top">
<PathIcon Data="{StaticResource store_microsoft_regular}"/>
</Button>
</Panel>
</Panel>
</Window>
And my App.axaml
Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Avalonia.MusicStore"
x:Class="Avalonia.MusicStore.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles>
<FluentTheme Mode="Dark"/>
<StyleInclude Source="avares://Avalonia.MusicStore/Icons.axaml"/>
</Application.Styles>
</Application>
Thank you in advance for your help :)
Unfortunately there was a minor bug in 0.10 release.
Please set on your Window the following property:
ExtendClientAreaChromeHints="PreferSystemChrome"

ReactiveUI - Views not rendering properly (or in full)

I'm in the process of starting a new project, using ReactiveUI and MahApps.Metro. The first hurdle I've hit is a problem of views not showing in their entirety. I have a Window, and in that Window I have a ViewModelViewHost from the RxUI library. It is a simple container for a nested view-model. The ActiveItem binding is properly binded to the view-model of the User Control, and the Button from the user control is visible on screen.
What I expect to see is a Window with a dark gray background and a button in the middle. What I see is the Window with its default background and a Button in the middle. This is not right. If I remove the button, I see nothing on the Window, only the default background.
It seems the ViewModelViewHost is only showing the actual contents of a UserControl and is disregarding what isn't considered a real Control, such as grids etc.
Has anyone come across this behaviour before?
<mah:MetroWindow x:Class="...MainWindow"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rx="clr-namespace:ReactiveUI;assembly=ReactiveUI"
WindowStartupLocation="CenterScreen"
ShowTitleBar="False"
ShowCloseButton="False"
ShowMaxRestoreButton="False"
ShowMinButton="False"
Height="768"
Width="1024">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<rx:ViewModelViewHost ViewModel="{Binding ActiveItem}" />
</Grid>
</mah:MetroWindow>
<UserControl x:Class="...NestedView"
Name="TheUserControl"
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"
TextOptions.TextHintingMode="Auto"
TextOptions.TextRenderingMode="Auto"
d:DesignHeight="768" d:DesignWidth="1024">
<Grid Background="DarkGray">
<Button Width="200" Height="100" />
</Grid>
</UserControl>
This won't be a ReactiveUI problem, just a simple WPF one.
The ViewModelViewHost is centered in your window. While your UserControl has a DesignHeight and DesignWidth set, when it's rendered at runtime it will automatically size itself to the height and width of its content - the Button.
Add VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" to your ViewModelViewHost declaration and remove the other two Alignment attributes (they default to Stretch) and it should stretch its contents to the size of the Window while centering any item that has a fixed size.

MouseDown event of image in usercontrol not fireing

I made a usercontrol in WPF with an image in it. I declared a MouseDown event for this image:
<Image x:Name="imgState" Height="300" Width="300" MouseDown="imgState_MouseDown" OpacityMask="#00000000" />
I placed this usercontrol on my application form, but the event isn't fireing. I'm pretty new to WPF and I read about RoutedEvents but I don't really understand it. I would be happy if someone could help and explain this to me!
Update
Changing to PreviewMouseDown didn't fire the event too. I tried setting the background to transparent and even tried with a blank 300x300 image. The grid workaround doesn't fire the event too. Here is how my code behind looks like:
private void imgState_MouseDown(object sender, MouseButtonEventArgs e)
{
//Some code here
}
Update 2
Here is my whole XAML file:
<UserControl x:Class="TicTacToe.controls.SingleField"
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>
<Image x:Name="imgState" MouseDown="imgState_MouseDown" Height="300" Width="300" Stretch="None" OpacityMask="#00000000"/>
</Grid>
</UserControl>
I removed the source again because I set one from code behind at runtime and adding a transparent/clear image didn't helped.
You probably want PreviewMouseUp instead of MouseDown event
<Image x:Name="imgState" Height="300" Width="300"
PreviewMouseUp="ImgState_OnPreviewMouseUp"
PreviewMouseDown="ImgState_OnPreviewMouseDown"/>
Either of the two, you can capture the event from there.
If the answer above does not help:
Not a very nice solution but does work so many times:
Wrap your image with a grid on which you will have your event...
<Grid MouseDown="imgState_MouseDown">
<Image/>
</Grid>
Okay I solved the problem myself.
The problem was the setting OpacityMask="#00000000" that prevented the image from appearing so there were, as #lll said, nothing to hit. I don't know when the setting was set, but I think it happened automatically while expanding the Representation tab.
Thanks for helping me!

wpf webbrowser on transparent window

I have a WPF WebBrowser control in a Window.
I want to remove the default window controls so I set AllowsTransparency="True" and WindowStyle="None".
<Window x:Class="InstallerToolkit.InteractiveDemosWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="870" Width="1110" WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen">
<Grid>
<WebBrowser Name="WebBrowser" HorizontalAlignment="Left" Height="795" Margin="10,35,0,0" VerticalAlignment="Top" Width="1082" />
</Grid>
This results in me not being able to see my webbrowser contents. If I remove the AllowsTransparency="True" then I can see by webpage but I now have the default controls which I dont want.
How do I get around this?
If the functionality you are looking for is truly AllowTransparency, you can update to WPF 4.5 where they have added a fix to Airspace issues, read this for more details.
If you are unable to upgrade your version of WPF then you will need to disable resize as others have mentioned.
Adding ResizeMode = NoResize should get you rid of the controls. I should mention that AllowTransparency = True can reduce performance! Depending on the kind of software, try avoinding it!

wpf borderless window focus not firing

I have the following WPF window definition but the focus/lost focus events are not firing when I click away from the application, any ideas?
<Window x:Class="BevaClient.windowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:BevaClient"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="VPN Tool" Height="500" Width="370"
Focusable="True"
WindowStyle="None"
ResizeMode="NoResize"
Topmost="True"
Style="{StaticResource defaultWindow}"
Loaded="Window_Loaded"
GotFocus="Window_GotFocus"
LostFocus="Window_LostFocus">
I have tried setting topmost to false to see if this was the issue but it makes no difference.
Any advice on this issue would be very much appreciated.
Thanks,
David
Hook to Activated and Deactivated events instead.

Categories