Опубликован: 01.11.2011 | Доступ: свободный | Студентов: 1424 / 63 | Оценка: 3.84 / 3.44 | Длительность: 15:38:00
Специальности: Программист
Практическая работа 8:

Навигация между страницами с помощью Silverlight

Упражнение 16.2. Навигация с помощью кнопок. Математические функции

В данной работе навигацию между xaml-документами мы будем осуществлять с помощью элемента управления Button.

Создаем новый проект MS Windows Phone.

Код MainPage.xaml:

<phone:PhoneApplicationPage 
    x:Class="p8_2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Навигация" 
              Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Математика" Margin="9,-7,0,0" 
             Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Name="square_root" Click="square_root_Click" Margin="0,0,0,514"
              Content="Решение квадратного уравнения"></Button>
            <Button Name="number_order" Click="number_order_Click" Margin="0,74,0,440" C
             ontent="Нахождение порядка числа
                    "></Button>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>
    

Код MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace p8_2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void square_root_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/SquareRoot.xaml", UriKind.Relative));
        }

        private void number_order_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/NumberOrder.xaml", UriKind.Relative));
        }

    }
}
    

Добавляем два xaml-документа: SquareRoot.xaml и NumberOrder.xaml. Первый документ решает квадратное уравнение, второй - определяет порядок вводимого числа.

SquareRoot.xaml (Основной фрагмент)

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Решение квадратного уравнения" 
              Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Математика" Margin="9,-7,0,0" 
              Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,132">
            <TextBox Name="txtA" Margin="0,6,0,400" />
            <TextBox Name="txtB" Margin="0,62,0,344" />
            <TextBox Name="txtC" Margin="0,118,0,288"  />
            <Button Name="bttnCalculate" Click="bttnCalculate_Click" 
              Content="Рассчитать!" Margin="0,199,0,190" />
            <ContentControl Name="cnt1" Margin="0,291,0,0" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>
    

SquareRoot.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace p8_2
{
    public partial class SquareRoot : PhoneApplicationPage
    {
        public SquareRoot()
        {
            InitializeComponent();
        }

        private void bttnCalculate_Click(object sender, RoutedEventArgs e)
        {
            double a, b, c, d, x1, x2;
            string str;
            a = System.Convert.ToDouble(txtA.Text);
            b = System.Convert.ToDouble(txtB.Text);
            c = System.Convert.ToDouble(txtC.Text);
            d = Math.Pow(b, 2) - 4 * a * c;

            if (d < 0) { str = "Действительных корней нет!"; }
            else
            {
                x1 = (-b - Math.Sqrt(d)) / (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                str = "x1 = " + x1 + "\nx2 = " + x2;
            }
            cnt1.Content = str;

        }

    }
}
    
Листинг .

NumberOrder.xaml (основной фрагмент)

<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Нахождение порядка числа" 
              Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Математика" Margin="9,-7,0,0" 
              Style="{StaticResource PhoneTextTitle1Style}" FontSize="72" />
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Name="txtNumber" Margin="0,6,0,519" />
            <Button Name="bttnCalculate" Click="bttnCalculate_Click" 
              Content="Рассчитать!" Margin="-6,105,6,423" />
            <ContentControl Name="cnt1" Margin="0,200,0,0" />
        </Grid>
    </Grid>
    

NumberOrder.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace p8_2
{
    public partial class NumberOrder : PhoneApplicationPage
    {
        public NumberOrder()
        {
            InitializeComponent();
        }

        private void bttnCalculate_Click(object sender, RoutedEventArgs e)
        {
            Int64 number, order;
            string str;
            number = System.Convert.ToInt64(txtNumber.Text);
            order = 0;
            str = "Число: " + number;
            while (number > 0)
            {
                order++;
                number /= 10;
            };
            str += "\nПорядок величины: " + order;
            cnt1.Content = str;

        }
    }
}
    
Листинг .