This commit is contained in:
Wiesław Šoltés 2022-01-28 13:42:03 +01:00
Родитель 36d50c7661
Коммит f0651027fb
5 изменённых файлов: 146 добавлений и 5 удалений

Просмотреть файл

@ -1,4 +1,5 @@
<StackPanel xmlns="https://github.com/avaloniaui" Orientation="Vertical" Spacing="4">
<StackPanel xmlns="https://github.com/avaloniaui"
Orientation="Vertical" Spacing="4">
<TextBlock Classes="h2">A control which decorates a child with a border and background</TextBlock>
<StackPanel Orientation="Vertical"

Просмотреть файл

@ -0,0 +1,43 @@
<StackPanel xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Orientation="Vertical" Spacing="4">
<TextBlock Classes="h2">A button control</TextBlock>
<StackPanel Orientation="Horizontal"
Margin="0,16,0,0"
HorizontalAlignment="Center"
Spacing="16">
<StackPanel Orientation="Vertical" Spacing="8" Width="200">
<Button>Standard _XAML Button</Button>
<Button Foreground="White">Foreground</Button>
<Button Background="{DynamicResource SystemAccentColor}">Background</Button>
<Button IsEnabled="False">Disabled</Button>
<Button Content="Re-themed">
<Button.Styles>
<Style>
<Style.Resources>
<SolidColorBrush x:Key="ThemeBorderMidBrush">Red</SolidColorBrush>
<SolidColorBrush x:Key="ThemeControlHighBrush">DarkRed</SolidColorBrush>
<SolidColorBrush x:Key="ButtonBorderBrush">Red</SolidColorBrush>
<SolidColorBrush x:Key="ButtonBackground">DarkRed</SolidColorBrush>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver">Red</SolidColorBrush>
<SolidColorBrush x:Key="ButtonBackgroundPressed">OrangeRed</SolidColorBrush>
</Style.Resources>
</Style>
</Button.Styles>
</Button>
<RepeatButton Name="RepeatButton">
<TextBlock Name="RepeatButtonTextBlock" Text="Repeat Button: 0" />
</RepeatButton>
<ToggleButton Content="Toggle Button"/>
</StackPanel>
<StackPanel Orientation="Vertical" Spacing="8" Width="150">
<Button BorderThickness="0">No Border</Button>
<Button BorderBrush="{DynamicResource SystemAccentColor}">Border Color</Button>
<Button BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="4">Thick Border</Button>
<Button BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="4" IsEnabled="False">Disabled</Button>
<Button BorderBrush="{DynamicResource SystemAccentColor}" KeyboardNavigation.IsTabStop="False">IsTabStop=False</Button>
</StackPanel>
</StackPanel>
</StackPanel>

Просмотреть файл

@ -1,5 +1,7 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Input;
using Avalonia.Controls;
using ReactiveUI;
@ -9,14 +11,55 @@ namespace XamlToy.ViewModels
{
public class MainViewModel : ViewModelBase
{
private ObservableCollection<SampleViewModel> _samples;
private SampleViewModel? _selectedSample;
private string? _xaml;
private IControl? _control;
public MainViewModel()
{
RunCommand = ReactiveCommand.Create(Run);
_samples = new ObservableCollection<SampleViewModel>();
_xaml = LoadResourceString("XamlToy.Samples.BorderPage.txt");
var assembly = typeof(MainViewModel).Assembly;
var resourceNames = assembly.GetManifestResourceNames();
foreach (var resourceName in resourceNames)
{
var xaml = LoadResourceString(resourceName);
if (xaml is { } && resourceName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
var name = GetName(resourceName);
if (name is { })
{
_samples.Add(new SampleViewModel(name, xaml));
}
}
}
_selectedSample = _samples.FirstOrDefault();
_xaml = _selectedSample?.Xaml;
this.WhenAnyValue(x => x.SelectedSample)
.WhereNotNull()
.Subscribe(x =>
{
Xaml = x.Xaml;
Control = null;
});
RunCommand = ReactiveCommand.Create(Run);
}
public ObservableCollection<SampleViewModel> Samples
{
get => _samples;
set => this.RaiseAndSetIfChanged(ref _samples, value);
}
public SampleViewModel? SelectedSample
{
get => _selectedSample;
set => this.RaiseAndSetIfChanged(ref _selectedSample, value);
}
public string? Xaml
@ -33,6 +76,17 @@ namespace XamlToy.ViewModels
public ICommand RunCommand { get; }
private string? GetName(string resourceName)
{
var parts = resourceName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2)
{
return $"{parts[parts.Length - 2]}";
}
return null;
}
private string? LoadResourceString(string name)
{
var assembly = typeof(MainViewModel).Assembly;

Просмотреть файл

@ -0,0 +1,27 @@
using ReactiveUI;
namespace XamlToy.ViewModels;
public class SampleViewModel : ViewModelBase
{
private string _name;
private string _xaml;
public string Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
public string Xaml
{
get => _xaml;
set => this.RaiseAndSetIfChanged(ref _xaml, value);
}
public SampleViewModel(string name, string xaml)
{
_name = name;
_xaml = xaml;
}
}

Просмотреть файл

@ -12,8 +12,21 @@
<Design.DataContext>
<vm:MainViewModel />
</Design.DataContext>
<Grid ColumnDefinitions="50*,Auto,50*">
<ae:TextEditor Grid.Column="0"
<Grid ColumnDefinitions="50*,Auto,50*" RowDefinitions="Auto,*">
<ComboBox Grid.Column="2"
Grid.Row="0"
Margin="6"
HorizontalAlignment="Right"
Items="{Binding Samples}"
SelectedItem="{Binding SelectedSample}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="vm:SampleViewModel">
<Label Content="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ae:TextEditor Grid.Column="0"
Grid.Row="1"
Margin="0"
Background="WhiteSmoke"
SyntaxHighlighting="XML"
@ -29,6 +42,7 @@
</i:Interaction.Behaviors>
</ae:TextEditor>
<Button Grid.Column="0"
Grid.Row="1"
Background="#2fa6e2"
Foreground="White"
Content="Run"
@ -37,11 +51,13 @@
Margin="0,10,10,0"
Command="{Binding RunCommand}" />
<GridSplitter Grid.Column="1"
Grid.Row="1"
Width="6"
ResizeBehavior="PreviousAndNext"
ResizeDirection="Columns"
Background="Transparent"/>
<ContentControl Grid.Column="2"
Grid.Row="1"
Content="{Binding Control}" />
</Grid>
</UserControl>