strikan
strikan

Reputation: 3

.NET WPF always get an error

Hello I'm learning WPF but I always seem to get an error when I try this:

<Window x:Class="Projectosaurus.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:Projectosaurus"> //// this is where I mapped my namespace
<Grid>
    <Canvas>
        <Ellipse Fill="DarkGreen" Width="100" Height="75" local:Rotation.Angle="60"/> 

//and when I try to get it here I always get an error that the name (in this case) "Rotation" does not exist in the namespace (in this case) Projectosaurus, yet it does I've created a class "Rotation" inside of this project and its Namespace is Projectosaurus, what am I doing wrong? (I get this every time in every project)

namespace Projectosaurus
{
class Rotation : DependencyObject
{


    public static double GetAngle(DependencyObject obj)
    {
        return (double)obj.GetValue(AngleProperty);
    }

    public static void SetAngle(DependencyObject obj, double value)
    {
        obj.SetValue(AngleProperty, value);
    }

    // Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AngleProperty =
        DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(Rotation), new PropertyMetadata(0.0, OnAngleChanged));

    private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = d as UIElement;
        if (element != null)
        {
            element.RenderTransformOrigin = new Point(.5, .5);
            element.RenderTransform = new RotateTransform(
            (double)e.NewValue);
        }
    }

**FIXED IT! First time I had this problem yesterday, my immediate thought was to make the class "public" but it did not work, but that was because I did not rebuild it! Thanks everyone.

Upvotes: 0

Views: 69

Answers (1)

123 456 789 0
123 456 789 0

Reputation: 10865

You need to make Rotation class public. Also make it static and you don't need to inherit from DependencyProperty you are using it as an AttachProperty.

I don't see where you initialize <local:Rotation/>

public static class Rotation 
{


public static double GetAngle(DependencyObject obj)
{
    return (double)obj.GetValue(AngleProperty);
}

public static void SetAngle(DependencyObject obj, double value)
{
    obj.SetValue(AngleProperty, value);
}

// Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty AngleProperty =
    DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(Rotation), new PropertyMetadata(0.0, OnAngleChanged));

private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var element = d as UIElement;
    if (element != null)
    {
        element.RenderTransformOrigin = new Point(.5, .5);
        element.RenderTransform = new RotateTransform(
        (double)e.NewValue);
    }
}

Upvotes: 1

Related Questions