不要生我的气,我很业余,但我已经为此工作了很长时间,但我无法弄清楚。我一直在研究这个 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();
}
}