0

不要生我的气,我很业余,但我已经为此工作了很长时间,但我无法弄清楚。我一直在研究这个 uwp 文本编辑器,并添加了一个带有一些项目的 MenuBar。我试图让打开按钮弹出 FileOpenPicker 但无论我做什么都不起作用。请帮忙!

Example.xaml

    <Grid Margin="10,10,10,10">
        <Grid.RowDefinitions>
            <RowDefinition Height="396*"/>
            <RowDefinition Height="0*"/>
            <RowDefinition Height="661*"/>
        </Grid.RowDefinitions>





        <MenuBar Background="White" Foreground="Black" FocusVisualPrimaryBrush="White" Height="40" VerticalAlignment="Top">
            <MenuBarItem x:Name="File" Title="File" Background="White" Foreground="White" BorderBrush="#66FFFFFF" FocusVisualSecondaryBrush="#991F1F1F" RequestedTheme="Light">
                <MenuFlyoutSubItem Text="New">
                    <MenuFlyoutItem x:Name="Plaintext" Text="Plain Text Document"/>
                    <MenuFlyoutItem x:Name="WordDoc" Text="Word Document"/>
                </MenuFlyoutSubItem>
                <MenuFlyoutItem x:Name="SaveButton" Text="Save"/>
                <MenuFlyoutItem x:Name="OpenButton" Text="Open..."/>
            </MenuBarItem>
            <MenuBarItem x:Name="Edit" Title="Edit" RequestedTheme="Light">
                <MenuFlyoutItem x:Name="Undo" Text="Undo"/>
                <MenuFlyoutItem x:Name="Cut" Text="Cut"/>
                <MenuFlyoutItem x:Name="Copy" Text="Copy"/>
                <MenuFlyoutItem x:Name="Paste" Text="Paste"/>
            </MenuBarItem>
            <MenuBarItem Title="Format" RequestedTheme="Light"/>
        </MenuBar>
        <TextBox x:Name="j" Margin="-10,45,0,0" TextWrapping="Wrap" Text="" RequestedTheme="Light" Grid.RowSpan="3"/>


    </Grid>

*example.cs*

namespace example
{



    public sealed partial class example : Page
    {
        public Txt()
        {
            this.InitializeComponent();
        }





        private async void OpenButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".txt");
            openPicker.FileTypeFilter.Add(".docx");
            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                var txt = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                j.Text = txt.ToString();

            }
        }
4

1 回答 1

0

您必须将事件添加到 MainPage.xaml 代码中,否则您的函数 OpenButton_Tapped 不会被调用:

<MenuFlyoutItem x:Name="OpenButton" Click="OpenButton_Click" Text="Open..."/>

然后 Visual Studio 会自动将一个函数添加到您的 MainPage.xaml.cs 中,该函数应如下所示:

private void OpenButton_Click(object sender, RoutedEventArgs e)
{

}

在此函数中,您可以添加您的 OpenFilePicker:

private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".txt");
    openPicker.FileTypeFilter.Add(".docx");
    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
        var txt = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        j.Text = txt.ToString();

    }
}
于 2022-02-26T17:37:17.690 回答