简体   繁体   English

Uwp MenuBaritem click 事件 Makes nothing happen

[英]Uwp MenuBaritem click event Makes nothing happen

Don't get mad at me, I'm very amateur but I have been working on this for a long time and I can not figure it out.不要生我的气,我很业余,但我已经研究了很长时间,但我无法弄清楚。 I have been working on this uwp text editor and I have added a MenuBar with some items on it.我一直在研究这个 uwp 文本编辑器,我添加了一个带有一些项目的菜单栏。 I'm trying to get the open button to make a FileOpenPicker pop up but whatever I do it does not work.我试图让打开按钮弹出 FileOpenPicker,但无论我做什么都不起作用。 Please help!请帮忙!

Example.xaml例子.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* *例子.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(); } }

You have to add the event into your MainPage.xaml code otherwise your function OpenButton_Tapped wont get called:您必须将该事件添加到您的 MainPage.xaml 代码中,否则您的 function OpenButton_Tapped 将不会被调用:

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

And then Visual Studio will automatically add a function into your MainPage.xaml.cs which should look like this:然后 Visual Studio 会自动将 function 添加到您的 MainPage.xaml.cs 中,它应该如下所示:

private void OpenButton_Click(object sender, RoutedEventArgs e)
{

}

In this function you can add your OpenFilePicker:在此 function 中,您可以添加您的 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();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM