OpenFileDialog in Avalonia - Error with ShowAsync

自古美人都是妖i 提交于 2019-12-25 01:12:55

问题


Background:

I have been using Avalonia to develop a cross-platform UI. To learn it I am trying to creating a simple program that opens a .txt file using OpenFileDialog and displays its contents in a ComboBox in another window.

I am trying to write a method that opens file dialog and returns the path the user opened. I am using a button with a Click event to open file dialog and put the specified path in a TextBox for the user to see. Since I am new to both WPF and Avalonia, I am not sure how to proceed.

Problem:

I have created the UI view and a view model. The view model uses INotifyPropertyChanged to display the selected file path in a TextBox, which I have default set to the C drive.

Now I want to use OpenFileDialog to update that path with the path to the .txt file the user has selected. I found an example that demonstrates OpenFileDialog, but it doesn't seem to work for me and I don't really understand how it should work.

TxtView.xaml:

<TextBox Text="{Binding Path}" Margin="0 0 5 0" Grid.Column="0" IsReadOnly="True" />
<Button Content="Browse" Click="OnBrowseClicked" Margin="0 0 0 0" Grid.Column="1" />

TxtView.xaml.cs:

public async Task GetPath()
{
   var dialog = new OpenFileDialog();
   dialog.Filters.Add(new FileDialogFilter() { Name = "Text", Extensions = { "txt" } });
   dialog.AllowMultiple = true;

   var result = await dialog.ShowAsync();

   if (result != null)
   {
      await GetPath(result);
   }
}

public void OnBrowseClicked(object sender, RoutedEventArgs args)
{
   var context = this.DataContext as TxtViewModel;
   context.Path = "path";
}

TxtViewModel.cs

class TxtViewModel : INotifyPropertyChanged
{
   private string _path;

   public string Path
   {
      get => _path;
      set
      {
         if (value != _path)
         {
            _path = value;
            OnPropertyChanged();
         }
      }
   }  

   public event PropertyChangedEventHandler PropertyChanged;

   protected virtual void OnPropertyChanged([CallerMemberName] string 
   propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }

}

The error I am getting with this code is with the ShowAsync() in the TxtView.xaml.cs file. The error says "There is no argument given that corresponds to the required formal parameter 'parent' of 'OpenFileDialog.ShowAsync(Window)'"

I have tried ShowAsync(this) which fixes the error with ShowAsync but now gives me the error "Arguement 1: cannot convert from 'Txt2List.Views.TxtView' to 'Avalonia.Controls.Window'

All help is appreciated and I apologize if I am not clear, I am completely new to Avalonia, WPF, and XAML so I am learning. Please let me know if there is additional information I can provide.


回答1:


dialog.ShowAsync(); has a Window parent parameter which currently is mandatory. You are probably using an outdated Avalonia version (0.7) where it wasn't specified in the method signature.

You can get the Window for a mapped control by calling (Window)control.GetVisualRoot()



来源:https://stackoverflow.com/questions/56566570/openfiledialog-in-avalonia-error-with-showasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!