Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JCBurnside
Created January 23, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCBurnside/e1d74a3f5cc78cbe87c20bf00decfd25 to your computer and use it in GitHub Desktop.
Save JCBurnside/e1d74a3f5cc78cbe87c20bf00decfd25 to your computer and use it in GitHub Desktop.
Avalonia based stuff.
System.ArgumentNullException: Value cannot be null.
Parameter name: xamlType
at Portable.Xaml.XamlWriterInternalBase.WriteStartObject(XamlType xamlType)
at Portable.Xaml.XamlObjectWriter.WriteStartObject(XamlType xamlType)
at Portable.Xaml.XamlWriter.WriteNode(XamlReader reader)
at Portable.Xaml.XamlServices.Transform(XamlReader xamlReader, XamlWriter xamlWriter, Boolean closeWriter)
at Avalonia.Markup.Xaml.AvaloniaXamlLoaderPortableXaml.LoadFromReader(XamlReader reader, AvaloniaXamlContext context)
at Avalonia.Markup.Xaml.AvaloniaXamlLoaderPortableXaml.Load(Stream stream, Object rootInstance, Uri uri)
at Avalonia.DesignerSupport.DesignWindowLoader.LoadDesignerWindow(String xaml, String assemblyPath)
at Avalonia.DesignerSupport.Remote.RemoteDesignerEntryPoint.<>c__DisplayClass17_0.<OnTransportMessage>b__0()
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:CanvasTesting.ViewModels;assembly=CanvasTesting"
Icon="resm:CanvasTesting.Assets.avalonia-logo.ico"
Title="CanvasTesting"
Width="500"
Height="300">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Vertical">
<DockPanel>
<TextBox DockPanel.Dock="Right" Text="{Binding Sides}"/>
<Slider Orientation="Horizontal" Minimum="3" Maximum="30" Value="{Binding Sides}"/>
</DockPanel>
<DockPanel>
<TextBox DockPanel.Dock="Right" Text="{Binding CenterX}"/>
<Slider Orientation="Horizontal" Minimum="0" Maximum="500" Value="{Binding CenterX}"/>
</DockPanel>
<DockPanel>
<TextBox DockPanel.Dock="Right" Text="{Biding CenterY}"/>
<Slider Orientation="Horizontal" Minimum="0" Maximum="300" Value="{Biding CenterY}"/>
</DockPanel>
</StackPanel>
<ItemsControl Items="{Binding Lines}"
Width="{Binding Path=CanvasWidth, Mode=OneWayToSource}"
Height="{Binding Path=CanvasHeight, Mode=OneWayToSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.DataTemplates>
<DataTemplate DataType="{x:Type vm:LinePoints}">
<Line StartPoint="{Binding Start}" EndPoint="{Binding End}" Stroke="Silver" StrokeThickness="3"/>
</DataTemplate>
</ItemsControl.DataTemplates>
</ItemsControl>
</DockPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Text;
using Avalonia;
using ReactiveUI;
using System.Linq;
using static System.Math;
using Avalonia.Controls.Shapes;
using Avalonia.Media.Immutable;
namespace CanvasTesting.ViewModels
{
public class MainWindowViewModel : ReactiveObject
{
private int sides = 5;
private int radius = 10;
private int centerY;
private int centerX;
public int Sides
{
get => sides;
set
{
if (sides != value)
{
sides = value;
this.RaisePropertyChanged();
this.RaisePropertyChanged(nameof(Lines));
}
}
}
public int CenterX
{
get => centerX;
set
{
if (centerX != value)
{
centerX = value;
this.RaisePropertyChanged();
this.RaisePropertyChanged(nameof(Lines));
}
}
}
public int CenterY
{
get => centerY;
set
{
if (centerY != value)
{
centerY = value;
this.RaisePropertyChanged();
this.RaisePropertyChanged(nameof(Lines));
}
}
}
public int Radius
{
get => radius;
set
{
if (radius != value)
{
radius = value;
this.RaisePropertyChanged();
this.RaisePropertyChanged(nameof(Lines));
}
}
}
private readonly ImmutableSolidColorBrush brush = new ImmutableSolidColorBrush(0xC0C0C0);
public List<LinePoints> Lines
{
get => Enumerable.Range(0, sides).Select(i =>
new LinePoints
{
Start = new Point(GenX(i), GenY(i)),
End = new Point(GenX((i + sides / 2) % sides), GenY((i + sides / 2) % sides)),
}).ToList();
}
private double GenX(int ctr) => radius * Cos(2 * PI * ctr / sides) + centerX;
private double GenY(int ctr) => radius * Sin(2 * PI * ctr / sides) + centerY;
}
public class LinePoints : ReactiveObject
{
public Point Start { get; set; }
public Point End { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment