feat: update demo

This commit is contained in:
rabbitism
2023-02-25 00:16:31 +08:00
parent db4db9f1fe
commit db1c674968
3 changed files with 60 additions and 4 deletions

View File

@@ -4,14 +4,32 @@
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:pages="clr-namespace:Ursa.Demo.Pages"
xmlns:u="https://irihi.tech/ursa"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Design.DataContext>
<pages:IPv4DemoViewMode />
</Design.DataContext>
<StackPanel HorizontalAlignment="Left">
<u:IPv4Box Name="box" Width="200" />
<u:IPv4Box Width="200" IsEnabled="False" />
<ToggleButton
Name="format"
Margin="0,0,0,50"
Content="Show Leading Zeroes" />
<u:IPv4Box
Name="box"
Width="200"
ShowLeadingZero="{Binding #format.IsChecked}" />
<TextBlock Text="IP: " />
<TextBlock Text="{Binding #box.IPAddress}" />
<RepeatButton Command="{Binding ChangeAddress}" Content="Random" />
<u:IPv4Box
Width="200"
IPAddress="{Binding Address}"
ShowLeadingZero="{Binding #format.IsChecked}" />
<u:IPv4Box Width="200" IsEnabled="False" />
</StackPanel>
</UserControl>

View File

@@ -1,6 +1,9 @@
using System;
using System.Net;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Ursa.Demo.Pages;
@@ -9,10 +12,18 @@ public partial class IPv4BoxDemo : UserControl
public IPv4BoxDemo()
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
DataContext = new IPv4DemoViewMode();
}
}
public partial class IPv4DemoViewMode: ObservableObject
{
[ObservableProperty]
private IPAddress? _address;
public void ChangeAddress()
{
long l = Random.Shared.NextInt64(0x00000000FFFFFFFF);
Address = new IPAddress(l);
}
}

View File

@@ -90,6 +90,7 @@ public class IPv4Box: TemplatedControl
static IPv4Box()
{
ShowLeadingZeroProperty.Changed.AddClassHandler<IPv4Box>((o, e) => o.OnFormatChange(e));
IPAddressProperty.Changed.AddClassHandler<IPv4Box>((o, e) => o.OnIPChanged(e));
}
private void OnFormatChange(AvaloniaPropertyChangedEventArgs arg)
@@ -98,6 +99,32 @@ public class IPv4Box: TemplatedControl
ParseBytes(showLeadingZero);
}
private void OnIPChanged(AvaloniaPropertyChangedEventArgs arg)
{
IPAddress? address = arg.GetNewValue<IPAddress?>();
if (address is null)
{
foreach (var presenter in _presenters)
{
if(presenter!=null) presenter.Text = string.Empty;
}
ParseBytes(ShowLeadingZero);
}
else
{
var sections = address.ToString().Split('.');
for (int i = 0; i < 4; i++)
{
var presenter = _presenters[i];
if (presenter != null)
{
presenter.Text = sections[i];
}
}
ParseBytes(ShowLeadingZero);
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);