Reputation: 11
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. I'm trying to get the open button to make a FileOpenPicker pop up but whatever I do it does not work. Please help!
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>
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();
}
}
Upvotes: 0
Views: 250
Reputation: 1450
You have to add the event into your MainPage.xaml code otherwise your function OpenButton_Tapped wont get called:
<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:
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
}
In this function you can add your 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();
}
}
Upvotes: 0