Add files support
This commit is contained in:
Родитель
a5536e8923
Коммит
426726e874
|
@ -9,7 +9,7 @@ namespace BlazorDemo
|
|||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
public static IInputDialogService? InputDialogService { get; set;}
|
||||
public static IInputDialogService? InputDialogService { get; set; }
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
using ReactiveUI;
|
||||
|
||||
namespace BlazorDemo.ViewModels
|
||||
{
|
||||
public class FileViewModel : ViewModelBase
|
||||
{
|
||||
private string _name;
|
||||
private string _contents;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set => this.RaiseAndSetIfChanged(ref _name, value);
|
||||
}
|
||||
|
||||
public string Contents
|
||||
{
|
||||
get => _contents;
|
||||
set => this.RaiseAndSetIfChanged(ref _contents, value);
|
||||
}
|
||||
|
||||
public FileViewModel(string name, string contents)
|
||||
{
|
||||
_name = name;
|
||||
_contents = contents;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections.ObjectModel;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace BlazorDemo.ViewModels
|
||||
{
|
||||
public class MainViewModel : ViewModelBase
|
||||
{
|
||||
public string Greeting => "Welcome to Avalonia!";
|
||||
private FileViewModel? _selectedFile;
|
||||
private ObservableCollection<FileViewModel> _files;
|
||||
|
||||
public FileViewModel? SelectedFile
|
||||
{
|
||||
get => _selectedFile;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedFile, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<FileViewModel> Files
|
||||
{
|
||||
get => _files;
|
||||
set => this.RaiseAndSetIfChanged(ref _files, value);
|
||||
}
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
_files = new ObservableCollection<FileViewModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,29 +12,41 @@
|
|||
</Design.DataContext>
|
||||
|
||||
<DockPanel>
|
||||
<StackPanel DockPanel.Dock="Left" Spacing="6" Margin="6">
|
||||
<Button Name="buttonSingle"
|
||||
Content="Open Single File"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
<Button Name="buttonMultiple"
|
||||
Content="Open Multiple Files"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
<Button Name="buttonFolder"
|
||||
Content="Open Folder"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
</StackPanel>
|
||||
<TextBox Name="text"
|
||||
Text=""
|
||||
<DockPanel DockPanel.Dock="Left">
|
||||
<StackPanel DockPanel.Dock="Top" Spacing="6" Margin="6,6,0,6">
|
||||
<Button Name="buttonSingle"
|
||||
Content="Open Single File"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
<Button Name="buttonMultiple"
|
||||
Content="Open Multiple Files"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
<Button Name="buttonFolder"
|
||||
Content="Open Folder"
|
||||
HorizontalContentAlignment="Center"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="Left"/>
|
||||
</StackPanel>
|
||||
<ListBox Items="{Binding Files}"
|
||||
SelectedItem="{Binding SelectedFile}"
|
||||
Margin="6,0,0,6">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</DockPanel>
|
||||
|
||||
<TextBox Text="{Binding SelectedFile.Contents, FallbackValue=''}"
|
||||
Margin="6"/>
|
||||
|
||||
</DockPanel>
|
||||
|
||||
</UserControl>
|
||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.IO;
|
||||
using Avalonia.Controls;
|
||||
using BlazorDemo.Dialogs;
|
||||
using BlazorDemo.ViewModels;
|
||||
|
||||
namespace BlazorDemo.Views
|
||||
{
|
||||
|
@ -74,12 +75,14 @@ namespace BlazorDemo.Views
|
|||
|
||||
private void Callback(Stream stream, string name)
|
||||
{
|
||||
if (text is { })
|
||||
if (DataContext is MainViewModel viewModel)
|
||||
{
|
||||
Console.WriteLine($"Callback(): {name}");
|
||||
using var reader = new StreamReader(stream);
|
||||
var str = reader.ReadToEnd();
|
||||
text.Text = str;
|
||||
var contents = reader.ReadToEnd();
|
||||
var file = new FileViewModel(name, contents);
|
||||
viewModel.Files.Add(file);
|
||||
viewModel.SelectedFile = file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче