10 April 2017

Windows Store và File: Tạo Folder và Bindding hiển thị Folder lên ComboBox trên App Windows Store C#

MainPage.xaml
Windows Store 2017
<Page
    x:Class="CaptureDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CaptureDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBox x:Name="tb_folder_name" HorizontalAlignment="Left" Margin="305,250,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="32" Width="190" FontSize="20"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="40" Margin="105,250,0,0" TextWrapping="Wrap" Text="Folder name:" VerticalAlignment="Top" Width="155" FontSize="24"/>
        <Button x:Name="btn_create" Content="Create new Folder" HorizontalAlignment="Left" Margin="567,247,0,0" VerticalAlignment="Top" Click="btn_create_Click"/>
        <ComboBox x:Name="comboBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="305,357,0,0" VerticalAlignment="Top" Width="190" Height="32"/>
        <Button x:Name="btn_show" Content="Get folder name" HorizontalAlignment="Left" Margin="567,357,0,0" VerticalAlignment="Top" Click="btn_show_Click"/>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="105,360,0,0" TextWrapping="Wrap" Text="Folder name:" VerticalAlignment="Top" FontSize="24"/>
    </Grid>

</Page>
Cấp quyền vào thư mục Pricture Libarary
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.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Storage;
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 CaptureDemo
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void btn_create_Click(object sender, RoutedEventArgs e)
        {
            //Tao folder o PicturesLibrary
            StorageFolder appFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync(tb_folder_name.Text, CreationCollisionOption.OpenIfExists);
            var dialog = new MessageDialog("Tao Folder: " + tb_folder_name.Text + " thanh cong");
            await dialog.ShowAsync();
        }

        private async void btn_show_Click(object sender, RoutedEventArgs e)
        {
            //Tao List add name folder
            List<String> list = new List<String>();
            StorageFolder folder = KnownFolders.PicturesLibrary;
            IReadOnlyList<IStorageItem> items = await folder.GetItemsAsync();
            foreach (var item in items)
            {
                if (item is StorageFolder)
                {
                    list.Add(item.Name);
                }

            }
            //Bindding name folder vao ComboBox
            comboBox.DataContext = list;
        }
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang