System.AccessViolationException when trying to launch UWP program - c#

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>

Related

WPF C#: Increase MainWindow width dynamically on page load

I'm trying to create an animation by using for loop when I load into page by increasing it's width and position. My problem is that Application.Current.MainWindow.Width in the Page_Loaded for loop doesn't work. While it works perfectly when leaving the page. My Code:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
for (double i = 0; i < 50; i++)
{
Application.Current.MainWindow.Left = Application.Current.MainWindow.Left - 2;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width + 4;
}
}
private void Home_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
for (double i = 0; i < 50; i++)
{
Application.Current.MainWindow.Left = Application.Current.MainWindow.Left + 2;
Application.Current.MainWindow.Width = Application.Current.MainWindow.Width - 4;
}
}
When I go to the intended page, my window just moves to the left but the width doesn't increase until the page is completely loaded and it just jumps to the final width rather than animate that. When I leave the page, both my window and width are being changed together.
Is there some limitation when setting the width under Page_Loaded? If so, how can I get over this?
XAML Settings
mc:Ignorable="d"
Loaded="Page_Loaded"
Title="DatabaseViewer">
<Grid >
<TextBlock Height="75" Margin="65,20,65,0" Text="Solder Paste Database" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" />
<Button Margin="0,17,30,0" Width="50" Height="50" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Top" BorderThickness="0" Click="Export_Click"
Style="{DynamicResource SquareButtonStyle}" ToolTip="Export to Excel">
<StackPanel>
<Rectangle Width="40" Height="40" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Green">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_office_excel}" />
</Rectangle.OpacityMask>
</Rectangle>
</StackPanel>
</Button>
<DataGrid Name="dbGrid" Width="750" Height="340" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20"
HorizontalContentAlignment="Center" IsReadOnly="True" PreviewKeyDown="dbGrid_KeyDown" GridLinesVisibility="All"
BorderBrush="Black" BorderThickness="1" SelectionChanged="dbGrid_SelectionChanged" >
</DataGrid>
<Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,70,25,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label FontSize="14" FontWeight="Bold" Grid.Column="0" Grid.Row="0" Content="Selected Count: " HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<Label FontSize="14" Grid.Column="1" Grid.Row="0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Bottom" x:Name="selectedCount" />
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,70,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="26" />
</Grid.RowDefinitions>
<TextBox Width="150" FontSize="14" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" x:Name="TB" ToolTip="Column Name = Value [& Column Name = Value]"/>
<Button Margin="10,0,0,0" Padding="0,0,0,0" FontSize="14" Width="80" Content="Filter" Grid.Column="1" Grid.Row="0" VerticalContentAlignment="Center"
HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="filter_click" />
<Label Grid.Column="2" x:Name="filterLabel" Content="Invalid Search" Foreground="Red" FontWeight="Bold"/>
</Grid>
<Button Margin="20,27,0,0" Width="30" Height="30" FontSize="18" HorizontalAlignment="Left" Click="Home_Click" VerticalAlignment="Top" BorderThickness="0" UseLayoutRounding="True" BorderBrush="White"
Style="{DynamicResource MetroCircleButtonStyle}" >
<Rectangle Width="30" Height="30"
Fill="#41b1ff">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{DynamicResource appbar_arrow_left}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
</Grid>
Using explicit code to change dimensions in a loop will most likely result in batching as you have seen. Not to mention you are blocking the UI during all this so I am rather surprised you saw anything change during the loop.
The correct way to essentially animate is to use WPF Storyboards. Here you would define your start and end conditions, what it is you want to animate (width, Left) and how long the animation should run for and tell it to play. Storyboards then run without you needing to do anything more. You can define it in code or straight-up XAML.
Tell me more...
MickyD is right, a Storyboard is the best bet in this situation. Though I can achieve what you want through looping in the code-behind, the result is often buggy as you have seen. In your situation for example you would want to make a DoubleAnimation for expansion in your Windows resources.
XAML
<Window.Resources>
<Storyboard x:Key="sb2">
<DoubleAnimation Storyboard.TargetName="myWindowName" Storyboard.TargetProperty="Width" From="500" To="1200" Duration="0:0:3" AutoReverse="False"/>
<DoubleAnimation Storyboard.TargetName="myWindowName" Storyboard.TargetProperty="Left" From="500" To="200" Duration="0:0:3" AutoReverse="False"/>
</Storyboard>
</Window.Resources>
Then in your Window (or Controls) Loaded event
private void myWindowName_Loaded(object sender, RoutedEventArgs e)
{
//Reference System.Windows.Media.Animation;
Storyboard storyBoardIn = (Storyboard)TryFindResource("sb2");
storyBoardIn.Begin();
}
You will obviously need to change your storyboard params in XAML to acheive the exact thing you want. This solution is less code and more reliable in WPF. Best of luck!

need help wpf app need the sum of 5 boxes

heeey everyone im new to wpf and i need some help i've been trying the internet but no luck so i thought i would ask here since u help alot of people here...
my project is to make a program that can find the sum of 5 different boxes but the problem is i dont know how to use the math function to make it understand that it needs to sum the boxes hope u understand what i mean...
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
double output1 = double.Parse(Txtbox1.Text) + double.Parse(Txtbox2.Text);
Result.Text = output1.ToString();
}
xaml
<Grid HorizontalAlignment="Left" Height="323" VerticalAlignment="Top" Width="517">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="102*"/>
<ColumnDefinition Width="127*"/>
<ColumnDefinition Width="147*"/>
<ColumnDefinition Width="141*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="32*"/>
<RowDefinition Height="43*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="40*"/>
<RowDefinition Height="33*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="32*"/>
</Grid.RowDefinitions>
<TextBox Name="Txtbox1" Grid.Column="3" HorizontalAlignment="Left" Height="32" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="131"/>
<TextBox Name="Txtbox2" Grid.Column="3" HorizontalAlignment="Left" Height="43" Grid.Row="1" TextWrapping="Wrap" Text="2" VerticalAlignment="Top" Width="131"/>
<TextBox Name="Txtbox3" Grid.Column="3" HorizontalAlignment="Left" Height="38" Grid.Row="2" TextWrapping="Wrap" Text="3" VerticalAlignment="Top" Width="141"/>
<TextBox Name="Txtbox4" Grid.Column="3" HorizontalAlignment="Left" Height="33" Margin="0,2,0,0" Grid.Row="3" TextWrapping="Wrap" Text="4" VerticalAlignment="Top" Width="141"/>
<TextBox Name="Result" Grid.Column="3" HorizontalAlignment="Left" Height="33" Margin="0,2,0,0" Grid.Row="7" TextWrapping="Wrap" VerticalAlignment="Top" Width="141" TextChanged="TextBox_TextChanged"/>
Your general approach is fine, but you need to trigger the calculation correctly.
You currently handle the TextChanged event of the result, but you really need to trigger on the inputs.
You can do this in several ways:
Move your event handler to the input boxes, or
Use a Command on the input boxes, or
Bind the inputs to properties of a view model, and have their setters update the output (you'll need to use INotifyPropertyChanged for this).
If none of that makes sense, then you'll just have to take some time to study more WPF.

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.

access controls present in data template from code behind

I have the following XAML code..
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DataTemplate1" >
<Border BorderBrush="LightGray" BorderThickness="1" Height="150" Width="500" >
<Grid Width="500" Height="150" Background="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="2.5*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Column="0" Height="Auto" Width="Auto" Source="/Images/notav.jpg" Margin="0,5,4,4" HorizontalAlignment="Left" />
<TextBlock Text="{Binding PRICE}" TextWrapping="Wrap" Grid.Column="1" Width="350" Foreground="Black" Height="60" Margin="30,85,20,-10"/>
<TextBlock Text="{Binding ITMNAME }" FontSize="22" TextWrapping="Wrap" Grid.Column="1" Name="txtITMNAME" Foreground="DarkBlue" Width="500" Height="130" Margin="30,40,20,-10"/>
<c4f:RoundButton Grid.Column="2" Name="btntick" Click="btntick_Click" Grid.Row="0" FontSize="25" HorizontalAlignment="Right" Background="LightGray" Foreground="DarkGray" Margin="10,20,45,10" />
</Grid>
</Border>
</DataTemplate>
<ListBox Height="Auto" Name="lstbxmanual" ItemTemplate="{StaticResource DataTemplate1 }" Width="475" Margin="4,148,0,5" Background="White" HorizontalAlignment="Left" Grid.RowSpan="2">
</ListBox>
I have to access round button at code behind ,to change its background property...
private void btntick_Click(object sender, RoutedEventArgs e)
{
btntick. //not able to access...
}
I have gone through following stackoverflow questions..
Access DataTemplate controls in code behind
MSDN link
it seems to be not relevant to my requirement..
please help me in this regard...
Solution 1: Use x:Name instead of Name.
Solution 2: Here is the an alternate way too.
You can cast the event sender object:
private void btntick_Click(object sender, RoutedEventArgs e)
{
RoundButton rdbtn = sender as RoundButton;
//rdbtn.BackColor
}
please try, x:Name in place of Name it will be accessible from your code behind file.
<c4f:RoundButton Grid.Column="2" x:Name="btntick" Click="btntick_Click" Grid.Row="0" FontSize="25" HorizontalAlignment="Right" Background="LightGray" Foreground="DarkGray" Margin="10,20,45,10" />
private void btntick_Click(object sender, RoutedEventArgs e)
{
btntick.Foreground = Brushes.Blue;
}
Good Luck.

How to supress children from invoking parent's event?

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;
//...
}

Categories