我对 Avalonia/WPF、Xaml 和桌面开发总体来说是新手,所以请原谅并澄清我所展示的任何相关误解。我将继续研究可用的文档,但我很难找到能够解决我所困扰的问题的材料。
我正在尝试使用推荐的 MVVM 模式和关联的 Avalonia 项目模板,在我的 Avalonia 应用程序中实现基于组合根、构造函数注入的依赖注入系统。我对 Microsoft.Extensions.DependencyInjection 包有一定的熟悉,因此一直在尝试使用该系统。
在基于此 DI 框架以及其他框架的 WPF 和 Avalonia 教程之间,我尝试拼凑出一个可行的解决方案。我认为我已经在概念上弄清楚了注册服务和视图模型并适当地为这些类设置构造函数,以便框架在实例化时将依赖项注入到这些类中。然而,我遇到的问题是如何实现 View 类的构造函数注入。
我尝试将 MainWindow 和 MainWindowViewModel 注册为服务:
// App.axaml.cs
public partial class App : Application
{
private IServiceProvider _services;
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
ConfigureServiceProvider();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = _services.GetService<MainWindow>();
}
base.OnFrameworkInitializationCompleted();
}
private void ConfigureServiceProvider()
{
var services = ConfigureServices();
_services = services.BuildServiceProvider();
}
private static IServiceCollection ConfigureServices()
{ …
Run Code Online (Sandbox Code Playgroud) 我正在使用OpenFileDialog
,SaveFileDialog
并OpenFolderDialog
在 Avalonia 应用程序中工作。
一个要求是将标题设置为特定字符串。
我实现了这一点OpenFileDialog
,SaveFileDialog
但不是为了OpenFolderDialog
。
OpenFolderDialog
拒绝应用标题,因此仅保留根据操作系统语言设置的默认标题。
我发现在ControlCatalogStandalone中工作OpenFolderDialog
正常,即标题可以根据需要设置。
因此,我尝试将ControlCatalogStandalone简化为对话框功能,并将其转换为与我的测试应用程序尽可能相似。
我无法实现这一点,但我发现了一些线索,可以帮助技术更熟练的开发人员找到我的测试应用程序行为不当的原因。ControlCatalogStandalone的解决方案
和项目的结构和组成与我的 Avalonia 测试应用程序有很大不同。
在我的测试应用程序中,目标框架是. 在ControlCatalogStandalone解决方案
的一个项目中,目标框架是.
所以,我认为在 中工作正确,但在.
我还认为,如果ControlCatalogStandalone是使用当前版本的扩展从头开始构建的,它不会产生框架的混合。
以下是我的测试应用程序的相关代码部分:
MainWindow.axaml:.NET Core 3.1
.NET Standard 2.0
OpenFolderDialog
.NET Standard
.NET Core
Avalonia for Visual Studio
<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/> …
Run Code Online (Sandbox Code Playgroud) 我正在考虑将我在 WPF 中制作的应用程序移植到 Avalonia。
我的第一个要求是启用点击透明度(与<Window Background="{x:Null}" WindowStyle="None" AllowsTransparency="True"
WPF 中相同)。
我已经尝试过了,但它看起来不起作用(至少在 Windows 上)。
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaTitleBarHeightHint="-1"
TransparencyBackgroundFallback="Transparent"
Background="{x:Null}"
Run Code Online (Sandbox Code Playgroud)
目标操作系统将是 Windows、macOS 和 Linux。因此,在将其安装在虚拟机中或购买新设备之前,我需要知道是否可以在所有操作系统中都具有该功能。
我正在尝试弄清楚如何在 Avalonia 中制作动画。
我有一条包含 4 条线段的路径,我想将每个点设置为新位置的动画。在 WPF 中我是这样做的:
public void AnimatePoints(PointCollection pts, TimeSpan timespan, bool randomized = true, Action onFinished = null)
{
Points = PointCollection.Parse(PathString);
//PathFigure needs an animation too (for the start point), otherwise the first point always stays in one place
var pfa = new PointAnimation(pts[0], timespan);
if (onFinished != null)
{
pfa.Completed += (sender, args) => onFinished();
}
PathFigure.BeginAnimation(PathFigure.StartPointProperty, pfa);
for (int i = 0; i < pts.Count; i++)
{
var pa = new PointAnimation(pts[i], timespan); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 ReactiveUI 与 Avalonia 一起使用。由于 Avalonia 0.10 预览版中的初始化顺序,以下代码失败:
class ViewModel : IActivatableViewModel
{
public ViewModel(){
this.WhenActivated(disposables => {
_myProperty = observable.ToProperty(this, nameof(MyProperty)).DisposeWith(disposables).
});
}
private ObservableAsPropertyHelper<object> _myProperty = null!;
public object MyProperty => _myProperty.Value;
}
Run Code Online (Sandbox Code Playgroud)
因为WhenActivated
在视图绑定到 viewModel 之后调用(因此 _myProperty 为 null)。
我认为没有简单的解决方法需要大量的修改、手动提高属性等等。
所以问题是:
如何在 Avalonia 中使用 OAPH 和 WhenActivated?
I'm trying out styles in avalonia and most works, except for pseudoclasses, they just get ignored.
I have created a window and all styles are in there and I have created a user control (with a button on - the pseudoclasses are on the button), using the styles. I do not use code, only xaml to define the styles.
I have tried it out in the "Style Selector" for the button as "Button:pseudoclassname" and "Button.le:pseudoclassname". I have also tried "Button:pointerover" …
我正在使用 avalonia 创建一个应用程序,由于可重用性,它在另一个程序集中有一些 UI 内容。在此附加程序集中,我想要一个由应用程序项目和其他项目引用的 Resource.xml 文件。它包含样式等。
内容:
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Style x:Key="normal_button" Selector="Button.normal_button">
<Setter Property="Margin" Value="0" />
<Setter Property="Height" Value="25" />
<Setter Property="MinWidth" Value="75" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
错误:
Unable to find suitable setter or adder for property Content of type Avalonia.Styling:Avalonia.Controls.ResourceDictionary for argument Avalonia.Styling:Avalonia.Styling.Style, available setter parameter lists are: ...
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?.csproj 文件中是否需要某些条目?
我有一个简单的 Avalonia 表格:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaExperiment.MainWindow"
Title="AvaloniaExperiment">
<StackPanel>
<TextBlock>Welcome to Avalonia!</TextBlock>
<Button Name="btn" Click="btn_OnClick">Fred!</Button>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
以及后面代码中的一个方法(我想这样做,直到我熟悉 Avalonia,然后也许我会尝试 MVVM):
private void btn_OnClick()
{
btn.Text = "Ginger";
}
Run Code Online (Sandbox Code Playgroud)
但是我收到这些编译错误:
名称 btn 在当前上下文中不存在(在后面的代码中)
无法为参数 System.Private.CoreLib:System.String 的 Avalonia.Controls:Avalonia.Controls.Button 类型的属性 Click 找到合适的设置器或加法器,可用的设置器参数列表为:System.EventHandler`1[[Avalonia.Interactivity. RoutedEventArgs、Avalonia.Interactivity、Version=0.9.0.0、Culture=neutral、PublicKeyToken=null]](在 XAML 中)
无法为参数 System.Runtime:System.String 的 Avalonia.Controls:Avalonia.Controls.Button 类型的属性命令找到合适的设置器或加法器,可用的设置器参数列表为:Avalonia.UnsetValueType Avalonia.Data.IBinding System.Windows.Input .ICommand(也在 XAML 中)
连接此事件处理程序时我可能做错了什么?
我正在尝试使用Avalonia 项目中包含的文件来实现拖放。\n由于我无法让它工作,并且我认为这可能是一种特殊情况,因此我尝试制作一个类似的示例,例如ControlCatalogStandalone中的示例。\n虽然ControlCatalogStandalone中的代码按预期工作,但我的测试应用程序中的几乎相同的代码无法正常工作。\ nControlCatalogStandalone中的相关代码属于 UserControl,在我的应用程序中它属于. 这可能是不当行为的原因吗?\n我基于Visual Studio 2019 中的NuGet 包创建了一个新的Avalonia MVVM 应用程序。 \n我也尝试了徒劳的版本。\n这是 XAML 文件:ListBox
Window
ListBox
MainWindow
0.9.11
0.10.0-preview2
<Window xmlns="https://github.com/avaloniaui"\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\n xmlns:vm="clr-namespace:DragAndDropTests.ViewModels;assembly=DragAndDropTests"\n xmlns:d="http://schemas.microsoft.com/expression/blend/2008"\n xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"\n mc:Ignorable="d" Width="400" Height="200"\n x:Class="DragAndDropTests.Views.MainWindow"\n Icon="/Assets/avalonia-logo.ico"\n Title="DragAndDropTests">\n\n <Design.DataContext>\n <vm:MainWindowViewModel/>\n </Design.DataContext>\n\n <StackPanel Orientation="Vertical" Spacing="4">\n <TextBlock Classes="h1">Drag+Drop</TextBlock>\n <TextBlock Classes="h2">Example of Drag+Drop capabilities</TextBlock>\n\n <StackPanel Orientation="Horizontal"\n Margin="0,16,0,0"\n HorizontalAlignment="Center"\n Spacing="16">\n <Border BorderBrush="{DynamicResource ThemeAccentBrush}" BorderThickness="2" Padding="16" Name="DragMe">\n <TextBlock Name="DragState">Drag Me</TextBlock>\n </Border>\n <Border Background="{DynamicResource ThemeAccentBrush2}" Padding="16"\n DragDrop.AllowDrop="True">\n <TextBlock Name="DropState">Drop some text …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试各种技术来尝试使按钮标签文本居中对齐。当我查看 Avalonia DevTools 检查器中的按钮时,我可以看到 AccessText TextAlignment 始终设置为 Left。
这是应用样式的一种尝试,但不起作用:
<Style Selector="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
修改 TextBlock TextAlignment 的正确方法是什么?