im doing a tetris game with a tutorial it works for him but not to for me idk what to do it does not work for me
<Window x:Class="tetris.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:local="clr-namespace:tetris"
mc:Ignorable="d"
Title="Tetris" Height="600" Width="800"
MinWidth = "600" MinHeight = "600"
Foreground = "White"
FontFamily="Segoe UI Light" FontSize="28"
KeyDown="Window_KeyDown">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Background>
<ImageBrush ImageSource="Assets/Background.png"/>
</Grid.Background>
<Viewbox Grid.Row="1"
Grid.Column="1"
Margin="0,0,0,20">
<Canvas x:Name="GameCanvas"
Background="#101010"
Width="250"
Height="500"
ClipToBounds="True"
Loaded="GameCanvas_Loaded"/>
</Viewbox>
<TextBlock x:Name="SourceText"
Grid.Row="0"
Grid.Column="1"
Text="Score: "
Margin="0,10"
TextAlignment="Center"/>
<StackPanel Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center"
HorizontalAlignment="Right">
<TextBlock Text="Hold"
TextAlignment="Center"/>
<Image x:Name="HoldImage"
Margin="20"
Width="125"/>
</StackPanel>
<StackPanel Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center"
HorizontalAlignment="Left">
<TextBlock Text="Next"
TextAlignment="Center"/>
<Image x:Name="NextImage"
Margin="20"
Width="125"/>
</StackPanel>
<Grid x:Name="GameOverMenu"
Background="#cc000000"
Grid.RowSpan="2"
Grid.ColumnSpan="3"
Visibility="Hidden">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="Game Over"
FontSize="48"
TextAlignment="Center"/>
<TextBlock x:Name="FinalScoreText"
Text="Score"
FontSize="36"
TextAlignment="Center"/>
<Button Content="PlayAgain"
Background="LightGreen"
Margin="0,20,0,0"
Padding="5"
Click="PlayAgain_Click"/>
</StackPanel>
</Grid>
</Grid>
</Window>
but they were not in the .cs file like = private void Window_KeyDown(object sender, KeyEventArgs e) i only have this one / but the guy in the tutorial had these two too=
private void GameCanvas_Loaded(object sender, KeyEventArgs e)
{
}
private void PlayAgain_Click(object sender, KeyEventArgs e)
{
} i added these two by my self but it did not work .
To subscribe an event (e.g., Loaded or Click), you handler (e.g., GameCanvas_Loaded) must match with the event's type. Both these events require the second parameter to be RoutedEventArgs. Check Loaded, Click. You usually don't need to write the handlers manually, if you write Load=" in your IDE, it will suggest creating a handler for you.
private void GameCanvas_Loaded(object sender, RoutedEventArgs e)
{
}
private void PlayAgain_Click(object sender, RoutedEventArgs e)
{
}
Related
I developed an UWP application that's having a wrong behavior on new version of Windows 10. In my application i have some ContentDialog with some TextBox inside them and when the soft-keyboard is shown it should push upwards the contentdialog but in new build of windows 10 this is not happening, anyone know why?
Here is a sample of my ContentDialog code:
<ContentDialog
x:Class="ManImp.CDEseguiOdM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ManImp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
MaxHeight="{StaticResource ContentDialogMaxHeight}"
MaxWidth="{StaticResource ContentDialogMaxWidth}"
Margin="0"
Padding="0">
<Grid Background="{ThemeResource ApplicationBackGroudColor}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<!--0-->
<RowDefinition Height="Auto"></RowDefinition>
<!--0-->
<RowDefinition Height="Auto"></RowDefinition>
<!--1-->
<RowDefinition Height="Auto"></RowDefinition>
<!--2-->
<RowDefinition Height="Auto"></RowDefinition>
<!--3-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" x:Name="stckLabelFirma" Grid.Row="0" Grid.Column="0">
<TextBlock x:Name="lblFirma" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="24" Foreground="White" FontWeight="Normal" HorizontalAlignment="Center" Margin="0,0,0,5"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="0">
<InkToolbar InitialControls="None" VerticalAlignment="Top" HorizontalAlignment="Right" TargetInkCanvas="{x:Bind inkCanvasFirma}" >
<InkToolbarEraserButton />
<InkToolbarBallpointPenButton />
</InkToolbar>
<Grid BorderBrush="Black" BorderThickness="1">
<Image Name="imgFirma" Stretch="Uniform"></Image>
<InkCanvas x:Name="inkCanvasFirma" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></InkCanvas>
</Grid>
</StackPanel>
<StackPanel Orientation="Vertical" x:Name="stckDatiSupProd" Grid.Row="2" Grid.Column="0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<!--0-->
<ColumnDefinition Width="*"></ColumnDefinition>
<!--1-->
<ColumnDefinition Width="*"></ColumnDefinition>
<!--2-->
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" Grid.Column="0" Margin="0,10,5,0">
<TextBlock x:Name="lblCID" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
<TextBox x:Name="txtCID" BorderBrush="Red" MaxLength="8" TextChanged="txtCID_TextChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch" Margin="0,10,0,0" TextWrapping="Wrap" FontSize="20"></TextBox>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" Margin="5,10,5,0">
<TextBlock x:Name="lblAlias" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
<TextBox x:Name="txtAlias" BorderBrush="Red" TextChanged="txtAlias_TextChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch" Margin="0,10,0,0" TextWrapping="Wrap" FontSize="20"></TextBox>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="2" Margin="5,10,0,0">
<TextBlock x:Name="lblPIN" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
<PasswordBox x:Name="txtPIN" BorderBrush="Red" PasswordChanged="txtPIN_PasswordChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch" Margin="0,10,0,0" FontSize="20"></PasswordBox>
</StackPanel>
</Grid>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="0,10,0,0">
<TextBlock x:Name="lblErrore" Visibility="Collapsed" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="14" Foreground="Red" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,5,0,0"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="4" Margin="0,10,0,0" Grid.Column="0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<!--0-->
<ColumnDefinition Width="*"></ColumnDefinition>
<!--1-->
</Grid.ColumnDefinitions>
<Button x:Name="btnAnnulla" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0,30,10,0" VerticalAlignment="Top" Click="btnAnnulla_Click"></Button>
<Button x:Name="btnConfFirmaSupProd" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,30,0,0" VerticalAlignment="Top" Click="btnConfFirmaSupProd_Click"></Button>
<Button x:Name="btnConfFirmaOperatore" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,30,0,0" VerticalAlignment="Top" Click="btnConfFirmaOperatore_Click"></Button>
</Grid>
</StackPanel>
</Grid>
This should be an issue related to the old SDK that when the app is targeting old versions like 14393.
If you really want to achieve this behavior on all versions of Windows 10. I'd suggest you handle the showing and hiding events manually via InputPane.Showing Event and InputPane.Hiding Event. And then you might need to customize a PopUp control that looks like a ContentDialog. We can't change the position of the ContentDialog, because it is handled by the system. But we could change the position of a PopUp control.
When the on-screen keyboard is shown, the showing event will be fired and you could try to manually move your PopUp control upwards. When the keyboard is hidden, just move the PopUp control back to its original position.
Here is the sample code about how to handle the show and hide event:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InputPane currentInputPane = InputPane.GetForCurrentView();
// Subscribe to Showing/Hiding events
currentInputPane.Showing += OnShowing;
currentInputPane.Hiding += OnHiding;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
InputPane currentInputPane = InputPane.GetForCurrentView();
// Unsubscribe from Showing/Hiding events
currentInputPane.Showing -= OnShowing;
currentInputPane.Hiding -= OnHiding;
}
void OnShowing(InputPane sender, InputPaneVisibilityEventArgs e)
{
//do something
}
void OnHiding(InputPane sender, InputPaneVisibilityEventArgs e)
{
//do something
}
You could also get information about the touch keyboard here: Respond to the presence of the touch keyboard
I have a grid with an tap event and in this grid three more buttons with click event. The first (the middle) button is working, but the others are not. If I click on it the grid event is fired instead of the button event. And i didn't know why?
<Page
....
<Grid Background="{ThemeResource BackgroundBrush}" Tapped="Grid_Tapped">
<Grid.RowDefinitions>
<RowDefinition Height="130"/>
<RowDefinition Height="80"/>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
<RowDefinition Height="90"/>
</Grid.RowDefinitions>
<Border Grid.Row="2" Background="#BEBEBE"/>
<Border Grid.Row="3" Background="#BEBEBE"/>
....
<Button x:Name="btnGoToContent" Content="" HorizontalAlignment="Center" VerticalAlignment="Bottom" Grid.Row="3" Margin="0,0,0,20" Height="80" Width="320" Click="btnGoToContent_Click" Style="{StaticResource ButtonStyle2}" Opacity="0.7"/>
<Button x:Name="btnDiashowBack" Content="" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="3" Margin="0,0,0,0" Height="220" Width="220" Click="btnDiashowBack_Click" Style="{StaticResource BackButtonStyle}" Opacity="0.7"/>
<Button x:Name="btnDiashowNext" Content="" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="3" Margin="0,0,0,0" Height="220" Width="220" Click="btnDiashowNext_Click" Style="{StaticResource BackButtonMirror}" Opacity="0.7"/>
</Grid>
I don't know why it isn't working. But I've solved it with this Workaround. Skip the event in the grid event handler
private async void Grid_Tapped(object sender, TappedRoutedEventArgs e) {
if (e.OriginalSource.GetType() == typeof(TextBlock))
{
return;
}
.......
}
I am learning the WPF 4.5 Unleashed book. When I tried to incorporate Mouse Action Event such as MouseEnter or MouseDoubleClick for the Button by typing the code manually, the compiler told me it could not find the reference for the Mouse Action Event. However, when I use the double Tab shortcut, everything works well. What could possibly be the issue? I have bold the trouble code below.
<Window x:Class="WpfApplicationLearning0001.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="513.265" Width="748.469">
<DockPanel>
<Menu DockPanel.Dock="Top">
</Menu>
<StackPanel Name="ButtonBar" Orientation="Horizontal" DockPanel.Dock="Right">
<StackPanel.LayoutTransform>
<RotateTransform Angle="90">
</RotateTransform>
</StackPanel.LayoutTransform>
**<Button Name="Panel1Button" MouseEnter="Panel1Button_MouseEnter">
Toolbox
</Button>**
</StackPanel>
<Grid Background="White" Margin="0,0,2,3">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="Black" Foreground="White" HorizontalContentAlignment="Center" Content= "Start Page"/>
<GroupBox Grid.Row="1" Grid.Column="0" Background="White" Foreground="Black" Header="Start" />
<GroupBox Grid.Row="2" Grid.Column="0" Background="White" Foreground="Black" Header="Recent" />
<GroupBox Grid.Row="3" Grid.Column="0" Background="White" Foreground="Black" Header="Option" />
<GroupBox Grid.Row="1" Grid.Column="1" Grid.RowSpan="3" Background="White" Foreground="Black" Header="Get Start">
<ListBox>
<ListBoxItem>Article number1</ListBoxItem>
<ListBoxItem>Article number2</ListBoxItem>
<ListBoxItem>Article number3</ListBoxItem>
<ListBoxItem>Article number4</ListBoxItem>
</ListBox>
</GroupBox>
</Grid>
</DockPanel>
The double click not only puts it in the XAML code, put it also creates the code behind in the .cs file of:
private void Panel1Button_MouseEnter(object sender, MouseEventArgs e)
{
}
Until you add the code behind yourself, if you are doing it manually, then the error is correct. In addition, when manually coding be sure to include (object sender, MouseEventArgs e) in the function call or it may not recognize it as a valid function call to the MouseEvent.
i have a pop up and i want to close it when i tap anywhere outside the pop up. i searched and everyone advised me to use the property IsLightDismissEnabled; however if i touch outside, it will only remove the pop oup leaving everything inactive with a grey like screen as if it doesnt close the pop up completely
this is my code snippet:
<Popup x:Name="logincontroler" IsOpen="False" Margin="0,190,896,276" IsLightDismissEnabled="True">
<StackPanel Height="300" Width="470" x:Name="popup" FlowDirection="RightToLeft">
<Grid Width="470" Background="White" >
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<RichEditBox Grid.Row="1" Height="250" TextWrapping="Wrap" FontSize="20" Name="notesPopupTextBox" FlowDirection="LeftToRight"/>
<StackPanel Grid.Row="0" Orientation="Horizontal" Background="#FFE3E3E5">
<Button Name="CanclePopupButton" Content="Cancel" Width="64" Height="64" Click="CanclePopupButton_Click" />
<Button Name="ClearNotePopupButton" Content="Clear" Width="64" Height="64" Click="ClearNotePopupButton_Click" />
<Button Name="saveNoteButton" Content="Save" Width="64" Height="64" Click="saveNoteButton_Click" />
<TextBlock FontWeight="Medium" FontSize="40" Foreground="#2a2a86" Margin="170 12 0 0">Note</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Popup>
this is my code for the events
private void ShowButton_Click(object sender, RoutedEventArgs e)
{
logincontroler.IsOpen = true;
flipView1.IsEnabled = false;
}
private void CanclePopupButton_Click(object sender, RoutedEventArgs e)
{
logincontroler.IsOpen = false;
flipView1.IsEnabled = true;
}
Am I missing anything?
thank you in advance
Are you sure you don't have some other code in the App that you are not showing us?
There should be no Gray box behind the Popup.
I have just tested your code on an empty Windows 8.1 (XAML+C#) App and it works fine.
Try creating and Blank Windows 8.1 App and make you MainPage like this:
MainPage.xaml
<Page
x:Class="App19.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App19"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
RequestedTheme="Light">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Popup x:Name="logincontroler" IsOpen="False" Margin="0,190,896,276" IsLightDismissEnabled="True">
<StackPanel Height="320" Width="470" x:Name="popup" FlowDirection="RightToLeft">
<Grid Width="470" Background="BurlyWood" >
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<RichEditBox Grid.Row="1" Height="250" TextWrapping="Wrap" FontSize="20" Name="notesPopupTextBox" FlowDirection="LeftToRight"/>
<StackPanel Grid.Row="0" Orientation="Horizontal" Background="#FFE3E3E5">
<Button Name="CanclePopupButton" Content="Cancel" Width="64" Height="64" />
<Button Name="ClearNotePopupButton" Content="Clear" Width="64" Height="64" />
<Button Name="saveNoteButton" Content="Save" Width="64" Height="64" />
<TextBlock FontWeight="Medium" FontSize="40" Foreground="#2a2a86" Margin="170 12 0 0">Note</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Popup>
<Button Content="Show Popup" HorizontalAlignment="Left" Margin="692,260,0,0" VerticalAlignment="Top" Click="ShowButton_Click"/>
</Grid>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App19
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ShowButton_Click(object sender, RoutedEventArgs e)
{
logincontroler.IsOpen = true;
}
}
}
This has to work.
And comparing this with your solution should help you find the problem. If not, just edit your question with more information. (more code)
Note: I removed the click events from your popup, they were not needed to exemplify your problem, right?
In my Windows Store app (c#) i have Popup:
<Popup x:Name="LoginPopup" HorizontalAlignment="Center" VerticalAlignment="Center" Width="400" Height="300" IsOpen="{Binding Path=LoginPopupIsOpen}">
<Popup.ChildTransitions>
<TransitionCollection>
<PopupThemeTransition />
</TransitionCollection>
</Popup.ChildTransitions>
</Popup>
And when Popup IsOpen I need handle events only on Popup and freeze all another UI (including AppBar). It is possible without creating full screen popup with little work area?
You can do it in the xaml.cs file by using two properties.
In the event handler for the popup you are creating you can include the following two lines
this.IsEnabled = false;
this.ApplicationBar.IsVisible = false;
in the event handler where you want to close the popup, you can revert the properties back to their original values.
this.IsEnabled = true;
this.ApplicationBar.IsVisible = true;
I was also facing similar problem and used this to handle that.
private void AlertMessage_Opened(object sender, object e)
{
UIBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
private void AlertMessage_Closed(object sender, object e)
{
UIBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
My popup name was AlertMessage and I attack opened and closed events with it and place a border in xaml which covers whole screen, and handle is visibility through these events.
<Border Name="UIBlock" Grid.Row="0" Grid.RowSpan="3" Background="#AAFFFFFF" Visibility="Collapsed">
</Border>
And remember to place this border before popup
I have made small popup using below code.Please try this.
<Grid Background="Black" Opacity="0.7" Visibility="Collapsed" x:Name="gdalert" Height="{Binding Height, ElementName=gdfullpage}" Width="{Binding Width, ElementName=gdfullpage}">
<Popup x:Name="settingpopup" Width="350" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
<Border x:Name="settingbdrmain" Grid.Row="1" BorderThickness="0" Width="350" CornerRadius="15" >
<Grid x:Name="gdsubshape" Margin="0,10,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="0,0,0,0" Grid.Row="0" x:Name="dddf" FontSize="20" Text="" HorizontalAlignment="Center" TextAlignment="Center" FontFamily="Arial" FontWeight="Bold" TextWrapping="Wrap" ></TextBlock>
<TextBlock x:Name="txtsettingalert" Text="" FontSize="20" TextAlignment="Center" Width="300" FontFamily="Arial" TextWrapping="Wrap" Foreground="Black" Grid.Row="1" ></TextBlock>
<Border x:Name="settingbdr" Width="350" Grid.Row="2" Height="1" Background="Red" BorderBrush="Black" >
</Border>
<Grid x:Name="btnpanel" Grid.Row="3" Height="60">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto">/ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="settingok" Grid.Column="0" Height="50" HorizontalAlignment="Left"VerticalAlignment="Center" MinHeight="20" Width="170" Content="OK" FontSize="24" Foreground="Green" ></Button>
<Border x:Name="settingsubbdr" Grid.Column="1" BorderBrush="Green" Height="Auto" Width="1" ></Border>
<Button x:Name="sl" Grid.Column="2" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" MinHeight="20" Width="170" Content="Cancel" FontSize="24" Foreground="Green" ></Button>
</Grid>
</Grid>
</Border>
</Popup>
To Open Use :- popname.IsOpen = true; gdalert.Visibility = Visibility.Visible;
To Close popname.IsOpen = false; gdalert.Visibility = Visibility.Collapse;