how to make buttons accessible throughout the page in windows phone 7 - c#

I have a few buttons which i want to use throughout my application.
My xaml is:
<Button Content="" Grid.Row="1" Height="79" HorizontalAlignment="Left" Margin="9,643,0,0" Name="Home" VerticalAlignment="Top" Width="77" BorderThickness="0" Click="Home_Click" />
<Button Content="" Grid.Row="1" Height="74" HorizontalAlignment="Left" Margin="108,646,0,0" Name="Map1" VerticalAlignment="Top" Width="67" BorderThickness="0" Click="Map1_Click" />
<Button Content="" Grid.Row="1" Height="71" HorizontalAlignment="Left" Margin="198,649,0,0" Name="City1" VerticalAlignment="Top" Width="70" BorderThickness="0" Click="City1_Click" />
<Button Content="" Grid.Row="1" Height="74" HorizontalAlignment="Left" Margin="295,647,0,0" Name="Cuisine1" VerticalAlignment="Top" Width="74" BorderThickness="0" Click="Cuisine1_Click" />
<Button Content="" Grid.Row="1" Height="71" HorizontalAlignment="Left" Margin="398,649,0,0" Name="Share" VerticalAlignment="Top" Width="67" BorderThickness="0" Click="Share_Click" />
Can anyone please tell me how to use these buttons in all my pages without adding them all the time?

You can create a UserControl with all the buttons in it.
<UserControl x:Class="WPAppTests.ButtonsUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<!-- Buttons -->
</Grid>
</UserControl>
When you need to use this buttons, paste this code in your page:
xmlns:wpAppTests="clr-namespace:WPAppTests" <!-- Your own path to the UserControl -->
<wpAppTests:ButtonsUserControl />
I hope this will help!

Related

Black area to the right, inside window

A large black area appears on the right side of the window when I run the program, and I can't figure out why. It's not there in the preview on the XAML file page in Visual Studio 2017, and I ran it once on a Windows 7 computer, and the black area wasn't there (I'm primarily on Windows 10). The problem could possibly be in another file, but I cannot find it.
<Window x:Class="Project.Widget"
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:Project"
mc:Ignorable="d"
Title="Menu"
WindowStartupLocation="Manual"
ResizeMode="NoResize"
Width="109"
SizeToContent="WidthAndHeight"
WindowState="Minimized"
Background="#2D3A48"
Topmost="False"
Loaded="Window_Loaded"
Closing="Window_Closing" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="77"/>
<RowDefinition Height="28"/>
<RowDefinition Height="90"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Image x:Name="Icon"
Source="./Resources/Icon.png"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Height="77"
Width="109" />
<Label x:Name="labelUsername"
Grid.Row="1"
Content=""
FontSize="14"
Foreground="White"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" />
<Label x:Name="labelScore"
Grid.Row="2"
Content="0%, 0 of 0"
Foreground="White"
FontSize="14"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0,0,0,0" />
<Image x:Name="AvgScoreDot"
Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Height="50"
Source=".\Resources\yellow_dot.png"
Margin="0,32,0,0"/>
<Label x:Name="labelAvgScore"
Grid.Row="2"
Content="0.0"
Foreground="White"
FontSize="24"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0,35,0,0"
FontWeight="Bold" />
<Button x:Name="MailBtn"
Grid.Row="3"
Height="60"
Width="109"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderBrush="#FF2D3A48"
Click="Mail_Click" >
<Button.Background>
<ImageBrush
ImageSource="/Resources/mail.png"
Stretch="None" />
</Button.Background>
</Button>
<Button x:Name="MailBadgeBtn"
Grid.Row="3"
Content="0"
Foreground="White"
FontSize="11"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Height="20"
Width="20"
Margin="0,30,24,10"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Click="MailBadgeBtn_Click" BorderThickness="0">
<Button.Background>
<ImageBrush
ImageSource=".\Resources\red_dot.png" />
</Button.Background>
</Button>
<TextBlock x:Name="labelConnection"
Grid.Row="0"
Text=" No Connection or blocked by Firewall"
Foreground="Red"
Background="White"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Center"
Height="77"
Width="109"
HorizontalAlignment="Center" />
</Grid>
Here's an image of what I get when I run it:
Here's what I get when I remove SizeToContent="WidthAndHeight" and add a height of 300 (I also had to remove your event handlers since I don't have that code, and add garbage images to the Resources folder since I don't have the original images):
Is that more like what you were looking for? You can also choose to explicitly set SizeToContent to "Manual".

Buttons resize with page when i dont want them toWPF

Hello I added some buttons to my Main_Page.xaml and they resize with the window while running the application and i dont want that. I want them to stay the same proportion when i run the app.
Button names are: Exercise_tab_left_button, Exercise_tab_right_button
<Page x:Class="TrackFit_Project.Main_Page"
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:local="clr-namespace:TrackFit_Project"
mc:Ignorable="d"
d:DesignHeight="453.635" d:DesignWidth="855.207"
Title="Main_Page"
ShowsNavigationUI = "false" >
<TabControl Margin="10,10,10.333,10.333">
<TabItem Header="Exercise"
HorizontalAlignment="Left"
Width="58"
Height="22"
Margin="-2,-2,0,0"
VerticalAlignment="Top">
<Grid Background="#FFE5E5E5">
<Button x:Name="Exercise_Tab_Profile_Button" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Click="profileButtonClick">
<Image>
<Image.Source>stock profile image.png</Image.Source>
</Image>
</Button>
<Rectangle x:Name="Exercise_tab_rectangle" Fill="#FFCFC6C6" HorizontalAlignment="Left" Height="295" Margin="137,50,0,0" Stroke="Black" VerticalAlignment="Top" Width="625"/>
<TextBlock HorizontalAlignment="Left" Height="234" Margin="169,87,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="548"/>
**<Button x:Name="Exercise_tab_left_button" Content="<" Margin="148,189,655,154" FontSize="18" FontWeight="Bold"/>
<Button x:Name="Exercise_tab_Right_button" Content=">" Margin="717,189,83,155" FontSize="18" FontWeight="Bold"/>**
<TextBlock x:Name="Log_Out_Text_Block" HorizontalAlignment="Left" Margin="10,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontWeight="Bold">
<Hyperlink x:Name="Log_out_hyperlink" Click="Log_out_hyperlink_Click">Log out</Hyperlink>
</TextBlock>
</Grid>
</TabControl>
You have to specify the properties Height and Width instead of setting these values indirectly using the Margin property.
Currently the Margin property determines the width and height of the buttons when they are arranged in the Grid. This can be useful when the UI is static and must not resize when the window is resized.
Assuming you want the UI to dynamically adapt to the size you should embed the Rectangle, TextBlock and the Buttons in a second Grid in a way like this (you might want to figure out the right values for the Margin and size of the buttons):
<Grid Margin="137,50,10,10">
<Rectangle x:Name="Exercise_tab_rectangle" Fill="#FFCFC6C6" Stroke="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<TextBlock HorizontalAlignment="Stretch" Height="234" Margin="32,37" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Stretch" />
<Button x:Name="Exercise_tab_left_button" Content="<" Margin="11" VerticalAlignment="Center" HorizontalAlignment="Left" Width="20" Height="50" FontSize="18" FontWeight="Bold"/>
<Button x:Name="Exercise_tab_Right_button" Content=">" Margin="11" VerticalAlignment="Center" HorizontalAlignment="Right" Width="20" Height="50" FontSize="18" FontWeight="Bold"/>
</Grid>

Scrolling with ScrollViewer causes App crash

In Windows 10 Universal app development the ScrollViewer causes App crashes. In the Windows dev Center this was also mentioned but not solved (https://social.msdn.microsoft.com/Forums/windowsapps/en-US/a737f407-d59f-4b10-a7d0-de9ead6d4f5f/uwpscrollviewer-occasional-crash?forum=wpdevelop).
Now my Problem:
I put a second grid on the Standard one of the xaml file. Then I added the ScrollViewer to this grid. After that i put another grid on the scrollviewer and then i added some Buttons to this grid. When scrolling the app crashes very often.
Do you have any idea how to fix that or how it can be done different?
Here the Code:
<Page
x:Class="ProjectMoralsKite.Datenbank"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ProjectMoralsKite"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grid">
<Grid.Background>
<ImageBrush Stretch="Fill"/>
</Grid.Background>
<Grid HorizontalAlignment="Left" Height="529" Margin="10,10,0,0" VerticalAlignment="Top" Width="340" BorderThickness="0,-1,0,0" Background="White" Opacity="0.6"/>
<TextBlock x:Name="textBlock" TextAlignment="Center" HorizontalAlignment="Left" Margin="6,70,0,0" TextWrapping="Wrap" Text="Verwaltung" VerticalAlignment="Top" Width="340" FontSize="29.333"/>
<Button x:Name="button" Content="Zurück" HorizontalAlignment="Left" Margin="10,507,0,0" VerticalAlignment="Top" Width="150" Click="button_Click"/>
<ScrollViewer HorizontalAlignment="Left" Height="380" Margin="10,109,0,0" VerticalAlignment="Top" Width="340">
<Grid HorizontalAlignment="Left" Height="655" VerticalAlignment="Top" Width="340">
<Button x:Name="button1" Content="Plan" HorizontalAlignment="Left" Margin="0,24,0,0" VerticalAlignment="Top" Height="54" Width="340"/>
<Button x:Name="button1_Copy" Content="Kunden" HorizontalAlignment="Left" Margin="0,83,0,0" VerticalAlignment="Top" Height="54" Width="340" Click="button1_Copy_Click"/>
<Button x:Name="button1_Copy1" Content="Baustellen" HorizontalAlignment="Left" Margin="0,142,0,0" VerticalAlignment="Top" Height="54" Width="340" Click="button1_Copy1_Click"/>
<Button x:Name="button1_Copy2" Content="Aufträge" HorizontalAlignment="Left" Margin="0,201,0,0" VerticalAlignment="Top" Height="54" Width="340" Background="#33000000" Click="button1_Copy2_Click"/>
<Button x:Name="button1_Copy3" Content="Zuweisung" HorizontalAlignment="Left" Margin="0,260,0,0" VerticalAlignment="Top" Height="54" Width="340" Background="#33FF0000"/>
<Button x:Name="button1_Copy4" Content="Rechteverwaltung" HorizontalAlignment="Left" Margin="0,319,0,0" VerticalAlignment="Top" Height="54" Width="340" Background="#33FF0000"/>
</Grid>
</ScrollViewer>
</Grid>
</Page>

Switching between landscape and portrait mode using wpf in tablet devices

How could we arrange the controls on a WPF window so that it would automatically align the controls while switching between view modes occur ie between landscape and portrait mode.
<Window x:Class="Custom_Track_Reports.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LoginWindow" >
<Viewbox Stretch="Fill">
<Grid Background="#FF58B0DF" Height="401">
<Rectangle Fill="#FFF0F3F4" Height="173" Margin="45,197,58,0" Stroke="Black" VerticalAlignment="Top" AllowDrop="True" />
<Image Height="125" Margin="340,10,330,0" VerticalAlignment="Top" Source="Images/logo_lrg.png"/>
<Label Content="Custom Track Reports" HorizontalAlignment="Left" Margin="287,147,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.607,1.112" Width="268" Height="47" FontFamily="Arial" FontSize="26" Foreground="White"/>
<TextBox Margin="128,0,0,110" TextWrapping="Wrap" VerticalAlignment="Bottom" Height="25" BorderThickness="1" FontFamily="Arial" FontSize="21" Text="" Name="userTextBox" FontStretch="Expanded" Opacity="1" HorizontalAlignment="Left" Width="546" AcceptsReturn="False" KeyDown="userTextBox_KeyDown" />
<Button x:Name="enterButton" Content="Enter" HorizontalAlignment="Right" Margin="0,311,166,0" VerticalAlignment="Top" Width="110" Height="26" FontSize="14" Style="{DynamicResource button}" Click="enterButton_Click"/>
<Label Content="User ID" FontSize="20" FontWeight="Bold" Height="40" HorizontalAlignment="Left" Margin="122,215,0,0" Name="label1" VerticalAlignment="Top" Width="105" />
</Grid>
</Viewbox>

Ispost back like functionality in WPF pages

I am building a WPF application which contains a large number of pages. I want to know whether there is any function which is similar to IsPostBack in Asp.Net. I just wanted to know is this the first time the specific page is loading. Thanks in advance.
xaml
<Window x:Class="Custom_Track_Reports.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LoginWindow" >
<Viewbox Stretch="Fill">
<Grid Background="#FF58B0DF" Height="401">
<Rectangle Fill="#FFF0F3F4" Height="173" Margin="45,197,58,0" Stroke="Black" VerticalAlignment="Top" AllowDrop="True" />
<Image Height="125" Margin="340,10,330,0" VerticalAlignment="Top" Source="Images/logo_lrg.png"/>
<Label Content="Custom Track Reports" HorizontalAlignment="Left" Margin="287,147,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.607,1.112" Width="268" Height="47" FontFamily="Arial" FontSize="26" Foreground="White"/>
<TextBox Margin="128,0,0,110" TextWrapping="Wrap" VerticalAlignment="Bottom" Height="25" BorderThickness="1" FontFamily="Arial" FontSize="21" Text="" Name="userTextBox" FontStretch="Expanded" Opacity="1" HorizontalAlignment="Left" Width="546" AcceptsReturn="False" KeyDown="userTextBox_KeyDown" />
<Button x:Name="enterButton" Content="Enter" HorizontalAlignment="Right" Margin="0,311,166,0" VerticalAlignment="Top" Width="110" Height="26" FontSize="14" Style="{DynamicResource button}" Click="enterButton_Click"/>
<Label Content="User ID" FontSize="20" FontWeight="Bold" Height="40" HorizontalAlignment="Left" Margin="122,215,0,0" Name="label1" VerticalAlignment="Top" Width="105" />
</Grid>
</Viewbox>
If your page navigation is taking place via Frame, then attach to the Frame.Navigated event. If you provide more detail (some XAML and code) then we might be able to be more helpful...

Categories