15 March 2017

EXAMPLE: AppBar, MenuBar, Lấy và hiển thị tên File, Tên Forder trên Windows Store Apps (C #)

- Tạo giao diện và các hàm thực thi chuyển qua Page thứ 2
MainPage.xaml
Windows Store 2017
<Page
    x:Class="Assignment.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Assignment"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.TopAppBar>
        <AppBar Background="#5cbce4" IsOpen="True" IsSticky="True">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <AppBarButton Icon="Home" Label="Home" Name="abbHome" ></AppBarButton>
                    <AppBarSeparator></AppBarSeparator>
                    <AppBarButton Icon="Calendar" Label="Calendar" Name="abbCalendar" ></AppBarButton>
                    <AppBarButton Icon="Mail" Label="Mail" Name="abbMail" ></AppBarButton>
                    <AppBarButton Icon="AllApps" Label="Layout">
                        <AppBarButton.Flyout>
                            <Flyout>
                                <StackPanel>
                                    <MenuFlyoutItem Text="Grid"></MenuFlyoutItem>
                                    <MenuFlyoutSeparator></MenuFlyoutSeparator>
                                    <MenuFlyoutItem Text="StackPanel"></MenuFlyoutItem>
                                    <MenuFlyoutSeparator></MenuFlyoutSeparator>
                                    <MenuFlyoutItem Text="Canvas"></MenuFlyoutItem>
                                </StackPanel>
                            </Flyout>
                        </AppBarButton.Flyout>
                    </AppBarButton>
                </StackPanel>
                <SearchBox Foreground="Red" HorizontalAlignment="Right" Width="300" Height="50"></SearchBox>
            </Grid>
        </AppBar>
    </Page.TopAppBar>

    <Page.BottomAppBar>
        <CommandBar Background="#5cbce4" IsSticky="True" IsOpen="True">
            <CommandBar.PrimaryCommands>
                <AppBarButton Icon="Refresh" Label="Refresh" Name="abbRefresh"></AppBarButton>
                <AppBarButton Icon="Help" Label="Help" Name="abbHelp"></AppBarButton>
                <AppBarSeparator></AppBarSeparator>
                <AppBarToggleButton Icon="Like" Label="Like" Name="abtbLike" Checked="abtbLike_Checked" Unchecked="abtbLike_Unchecked"></AppBarToggleButton>
            </CommandBar.PrimaryCommands>

            <CommandBar.SecondaryCommands>
                <AppBarButton Icon="Add" Label="Add" Name="abbAdd"></AppBarButton>
                <AppBarButton Icon="Edit" Label="Edit" Name="abbEdit"></AppBarButton>
                <AppBarButton Icon="Remove" Label="Remove" Name="abbRemove"></AppBarButton>
                <AppBarButton Icon="Delete" Label="Delete" Name="abbDelete"></AppBarButton>
            </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <HyperlinkButton Content="Click here go to my Page 1" FontSize="22" HorizontalAlignment="Left" Height="98" Margin="59,152,0,0" VerticalAlignment="Top" Width="418" Click="HyperlinkButton_Click" />
        <TextBlock x:Name="tbLike" HorizontalAlignment="Left" Margin="610,385,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" FontSize="24"/>

    </Grid>

</Page>
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Assignment
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BasicPage));

        }

        private void abtbLike_Checked(object sender, RoutedEventArgs e)
        {
             //TODO
        }

        private void abtbLike_Unchecked(object sender, RoutedEventArgs e)
        {
             //TODO
        }
    }
}
- Tạo mới page 1
BasicPage.xaml
Windows Store 2017
<Page
    x:Name="pageRoot"
    x:Class="Assignment.BasicPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Assignment"
    xmlns:common="using:Assignment.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
        <x:String x:Key="AppName">My Application</x:String>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>
            <TextBlock x:Name="pageTitle" Text="My First Page" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
        </Grid>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="117,105,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Select A file" VerticalAlignment="Top" FontSize="24"/>
        <Button x:Name="button" Content="Pick Photo" HorizontalAlignment="Left" Margin="114,167,0,0" Grid.Row="1" VerticalAlignment="Top" FontSize="24" Click="button_Click"/>
        <Button x:Name="button1" Content="List All Files and Foders" HorizontalAlignment="Left" Margin="315,167,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0,-0.632" FontSize="24" Click="button1_Click"/>
        <TextBlock x:Name="tbShowImges" HorizontalAlignment="Left" Margin="117,265,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="305" Width="435" FontSize="20"/>
    </Grid>
</Page>
BasicPage.xaml.cs
Windows Store 2017
using Assignment.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


namespace Assignment
{

    public sealed partial class BasicPage : Page
    {

        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }


        public BasicPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            this.navigationHelper.SaveState += navigationHelper_SaveState;
        }

        
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }

        
        private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }

        #region NavigationHelper registration


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        FileOpenPicker picker = new FileOpenPicker();

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".jpg");
            StorageFile file = await picker.PickSingleFileAsync();

            //OK Button
            MessageDialog msgDialog = new MessageDialog(file.Name);

            //Show message
            msgDialog.ShowAsync();


        }
        private void OkBtnClick(IUICommand command)
        {
            //TODO
        }

        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            StorageFolder folder = KnownFolders.DocumentsLibrary;
            IReadOnlyList<IStorageItem> items = await folder.GetItemsAsync();
            String OutFile= "File:\n";
            String OutFoder = "Foder:\n";
            foreach (var item in items)
            {
                if (item is StorageFolder)
                {
                    OutFoder = OutFoder + item.Name + "\n";

                }
                else
                {
                    OutFile = OutFile + item.Name + "\n";
                }
            }

            tbShowImges.Text = OutFile +"\n"+ OutFoder;
        }
    }
}
RUN DEMO

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang