첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.02MB

▶ MainPage.xaml

<Page x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Grid
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="50"   />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Column="0"
                Spacing="10">
                <CommandBar Name="commandBar"
                    IsOpen="False"
                    DefaultLabelPosition="Right">
                    <AppBarButton Name="addButton"
                        Icon="Add"
                        Label="추가"
                        Click="appBarButton_Click" />
                    <AppBarButton Name="editButton"
                        Icon="Edit"
                        Label="편집"
                        Click="appBarButton_Click" />
                    <AppBarButton Name="shareButton"
                        Icon="Share"
                        Label="공유" Click="appBarButton_Click" />
                    <CommandBar.SecondaryCommands>
                        <AppBarButton Name="settingsButton"
                            Icon="Setting"
                            Label="설정"
                            Click="appBarButton_Click">
                            <AppBarButton.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="Control" Key="I" />
                            </AppBarButton.KeyboardAccelerators>
                        </AppBarButton>
                    </CommandBar.SecondaryCommands>
                </CommandBar>
                <TextBlock Name="textBlock"
                    Margin="0 10 0 0" />
            </StackPanel>
            <StackPanel Grid.Column="2"
                Spacing="10">
                <TextBlock Text="표시/닫기" />
                <Button Content="보조 명령바 표시"
                    Click="openButton_Click" />
                <Button Content="보조 명령바 닫기"
                    Click="closeButton_Click" />
                <TextBlock Text="컨텐트 수정" />
                <Button Content="보조 명령 추가"
                    Click="addSecondaryCommandButton_Click" />
                <Button Content="보조 명령 제거"
                    Click="removeSecondaryCommandButton_Click" />
            </StackPanel>
        </Grid>
    </Grid>
</Page>

 

728x90

 

▶ MainPage.xaml.cs

using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace TestProject
{
    /// <summary>
    /// 메인 페이지
    /// </summary>
    public sealed partial class MainPage : Page
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainPage()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            #region 윈도우 크기를 설정한다.

            double width  = 800d;
            double height = 600d;

            double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi;

            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi);

            ApplicationView.PreferredLaunchViewSize = windowSize;

            Window.Current.Activate();

            ApplicationView.GetForCurrentView().TryResizeView(windowSize);

            #endregion
            #region 윈도우 제목을 설정한다.

            ApplicationView.GetForCurrentView().Title = "CommandBar 엘리먼트 사용하기";

            #endregion

            this.addButton.KeyboardAccelerators.Add
            (
                new KeyboardAccelerator()
                {
                    Key       = VirtualKey.A,
                    Modifiers = VirtualKeyModifiers.Control
                }
            );

            this.editButton.KeyboardAccelerators.Add
            (
                new KeyboardAccelerator()
                {
                    Key       = VirtualKey.E,
                    Modifiers = VirtualKeyModifiers.Control
                }
            );

            this.shareButton.KeyboardAccelerators.Add
            (
                new KeyboardAccelerator()
                {
                    Key = VirtualKey.F4
                }
            );
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 앱바 버튼 클릭시 처리하기 - appBarButton_Click(sender, e)

        /// <summary>
        /// 앱바 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void appBarButton_Click(object sender, RoutedEventArgs e)
        {
            AppBarButton button = sender as AppBarButton;

            this.textBlock.Text = $"클릭했습니다 : {button.Label}";
        }

        #endregion
        #region 보조 명령바 표시 버튼 클릭시 처리하기 - openButton_Click(sender, e)

        /// <summary>
        /// 보조 명령바 표시 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void openButton_Click(object sender, RoutedEventArgs e)
        {
            this.commandBar.IsOpen   = true;
            this.commandBar.IsSticky = true;
        }

        #endregion
        #region 보조 명령바 닫기 버튼 클릭시 처리하기 - closeButton_Click(sender, e)

        /// <summary>
        /// 보조 명령바 닫기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            this.commandBar.IsOpen   = false;
            this.commandBar.IsSticky = false;
        }

        #endregion
        #region 보조 명령 추가 버튼 클릭시 처리하기 - addSecondaryCommandButton_Click(sender, e)

        /// <summary>
        /// 보조 명령 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addSecondaryCommandButton_Click(object sender, RoutedEventArgs e)
        {
            if(this.commandBar.SecondaryCommands.Count == 1)
            {
                AppBarButton newButton = new AppBarButton
                {
                    Icon  = new SymbolIcon(Symbol.Add),
                    Label = "버튼 1"
                };

                newButton.KeyboardAccelerators.Add
                (
                    new KeyboardAccelerator()
                    {
                        Key       = VirtualKey.N,
                        Modifiers = VirtualKeyModifiers.Control
                    }
                );

                this.commandBar.SecondaryCommands.Add(newButton);

                newButton = new AppBarButton
                {
                    Icon  = new SymbolIcon(Symbol.Delete),
                    Label = "버튼 2"
                };

                this.commandBar.SecondaryCommands.Add(newButton);

                newButton.KeyboardAccelerators.Add
                (
                    new KeyboardAccelerator()
                    {
                        Key = VirtualKey.Delete
                    }
                );

                this.commandBar.SecondaryCommands.Add(new AppBarSeparator());

                newButton = new AppBarButton
                {
                    Icon  = new SymbolIcon(Symbol.FontDecrease),
                    Label = "버튼 3"
                };

                newButton.KeyboardAccelerators.Add
                (
                    new KeyboardAccelerator()
                    {
                        Key       = VirtualKey.Subtract,
                        Modifiers = VirtualKeyModifiers.Control
                    }
                );

                this.commandBar.SecondaryCommands.Add(newButton);

                newButton = new AppBarButton
                {
                    Icon  = new SymbolIcon(Symbol.FontIncrease),
                    Label = "버튼 4"
                };

                newButton.KeyboardAccelerators.Add
                (
                    new KeyboardAccelerator()
                    {
                        Key       = VirtualKey.Add,
                        Modifiers = VirtualKeyModifiers.Control
                    }
                );

                this.commandBar.SecondaryCommands.Add(newButton);
            }
        }

        #endregion
        #region 보조 명령 제거 버튼 클릭시 처리하기 - removeSecondaryCommandButton_Click(sender, e)

        /// <summary>
        /// 보조 명령 제거 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void removeSecondaryCommandButton_Click(object sender, RoutedEventArgs e)
        {
            RemoveSecondaryCommand();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 보조 명령 제거하기 - RemoveSecondaryCommand()

        /// <summary>
        /// 보조 명령 제거하기
        /// </summary>
        private void RemoveSecondaryCommand()
        {
            while(this.commandBar.SecondaryCommands.Count > 1)
            {
                this.commandBar.SecondaryCommands.RemoveAt(this.commandBar.SecondaryCommands.Count - 1);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , , ,

댓글을 달아 주세요