我正在使用OpenFileDialog
,SaveFileDialog
并OpenFolderDialog
在 Avalonia 应用程序中。
一个要求是将标题设置为某个字符串。
我实现了这一点OpenFileDialog
,SaveFileDialog
但不是。拒绝应用标题,因此只有根据操作系统语言的默认标题保持设置。
我发现在ControlCatalogStandalone中工作正确,即可以根据需要设置标题。
因此,我尝试将ControlCatalogStandalone简化为Dialogs功能,并将其转换为尽可能类似于我的测试应用程序。OpenFolderDialog
OpenFolderDialog
OpenFolderDialog
我无法做到这一点,但我发现了一些线索,可以帮助更熟练的开发人员找到我的测试应用程序行为不端的原因。ControlCatalogStandalone
的Solution和Project的结构和组成与我的 Avalonia 测试应用程序大不相同。
在我的测试应用程序中,目标框架是.
在ControlCatalogStandalone解决方案的一个项目中,目标框架是.
所以,我认为在.
我还认为,如果ControlCatalogStandalone是使用当前版本的.NET Core 3.1
.NET Standard 2.0
OpenFolderDialog
.NET Standard
.NET Core
Avalonia for Visual Studio
扩展,它不会产生框架的混合。
以下是我的测试应用程序的相关代码部分:
MainWindow.axaml:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:DialogTests.ViewModels;assembly=DialogTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="200" Height="150"
x:Class="DialogTests.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="DialogTests">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<Grid RowDefinitions="*,*,*">
<Button Grid.Column="0" Grid.Row="0" Command="{Binding OpenFileDialogCommand}" Content="OpenFileDialog"/>
<Button Grid.Column="0" Grid.Row="1" Command="{Binding SaveFileDialogCommand}" Content="SaveFileDialog"/>
<Button Grid.Column="0" Grid.Row="2" Command="{Binding OpenFolderDialogCommand}" Content="OpenFolderDialog"/>
</Grid>
</Window>
MainWindowViewModel.cs:
using Avalonia.Controls;
using DialogTests.Views;
using ReactiveUI;
using System.Collections.Generic;
using System.Reactive;
namespace DialogTests.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
OpenFileDialogCommand = ReactiveCommand.Create(() => {
new OpenFileDialog()
{
Title = "Open File Dialog",
Filters = GetFilters()
}.ShowAsync(MainWindow.Instance);
});
SaveFileDialogCommand = ReactiveCommand.Create(() =>
{
new SaveFileDialog()
{
Title = "Save File Dialog",
Filters = GetFilters()
}.ShowAsync(MainWindow.Instance);
});
OpenFolderDialogCommand = ReactiveCommand.Create(() => {
new OpenFolderDialog()
{
Title = "Open Folder Dialog"
}.ShowAsync(MainWindow.Instance);
});
}
public ReactiveCommand<Unit, Unit> OpenFileDialogCommand { get; }
public ReactiveCommand<Unit, Unit> SaveFileDialogCommand { get; }
public ReactiveCommand<Unit, Unit> OpenFolderDialogCommand { get; }
List<FileDialogFilter> GetFilters()
{
return new List<FileDialogFilter>
{
new FileDialogFilter
{
Name = "Text files", Extensions = new List<string> {"txt"}
},
new FileDialogFilter
{
Name = "All files",
Extensions = new List<string> {"*"}
}
};
}
}
}
MainWindow.axaml.cs:
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DialogTests.Views
{
public class MainWindow : Window
{
public static MainWindow Instance { get; private set; }
public MainWindow()
{
Instance = this;
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
我正在显示最后一个文件,因为我无法
Window GetWindow() => (Window)this.VisualRoot;
在我的 ViewModel 中实现该方法,该方法在ControlCatalogStandalone中被实现并在后面的代码中使用DialogsPage
(实际上是 a UserControl
)。
因此,我实现了Instance
可能是 hack 的属性。
如果您也可以给我提示最佳实践以获取实例,MainWindow
我会很高兴。
我的问题是无法在实现OpenFolderDialog
的未解决问题中设置标题.NET Core
,还是你能告诉我如何让它在我的简单测试应用程序中工作?