How to supress children from invoking parent's event? - c#

I'm creating simple touch-based user comtol web browser. I'm using ManipulationDelta event to pan, zoom-in, zoom-out etc. The browser (Awesomium WebControl) is placed inside a grid of control. When I try to scroll page whole browser moves. How to supress invoking parent's event by childen?
I've set userinput type to ViewInput.Mouse because I want to handle touch by myself.
XAML:
<UserControl
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:awe="http://schemas.awesomium.com/winfx" x:Name="_control" x:Class="WebControlTouch.WebBrowser"
mc:Ignorable="d" d:DesignWidth="497" Height="323.333">
<Grid TouchDown="_mainGrid_TouchDown" x:Name="_mainGrid" Background="Honeydew" Grid.Row="0" Margin="0,0,-66,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="466*"/>
<ColumnDefinition Width="8*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="84*"/>
<RowDefinition Height="7*"/>
<RowDefinition Height="220*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<awe:WebControl TouchDown="_browser_TouchDown" Visibility="Visible" LoadingFrameFailed="_browser_LoadingFrameFailed" LoadingFrameComplete="_browser_LoadingFrameComplete" LoadingFrame="_browser_LoadingFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="_browser" AddressChanged="_browser_AddressChanged" Width="Auto" Height="Auto" Grid.Row="2" Grid.Column="1" Margin="0,0,1,0"/>
<TextBox Grid.Row="0" HorizontalAlignment="Left" Name="_addressBar" Height="43" Margin="0,10,0,0" TextWrapping="Wrap" Text="Address" VerticalAlignment="Top" Width="146" Grid.ColumnSpan="2"/>
<Button x:Name="_goButton" Grid.Row="0" TouchDown="Button_TouchDown" Content="Go!" HorizontalAlignment="Left" Margin="131,10,0,0" VerticalAlignment="Top" Width="132" RenderTransformOrigin="-0.164,0.514" Height="43" Click="Button_Click" Grid.Column="1"/>
<Button TouchDown="Btn_back" x:Name="_btnBack" Content="Back" Click="Btn_back" HorizontalAlignment="Left" Margin="276,10,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="20"/>
<Button TouchDown="_btn_forward" x:Name="_btnForward" Content="Forward" HorizontalAlignment="Left" Margin="365,10,0,0" VerticalAlignment="Top" Width="75" Click="_btn_forward" Grid.Column="1" Height="20"/>
<Label x:Name="_Status" Content="Ready" Margin="277,42,130,16" VerticalAlignment="Center" Grid.Column="1" Height="26"/>
<Button x:Name="_exitButton" Content="Exit!" Grid.Column="1" HorizontalAlignment="Left" Margin="365,45,0,0" VerticalAlignment="Top" Width="75" TouchDown="_exitButton_TouchDown"/>
<Button x:Name="_temp" Content="Scroll" Grid.Column="1" HorizontalAlignment="Left" Margin="459.312,10.04,0,0" VerticalAlignment="Top" Width="75" Click="_tempClick"/>
</Grid>
</UserControl>

This means you want some event(s) to stop bubbling (up) the control hierarchy.
All you should usually need to do is marked the event as handled (the event instance you get in your event handler):
e.Handled = true;
In your case should be something like:
private void _browser_TouchDown(object sender, TouchEventArgs e)
{
//...
e.Handled = true;
//...
}

Related

System.AccessViolationException when trying to launch UWP program

I tried to make a new UWP app just to test things out, but now I'm stuck with this exception:
System.AccessViolationException: 'Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.'
All I did after making new Solution, was to make few more pages and added this to the App() in order to achieve global back button:
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
Frame rootFrame = Window.Current.Content as Frame;
}
private void App_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
{
e.Handled = On_BackRequested();
}
And now, every time, it goes to this:
var currentView = SystemNavigationManager.GetForCurrentView();
I get this exception. I'm running out of ideas. Does anyone know what could cause this? In my Debugging settings I have unchecked the: "Suppress JIT opimization on module load" -setting, as suggested elsewhere, but that doesn't help.
Is there something that I don't understant of making an UWP-app, where you can click buttons at the top, and a page would change below them. Here's the XAML behind this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" x:Name="stackPanel_up" Orientation="Horizontal" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="10">
<Button x:Name="eka" Background="Aqua" Content="First" Width="200"
Height="50" FontSize="30" FontWeight="Bold" CornerRadius="10" Margin="5,0,5,0"
Click="eka_Click"/>
<Button x:Name="toka" Background="Aqua" Content="Second" Width="200" Height="50"
FontSize="30" FontWeight="Bold" CornerRadius="10" Margin="5,0,5,0"
Click="toka_Click"/>
<Button x:Name="kolmas" Background="Aqua" Content="Third" Width="200" Height="50"
FontSize="30" FontWeight="Bold" CornerRadius="10" Margin="5,0,5,0"
Click="kolmas_Click"/>
</StackPanel>
<Button Grid.Column="0" Grid.Row="0" x:Name="BackBtn" FontSize="50" Margin="40,0,0,0"
HorizontalAlignment="Left" Width="100" Height="100" CornerRadius="30"
Background="Transparent" Click="Back_Click" Style="{StaticResource NavigationBackButtonNormalStyle}" />
<Frame Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" x:Name="ChangingArea" Margin="10" Background="AliceBlue"></Frame>
</Grid>

Hiding flyout of a button

I got my DataTemplate for items and within this DataTemplate I have such code:
<Button x:Name="DoneButton"
Style="{StaticResource ButtonStyle1}"
BorderThickness="1"
Margin="0,0,20,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Column="2"
Grid.Row="1"
Width="50"
Height="50"
>
<Image Source="Images/WPIcons/checked.png" Width="30" Height="30" Margin="-10,0,-10,0" />
<Button.Flyout>
<Flyout x:Name="myFly">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" x:Uid="myNote" Text="Note: " Style="{StaticResource myText}" />
<TextBox Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" Height="40" x:Name="note" Text="{Binding RecentNote, Mode=TwoWay}" Style="{StaticResource TextBoxStyle1}"/>
<Button x:Name="CompletedButton"
Command="{Binding CompletedCommand}"
CommandParameter="{Binding}"
Style="{StaticResource ButtonStyle1}"
BorderThickness="1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Row="2"
Click="CompletedButton_Click"
Content="Done"
MinWidth="80"
Height="40"
/>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
After the flyout for the item has been called and user put his data in it I want to hide this flyout as soon as user hits the "Done" button (x:Name="CompletedButton").
I tried to do that in code behind like:
private void CompletedButton_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
Grid grid = (Grid)VisualTreeHelper.GetParent(button);
Flyout fly = (Flyout)VisualTreeHelper.GetParent(grid);
fly.Hide();
}
But I get cast exception with that I can't cast ContentPresenter to Flyout so I guess it's not the way I look for.
How I can hide this flyout?
I resolved it with creating global DependencyObject on the page. So when you click the button it keeps it and I can call its flyout to hide() from button within this flyout. A bit ugly but works like a charm.

Enable Manipulation On Windows Phone 8

I have the following code which I found from here:
private void Grid_ManipulationStarted_1(object sender, ManipulationStartedRoutedEventArgs e)
{
initialpoint = e.Position;
}
private void Grid_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial)
{
Point currentpoint = e.Position;
if (currentpoint.X - initialpoint.X >= 500)//500 is the threshold value, where you want to trigger the swipe right event
{
System.Diagnostics.Debug.WriteLine("Swipe Right");
e.Complete();
}
}
}
And my Xaml:
<Page
x:Class="MyApp.DetailPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Viewbox x:Name="MainViewbox" Stretch="Uniform" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1, 1, 1, 1" ManipulationDelta="Grid_ManipulationDelta_1" ManipulationStarted="Grid_ManipulationStarted_1">
<Grid ManipulationDelta="Grid_ManipulationDelta_1" ManipulationStarted="Grid_ManipulationStarted_1" Background='Transparent'>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="IDAndTitleTxt" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="1" TextAlignment="Center" Height="Auto" Width="Auto" FontSize="20" Foreground="White"/>
<TextBlock x:Name="IssueType" HorizontalAlignment="Center" VerticalAlignment="Top" Text="Code Defect" MaxHeight="10" Height="Auto" Width="Auto" Foreground="White" FontSize="4"/>
<TextBlock x:Name="UserAssignedTo" HorizontalAlignment="Center" VerticalAlignment="Top" Height="Auto" Width="Auto" Grid.Row="3" Foreground="White" FontSize="8"/>
<TextBlock x:Name="StateSubstate" HorizontalAlignment="Center" VerticalAlignment="Top" Text="Active / In Progress" Height="Auto" Width="Auto" Grid.Row="4" Foreground="White" FontSize="8"/>
<Button x:Name="BackBtn" Content="Back" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="40" Grid.RowSpan="2" Grid.Row="0"/>
<ScrollViewer Height="260" Width="200" HorizontalScrollBarVisibility="Disabled" Grid.Row="2" Canvas.Top="60" Canvas.Left="340">
<TextBlock Width="195" TextWrapping="Wrap" FontSize="5" x:Name="DescriptionTxt"/>
</ScrollViewer>
</Grid>
</Viewbox>
My problem is that both the manipulation events never fire no matter how much I swipe the screen in all directions. I've put breakpoints on the event handlers to confirm this. Someone else has already asked the same question here but there is no answers. Seeing as that question was more than 2 years ago, I'm rewriting it.
Also, my grid is inside a viewbox, which is in a page (not a window).
Any help is appreciated :).
I guess you meant a Windows Phone 8.1 runtime application? Because in silverlight it works fine.
For that you should set ManipulationMode to All for your grid. In my test it worked fine even without a background color (which is necessary in silverlight applications).
If you need just a subset of manipulations, you should use the best mode for your needs.
You need to add a background brush - eg Background="Transparent" - in order for it to receive input events.

Controls in WPF sticking together when resizing

Hello i am newbies in WPF and i have some problems with designing my UI.
In the design view, i have this:
http://i.stack.imgur.com/papJ2.png
And this is my XAML code:
<mui:ModernWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:awe="http://schemas.awesomium.com/winfx" x:Name="___No_Name_" mc:Ignorable="d" x:Class="JJADesign.MainWindow"
Title="mui"
LogoData="F1 M 24.9015,43.0378L 25.0963,43.4298C 26.1685,49.5853 31.5377,54.2651 38,54.2651C 44.4623,54.2651 49.8315,49.5854 50.9037,43.4299L 51.0985,43.0379C 51.0985,40.7643 52.6921,39.2955 54.9656,39.2955C 56.9428,39.2955 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0244,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.2954 21.0344,39.2954C 23.3079,39.2954 24.9015,40.7643 24.9015,43.0378 Z M 26.7727,20.5833C 29.8731,20.5833 32.3864,23.0966 32.3864,26.197C 32.3864,29.2973 29.8731,31.8106 26.7727,31.8106C 23.6724,31.8106 21.1591,29.2973 21.1591,26.197C 21.1591,23.0966 23.6724,20.5833 26.7727,20.5833 Z M 49.2273,20.5833C 52.3276,20.5833 54.8409,23.0966 54.8409,26.197C 54.8409,29.2973 52.3276,31.8106 49.2273,31.8106C 46.127,31.8106 43.6136,29.2973 43.6136,26.197C 43.6136,23.0966 46.127,20.5833 49.2273,20.5833 Z"
Style="{StaticResource BlankWindow}" d:DesignHeight="1055" d:DesignWidth="1400" WindowStartupLocation="CenterScreen" WindowStyle="SingleBorderWindow">
<DockPanel>
<TabControl DockPanel.Dock="Top" Height="69">
<TabItem Header="TabItem">
<Grid Margin="0,0,0,1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="225*"/>
<ColumnDefinition Width="466*"/>
</Grid.ColumnDefinitions>
<TextBox HorizontalAlignment="Left" Height="23" Margin="147,4,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="218"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="95,3,0,0" VerticalAlignment="Top" Width="170" Grid.Column="1"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="370,3,0,0" VerticalAlignment="Top" Width="170" Grid.ColumnSpan="2"/>
<Label Content="Saisir une produit JJA" HorizontalAlignment="Left" Width="100" Margin="15,7,335,5" />
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="676*"/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
</TabControl>
<awe:WebControl DockPanel.Dock="Bottom" Source="http://www.google.com/"/>
</DockPanel>
</mui:ModernWindow>
But when i run my application, i got this:
Normal:
http://i.stack.imgur.com/IH300.png
How can i make the control not sticking together?
You have the items in the tab grouped in a <grid>, but are using absolute margins to position them within the grid.
Try using the stackpanel with margins on the items inside it, like this :
<TabItem Header="TabItem">
<StackPanel Orientation="Horizontal" Margin="0,0,0,1">
<TextBox HorizontalAlignment="Left" Height="23" margin="0,0,10,0" TextWrapping="Wrap" Text="TextBox" Width="218"/>
<Button Content="Button" margin="0,0,10,0" Width="170" />
<Button Content="Button" margin="0,0,10,0" Width="170" />
<Label Content="Saisir une produit JJA" Width="100" />
</StackPanel>
</TabItem>

C# Strange WPF Combobox Behavior

I have simple window.
This is what happens when I click ComboBox:
List appears in upper left corner of screen instead of under Combobox.
XAML:
<Window x:Class="WpfPortOfTestingCamera.VideoSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Video Settings" WindowStartupLocation="CenterOwner" ResizeMode="NoResize" ShowInTaskbar="False" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" SizeToContent="WidthAndHeight" d:DesignHeight="167">
<StackPanel Name="stackPanel1" VerticalAlignment="Top" HorizontalAlignment="Center">
<GroupBox Header="Settings" Name="groupBox1">
<Grid Name="grid1" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*" />
<ColumnDefinition Width="175*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Resolution:" Height="28" Name="label1" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Label Content="Framerate:" Height="28" HorizontalAlignment="Left" Margin="0" Name="label2" VerticalAlignment="Center" Grid.Row="1" />
<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0" Name="comboBox1" VerticalAlignment="Center" Width="150" SelectionChanged="comboBox1_SelectionChanged" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0" Name="comboBox2" VerticalAlignment="Center" Width="150" Grid.Column="1" Grid.Row="1" SelectionChanged="comboBox2_SelectionChanged" />
</Grid>
</GroupBox>
<Label Name="labelSelectedSize" Content="Size # FPS" />
<Button Name="button1" Content="Apply" Click="button1_Click" />
</StackPanel>
</Window>
Instead of opening it directly in the Loaded event, just queue another message on the Dispatcher to open it.
I ran into exactly this and just posted an example at WPF ComboBox DropDown part appears in the wrong place which worked for me. The interested reader can go there to peruse my commentary, but here's the snippet (NOTE: WindoBaseLoadedHandler is the "Loaded=" handler specified in the XAML):
protected void WindowBaseLoadedHandler(object sender, RoutedEventArgs e)
{
...non-essential lines of code removed...
if (DataContext != null)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.IsEnabled = false;
LoginDlg loginDlg = new LoginDlg();
loginDlg.ShowDialog();
if (!loginDlg.Success)
{
/*-----------------------------------
* Log on failed -- terminate app...
*----------------------------------*/
...termination logic removed...
}
this.IsEnabled = true;
}));
}

Categories