扩展请参考
1
|
https:
//www.cnblogs.com/dalgleish/p/18972924
|
NonCompiledXaml.axaml代码
<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="AvaloniaUI.NonCompiledXaml" Title="NonCompiledXaml"> <ScrollViewer> <StackPanel Name="mainPanel" Background="#f0f0f0"> <TextBlock Text="" FontFamily="{StaticResource IconFont}" FontSize="20" Margin="5" HorizontalAlignment="Center"/> <Button Content="加载动态内容" Click="Button_Click" Margin="0,0,0,5"/> </StackPanel> </ScrollViewer> </Window>
NonCompiledXaml.axaml.cs代码,其中Window1是一个简单的Window窗口。新扩展已经支持多个cs文件编译,并且支持AvaloniaUseCompiledBindingsByDefault = true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
using
Avalonia;
using
Avalonia.Controls;
using
Avalonia.Interactivity;
using
Avalonia.Platform;
using
Shares.Avalonia;
using
System;
using
System.IO;
using
System.Text;
using
System.Threading.Tasks;
namespace
AvaloniaUI;
public
partial
class
NonCompiledXaml : Window
{
public
NonCompiledXaml()
{
InitializeComponent();
}
private
async
void
Button_Click(
object
? sender, RoutedEventArgs e)
{
Control control = await
this
.RunResourceAsync(
"Window1"
);
mainPanel.Children.Add(control);
var
xaml =
@"
<UserControl xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class='SampleView'>
<StackPanel Margin='20'>
<TextBlock Text='{Binding Message}' FontSize='24' Foreground='Blue'/>
</StackPanel>
</UserControl>
"
;
var
code =
@"
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
public class SampleView : UserControl
{
public SampleView()
{
this.DataContext = new ViewModel();
}
public class ViewModel : ObservableObject
{
private string message = ""Hello from dynamic SampleView!"";
public string Message
{
get => message;
set => SetProperty(ref message, value);
}
}
}
"
;
Control xamlControl = await
this
.RunXamlAsync(xaml, code);
mainPanel.Children.Add(xamlControl);
var
xaml1 =
@"<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""
xmlns:local=""using:AvaloniaUI""
mc:Ignorable=""d"" d:DesignWidth=""800"" d:DesignHeight=""450""
x:Class=""AvaloniaUI.Window2""
Title=""Window2"">
<StackPanel Margin=""20"">
<TextBlock x:DataType=""local:ViewModel"" Text=""{Binding Message}"" FontSize=""24"" Foreground=""Blue""/>
</StackPanel>
</Window>
"
;
var
code1 =
@"using Avalonia;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
namespace AvaloniaUI;
public partial class Window2 : Window
{
public Window2()
{
this.DataContext = new ViewModel();
}
}
public class ViewModel : ObservableObject
{
private string message = ""Hello from dynamic Window2View!"";
public string Message
{
get => message;
set => SetProperty(ref message, value);
}
}
"
;
Control xamlControl1 = await
this
.RunXamlAsync(xaml1, code1,
"Window2"
);
mainPanel.Children.Add(xamlControl1);
}
}
|
运行效果
原创作者: dalgleish 转载于: https://www.cnblogs.com/dalgleish/p/18974674